index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <script lang="ts" setup>
  2. import { message } from 'ant-design-vue';
  3. import { useVbenForm, z } from '#/adapter/form';
  4. const [Form] = useVbenForm({
  5. // 所有表单项共用,可单独在表单内覆盖
  6. commonConfig: {
  7. // 所有表单项
  8. componentProps: {
  9. class: 'w-full',
  10. },
  11. },
  12. // 提交函数
  13. handleSubmit: onSubmit,
  14. // 垂直布局,label和input在不同行,值为vertical
  15. // 水平布局,label和input在同一行
  16. layout: 'horizontal',
  17. schema: [
  18. {
  19. // 组件需要在 #/adapter.ts内注册,并加上类型
  20. component: 'Input',
  21. // 对应组件的参数
  22. componentProps: {
  23. placeholder: '请输入',
  24. },
  25. // 字段名
  26. fieldName: 'field1',
  27. // 界面显示的label
  28. label: '字段1',
  29. rules: 'required',
  30. },
  31. {
  32. component: 'Input',
  33. componentProps: {
  34. placeholder: '请输入',
  35. },
  36. defaultValue: '默认值',
  37. fieldName: 'field2',
  38. label: '默认值(必填)',
  39. rules: 'required',
  40. },
  41. {
  42. component: 'Input',
  43. componentProps: {
  44. placeholder: '请输入',
  45. },
  46. fieldName: 'field3',
  47. label: '默认值(非必填)',
  48. rules: z.string().default('默认值').optional(),
  49. },
  50. {
  51. component: 'Input',
  52. componentProps: {
  53. placeholder: '请输入',
  54. },
  55. fieldName: 'field31',
  56. label: '自定义信息',
  57. rules: z.string().min(1, { message: '最少输入1个字符' }),
  58. },
  59. {
  60. component: 'Input',
  61. // 对应组件的参数
  62. componentProps: {
  63. placeholder: '请输入',
  64. },
  65. // 字段名
  66. fieldName: 'field4',
  67. // 界面显示的label
  68. label: '邮箱',
  69. rules: z.string().email('请输入正确的邮箱'),
  70. },
  71. {
  72. component: 'InputNumber',
  73. componentProps: {
  74. placeholder: '请输入',
  75. },
  76. fieldName: 'number',
  77. label: '数字',
  78. rules: 'required',
  79. },
  80. {
  81. component: 'Select',
  82. componentProps: {
  83. allowClear: true,
  84. filterOption: true,
  85. options: [
  86. {
  87. label: '选项1',
  88. value: '1',
  89. },
  90. {
  91. label: '选项2',
  92. value: '2',
  93. },
  94. ],
  95. placeholder: '请选择',
  96. showSearch: true,
  97. },
  98. defaultValue: undefined,
  99. fieldName: 'options',
  100. label: '下拉选',
  101. rules: 'selectRequired',
  102. },
  103. {
  104. component: 'RadioGroup',
  105. componentProps: {
  106. options: [
  107. {
  108. label: '选项1',
  109. value: '1',
  110. },
  111. {
  112. label: '选项2',
  113. value: '2',
  114. },
  115. ],
  116. },
  117. fieldName: 'radioGroup',
  118. label: '单选组',
  119. rules: 'selectRequired',
  120. },
  121. {
  122. component: 'CheckboxGroup',
  123. componentProps: {
  124. name: 'cname',
  125. options: [
  126. {
  127. label: '选项1',
  128. value: '1',
  129. },
  130. {
  131. label: '选项2',
  132. value: '2',
  133. },
  134. ],
  135. },
  136. fieldName: 'checkboxGroup',
  137. label: '多选组',
  138. rules: 'selectRequired',
  139. },
  140. {
  141. component: 'Checkbox',
  142. fieldName: 'checkbox',
  143. label: '',
  144. renderComponentContent: () => {
  145. return {
  146. default: () => ['我已阅读并同意'],
  147. };
  148. },
  149. rules: 'selectRequired',
  150. },
  151. {
  152. component: 'DatePicker',
  153. defaultValue: undefined,
  154. fieldName: 'datePicker',
  155. label: '日期选择框',
  156. rules: 'selectRequired',
  157. },
  158. {
  159. component: 'RangePicker',
  160. defaultValue: undefined,
  161. fieldName: 'rangePicker',
  162. label: '区间选择框',
  163. rules: 'selectRequired',
  164. },
  165. {
  166. component: 'InputPassword',
  167. componentProps: {
  168. placeholder: '请输入',
  169. },
  170. fieldName: 'password',
  171. label: '密码',
  172. rules: 'required',
  173. },
  174. ],
  175. wrapperClass: 'grid-cols-1',
  176. });
  177. function onSubmit(values: Record<string, any>) {
  178. message.success({
  179. content: `form values: ${JSON.stringify(values)}`,
  180. });
  181. }
  182. </script>
  183. <template>
  184. <Form />
  185. </template>