index.vue 4.1 KB

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