basic.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <script lang="ts" setup>
  2. import { Page } from '@vben/common-ui';
  3. import { Button, Card, message } from 'ant-design-vue';
  4. import dayjs from 'dayjs';
  5. import { useVbenForm } from '#/adapter';
  6. const [BaseForm, baseFormApi] = useVbenForm({
  7. // 所有表单项共用,可单独在表单内覆盖
  8. commonConfig: {
  9. // 所有表单项
  10. componentProps: {
  11. class: 'w-full',
  12. },
  13. },
  14. // 提交函数
  15. handleSubmit: onSubmit,
  16. // 垂直布局,label和input在不同行,值为vertical
  17. // 水平布局,label和input在同一行
  18. layout: 'horizontal',
  19. schema: [
  20. {
  21. // 组件需要在 #/adapter.ts内注册,并加上类型
  22. component: 'Input',
  23. // 对应组件的参数
  24. componentProps: {
  25. placeholder: '请输入用户名',
  26. },
  27. // 字段名
  28. fieldName: 'username',
  29. // 界面显示的label
  30. label: '字符串',
  31. },
  32. {
  33. component: 'InputPassword',
  34. componentProps: {
  35. placeholder: '请输入密码',
  36. },
  37. fieldName: 'password',
  38. label: '密码',
  39. },
  40. {
  41. component: 'InputNumber',
  42. componentProps: {
  43. placeholder: '请输入',
  44. },
  45. fieldName: 'number',
  46. label: '数字(带后缀)',
  47. suffix: () => '¥',
  48. },
  49. {
  50. component: 'Select',
  51. componentProps: {
  52. allowClear: true,
  53. filterOption: true,
  54. options: [
  55. {
  56. label: '选项1',
  57. value: '1',
  58. },
  59. {
  60. label: '选项2',
  61. value: '2',
  62. },
  63. ],
  64. placeholder: '请选择',
  65. showSearch: true,
  66. },
  67. fieldName: 'options',
  68. label: '下拉选',
  69. },
  70. {
  71. component: 'RadioGroup',
  72. componentProps: {
  73. options: [
  74. {
  75. label: '选项1',
  76. value: '1',
  77. },
  78. {
  79. label: '选项2',
  80. value: '2',
  81. },
  82. ],
  83. },
  84. fieldName: 'radioGroup',
  85. label: '单选组',
  86. },
  87. {
  88. component: 'Radio',
  89. fieldName: 'radio',
  90. label: '',
  91. renderComponentContent: () => {
  92. return {
  93. default: () => ['Radio'],
  94. };
  95. },
  96. },
  97. {
  98. component: 'CheckboxGroup',
  99. componentProps: {
  100. name: 'cname',
  101. options: [
  102. {
  103. label: '选项1',
  104. value: '1',
  105. },
  106. {
  107. label: '选项2',
  108. value: '2',
  109. },
  110. ],
  111. },
  112. fieldName: 'checkboxGroup',
  113. label: '多选组',
  114. },
  115. {
  116. component: 'Checkbox',
  117. fieldName: 'checkbox',
  118. label: '',
  119. renderComponentContent: () => {
  120. return {
  121. default: () => ['我已阅读并同意'],
  122. };
  123. },
  124. },
  125. {
  126. component: 'Mentions',
  127. componentProps: {
  128. options: [
  129. {
  130. label: 'afc163',
  131. value: 'afc163',
  132. },
  133. {
  134. label: 'zombieJ',
  135. value: 'zombieJ',
  136. },
  137. ],
  138. placeholder: '请输入',
  139. },
  140. fieldName: 'mentions',
  141. label: '提及',
  142. },
  143. {
  144. component: 'Rate',
  145. fieldName: 'rate',
  146. label: '评分',
  147. },
  148. {
  149. component: 'Switch',
  150. componentProps: {
  151. class: 'w-auto',
  152. },
  153. fieldName: 'switch',
  154. label: '开关',
  155. },
  156. {
  157. component: 'DatePicker',
  158. fieldName: 'datePicker',
  159. label: '日期选择框',
  160. },
  161. {
  162. component: 'RangePicker',
  163. fieldName: 'rangePicker',
  164. label: '范围选择器',
  165. },
  166. {
  167. component: 'TimePicker',
  168. fieldName: 'timePicker',
  169. label: '时间选择框',
  170. },
  171. {
  172. component: 'TreeSelect',
  173. componentProps: {
  174. allowClear: true,
  175. placeholder: '请选择',
  176. showSearch: true,
  177. treeData: [
  178. {
  179. label: 'root 1',
  180. value: 'root 1',
  181. children: [
  182. {
  183. label: 'parent 1',
  184. value: 'parent 1',
  185. children: [
  186. {
  187. label: 'parent 1-0',
  188. value: 'parent 1-0',
  189. children: [
  190. {
  191. label: 'my leaf',
  192. value: 'leaf1',
  193. },
  194. {
  195. label: 'your leaf',
  196. value: 'leaf2',
  197. },
  198. ],
  199. },
  200. {
  201. label: 'parent 1-1',
  202. value: 'parent 1-1',
  203. },
  204. ],
  205. },
  206. {
  207. label: 'parent 2',
  208. value: 'parent 2',
  209. },
  210. ],
  211. },
  212. ],
  213. treeNodeFilterProp: 'label',
  214. },
  215. fieldName: 'treeSelect',
  216. label: '树选择',
  217. },
  218. ],
  219. // 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
  220. wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
  221. });
  222. const [CustomLayoutForm] = useVbenForm({
  223. // 所有表单项共用,可单独在表单内覆盖
  224. commonConfig: {
  225. // 所有表单项
  226. componentProps: {
  227. class: 'w-full',
  228. },
  229. },
  230. layout: 'horizontal',
  231. schema: [
  232. {
  233. component: 'Input',
  234. componentProps: {
  235. placeholder: '请输入',
  236. },
  237. fieldName: 'field1',
  238. label: '字符串',
  239. },
  240. {
  241. component: 'Input',
  242. componentProps: {
  243. placeholder: '请输入',
  244. },
  245. fieldName: 'field2',
  246. label: '字符串',
  247. },
  248. {
  249. component: 'Input',
  250. componentProps: {
  251. placeholder: '请输入',
  252. },
  253. fieldName: 'field3',
  254. label: '字符串',
  255. },
  256. {
  257. component: 'Input',
  258. componentProps: {
  259. placeholder: '请输入',
  260. },
  261. fieldName: 'field4',
  262. label: '字符串',
  263. },
  264. {
  265. component: 'Input',
  266. componentProps: {
  267. placeholder: '请输入',
  268. },
  269. fieldName: 'field5',
  270. // 从第三列开始 相当于中间空了一列
  271. formItemClass: 'col-start-3',
  272. label: '前面空了一列',
  273. },
  274. {
  275. component: 'Input',
  276. componentProps: {
  277. placeholder: '请输入',
  278. },
  279. fieldName: 'field6',
  280. // 占满三列空间
  281. formItemClass: 'col-span-3',
  282. label: '占满三列',
  283. },
  284. {
  285. component: 'Input',
  286. componentProps: {
  287. placeholder: '请输入',
  288. },
  289. fieldName: 'field7',
  290. // 占满2列空间 从第二列开始 相当于前面空了一列
  291. formItemClass: 'col-span-2 col-start-2',
  292. label: '占满2列',
  293. },
  294. {
  295. component: 'Input',
  296. componentProps: {
  297. placeholder: '请输入',
  298. },
  299. fieldName: 'field8',
  300. // 左右留空
  301. formItemClass: 'col-start-2',
  302. label: '左右留空',
  303. },
  304. {
  305. component: 'Input',
  306. componentProps: {
  307. placeholder: '请输入',
  308. },
  309. fieldName: 'field9',
  310. formItemClass: 'col-start-1',
  311. label: '占满2列',
  312. },
  313. ],
  314. // 一共三列
  315. wrapperClass: 'lg:grid-cols-3',
  316. });
  317. function onSubmit(values: Record<string, any>) {
  318. message.success({
  319. content: `form values: ${JSON.stringify(values)}`,
  320. });
  321. }
  322. function handleSetFormValue() {
  323. /**
  324. * 设置表单值(多个)
  325. */
  326. baseFormApi.setValues({
  327. checkboxGroup: ['1'],
  328. datePicker: dayjs('2022-01-01'),
  329. mentions: '@afc163',
  330. number: 3,
  331. options: '1',
  332. password: '2',
  333. radioGroup: '1',
  334. rangePicker: [dayjs('2022-01-01'), dayjs('2022-01-02')],
  335. rate: 3,
  336. switch: true,
  337. timePicker: dayjs('2022-01-01 12:00:00'),
  338. treeSelect: 'leaf1',
  339. username: '1',
  340. });
  341. // 设置单个表单值
  342. baseFormApi.setFieldValue('checkbox', true);
  343. }
  344. </script>
  345. <template>
  346. <Page
  347. content-class="flex flex-col gap-4"
  348. description="表单组件基础示例,请注意,该页面用到的参数代码会添加一些简单注释,方便理解,请仔细查看。"
  349. title="表单组件"
  350. >
  351. <Card title="基础示例">
  352. <template #extra>
  353. <Button type="primary" @click="handleSetFormValue">设置表单值</Button>
  354. </template>
  355. <BaseForm />
  356. </Card>
  357. <Card title="使用tailwind自定义布局">
  358. <CustomLayoutForm />
  359. </Card>
  360. </Page>
  361. </template>