basic.vue 9.6 KB

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