UseForm.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <template>
  2. <PageWrapper title="UseForm操作示例">
  3. <a-button class="mb-4" type="primary" @click="showDrawer"> 更改设置 </a-button>
  4. <Drawer v-model:open="visible" title="更改设置" placement="right">
  5. <BasicForm ref="settingFormRef" @register="registerSetting" @submit="handleSubmitSetting">
  6. <template #other>
  7. <Space>
  8. <a-button
  9. @click="() => withClose({ resetButtonOptions: { disabled: true, text: '重置New' } })"
  10. >
  11. 修改重置按钮
  12. </a-button>
  13. <a-button
  14. @click="() => withClose({ submitButtonOptions: { disabled: true, loading: true } })"
  15. >
  16. 修改查询按钮
  17. </a-button>
  18. <a-button @click="handleLoad" class="mr-2"> 联动回显 </a-button>
  19. </Space>
  20. </template>
  21. </BasicForm>
  22. <template #extra>
  23. <Space>
  24. <a-button @click="resetSettings">重置设置</a-button>
  25. <a-button type="primary" @click="onSettings">应用</a-button>
  26. </Space>
  27. </template>
  28. </Drawer>
  29. <CollapseContainer title="useForm示例">
  30. <BasicForm @register="register" @submit="handleSubmit" />
  31. </CollapseContainer>
  32. </PageWrapper>
  33. </template>
  34. <script lang="ts">
  35. import { defineComponent, ref } from 'vue';
  36. import { Drawer, Space } from 'ant-design-vue';
  37. import { BasicForm, FormSchema, useForm, type FormProps } from '/@/components/Form';
  38. import { CollapseContainer } from '/@/components/Container';
  39. import { PageWrapper } from '/@/components/Page';
  40. import { areaRecord } from '/@/api/demo/cascader';
  41. const sizeList = [
  42. { value: 'large', label: 'large' },
  43. { value: 'middle', label: 'middle' },
  44. { value: 'small', label: 'small' },
  45. { value: 'default', label: 'defualt' },
  46. ];
  47. const layoutList = [
  48. { value: 'vertical', label: 'vertical' },
  49. { value: 'inline', label: 'inline' },
  50. { value: 'horizontal', label: 'horizontal' },
  51. ];
  52. const labelAlignList = [
  53. { value: 'left', label: 'left' },
  54. { value: 'right', label: 'right' },
  55. ];
  56. const schemas: FormSchema[] = [
  57. {
  58. field: 'field1',
  59. component: 'Input',
  60. label: '字段1',
  61. colProps: { span: 8 },
  62. componentProps: {
  63. placeholder: '自定义placeholder',
  64. onChange: (e: any) => {
  65. console.log(e);
  66. },
  67. },
  68. },
  69. {
  70. field: 'field2',
  71. component: 'Input',
  72. label: '字段2',
  73. colProps: { span: 8 },
  74. },
  75. {
  76. field: 'field3',
  77. component: 'DatePicker',
  78. label: '字段3',
  79. colProps: { span: 8 },
  80. componentProps: {
  81. getPopupContainer: () => {
  82. return document.querySelector('.ant-form');
  83. },
  84. },
  85. },
  86. {
  87. field: 'fieldTime',
  88. component: 'RangePicker',
  89. label: '时间字段',
  90. colProps: { span: 8 },
  91. componentProps: {
  92. getPopupContainer: () => {
  93. return document.querySelector('.ant-form');
  94. },
  95. },
  96. },
  97. {
  98. field: 'field4',
  99. component: 'Select',
  100. label: '字段4',
  101. colProps: { span: 8 },
  102. componentProps: {
  103. options: [
  104. { label: '选项1', value: '1', key: '1' },
  105. { label: '选项2', value: '2', key: '2' },
  106. ],
  107. },
  108. },
  109. {
  110. field: 'field5',
  111. component: 'CheckboxGroup',
  112. label: '字段5',
  113. colProps: {
  114. span: 8,
  115. },
  116. componentProps: {
  117. options: [
  118. { label: '选项1', value: '1' },
  119. { label: '选项2', value: '2' },
  120. ],
  121. },
  122. },
  123. {
  124. field: 'field7',
  125. component: 'RadioGroup',
  126. label: '字段7',
  127. colProps: { span: 8 },
  128. componentProps: {
  129. options: [
  130. { label: '选项1', value: '1' },
  131. { label: '选项2', value: '2' },
  132. ],
  133. },
  134. },
  135. {
  136. field: 'field8',
  137. component: 'ApiCascader',
  138. label: '联动',
  139. colProps: { span: 8 },
  140. componentProps: {
  141. api: areaRecord,
  142. apiParamKey: 'parentCode',
  143. dataField: 'data',
  144. labelField: 'name',
  145. valueField: 'code',
  146. initFetchParams: {
  147. parentCode: '',
  148. },
  149. isLeaf: (record) => {
  150. return !(record.levelType < 3);
  151. },
  152. },
  153. },
  154. {
  155. field: 'field9',
  156. component: 'ApiCascader',
  157. label: '联动回显',
  158. colProps: { span: 8 },
  159. componentProps: {
  160. api: areaRecord,
  161. apiParamKey: 'parentCode',
  162. dataField: 'data',
  163. labelField: 'name',
  164. valueField: 'code',
  165. initFetchParams: {
  166. parentCode: '',
  167. },
  168. isLeaf: (record) => {
  169. return !(record.levelType < 3);
  170. },
  171. },
  172. },
  173. ];
  174. const formSchemas: FormSchema[] = [
  175. {
  176. field: 'd1',
  177. component: 'Divider',
  178. label: '基础属性',
  179. colProps: { span: 24 },
  180. componentProps: {
  181. orientation: 'center',
  182. },
  183. },
  184. {
  185. field: 'name',
  186. defaultValue: 'useForm',
  187. component: 'Input',
  188. label: 'name',
  189. colProps: { span: 24 },
  190. },
  191. {
  192. field: 'layout',
  193. defaultValue: 'horizontal',
  194. component: 'RadioButtonGroup',
  195. label: 'layout',
  196. colProps: { span: 24 },
  197. componentProps: {
  198. options: layoutList,
  199. },
  200. },
  201. {
  202. field: 'labelAlign',
  203. defaultValue: 'right',
  204. component: 'RadioButtonGroup',
  205. label: 'labelAlign',
  206. colProps: { span: 24 },
  207. componentProps: {
  208. options: labelAlignList,
  209. },
  210. },
  211. {
  212. field: 'labelWidth',
  213. defaultValue: 120,
  214. component: 'InputNumber',
  215. label: 'labelWidth',
  216. colProps: { span: 24 },
  217. },
  218. {
  219. field: 'size',
  220. defaultValue: 'default',
  221. component: 'Select',
  222. label: 'size',
  223. colProps: { span: 24 },
  224. componentProps: {
  225. options: sizeList,
  226. },
  227. },
  228. {
  229. field: 'colon',
  230. defaultValue: false,
  231. component: 'Switch',
  232. label: 'colon',
  233. colProps: { span: 24 },
  234. },
  235. {
  236. field: 'disabled',
  237. defaultValue: false,
  238. component: 'Switch',
  239. label: 'disabled',
  240. colProps: { span: 24 },
  241. },
  242. {
  243. field: 'compact',
  244. defaultValue: false,
  245. component: 'Switch',
  246. label: 'compact',
  247. colProps: { span: 24 },
  248. },
  249. {
  250. field: 'autoSetPlaceHolder',
  251. defaultValue: true,
  252. component: 'Switch',
  253. label: 'autoSetPlaceHolder',
  254. colProps: { span: 24 },
  255. },
  256. {
  257. field: 'autoSubmitOnEnter',
  258. defaultValue: false,
  259. component: 'Switch',
  260. label: 'autoSubmitOnEnter',
  261. colProps: { span: 24 },
  262. },
  263. {
  264. field: 'showAdvancedButton',
  265. defaultValue: false,
  266. component: 'Switch',
  267. label: 'showAdvancedButton',
  268. colProps: { span: 24 },
  269. },
  270. {
  271. field: 'd2',
  272. component: 'Divider',
  273. label: '网格布局(rowProps)',
  274. colProps: { span: 24 },
  275. componentProps: {
  276. orientation: 'center',
  277. },
  278. },
  279. {
  280. field: 'rowProps.gutter.0',
  281. component: 'InputNumber',
  282. defaultValue: 0,
  283. label: 'Horizontal Gutter',
  284. colProps: { span: 24 },
  285. componentProps: {
  286. addonAfter: 'px',
  287. },
  288. },
  289. {
  290. field: 'rowProps.gutter.1',
  291. component: 'InputNumber',
  292. defaultValue: 0,
  293. label: 'Vertical Gutter',
  294. colProps: { span: 24 },
  295. componentProps: {
  296. addonAfter: 'px',
  297. },
  298. },
  299. {
  300. field: 'rowProps.align',
  301. defaultValue: 'top',
  302. component: 'Select',
  303. label: 'align',
  304. colProps: { span: 24 },
  305. componentProps: {
  306. options: [
  307. { value: 'stretch', label: 'stretch' },
  308. { value: 'bottom', label: 'bottom' },
  309. { value: 'top', label: 'top' },
  310. { value: 'middle', label: 'middle' },
  311. ],
  312. },
  313. },
  314. {
  315. field: 'rowProps.justify',
  316. defaultValue: 'start',
  317. component: 'Select',
  318. label: 'justify',
  319. colProps: { span: 24 },
  320. componentProps: {
  321. options: [
  322. { value: 'space-around', label: 'space-around' },
  323. { value: 'space-between', label: 'space-between' },
  324. { value: 'center', label: 'center' },
  325. { value: 'end', label: 'end' },
  326. { value: 'start', label: 'start' },
  327. ],
  328. },
  329. },
  330. {
  331. field: 'wrap',
  332. defaultValue: true,
  333. component: 'Switch',
  334. label: 'wrap',
  335. colProps: { span: 24 },
  336. },
  337. {
  338. field: 'd3',
  339. component: 'Divider',
  340. label: '操作按钮',
  341. colProps: { span: 24 },
  342. componentProps: {
  343. orientation: 'center',
  344. },
  345. },
  346. {
  347. field: 'showActionButtonGroup',
  348. defaultValue: true,
  349. component: 'Switch',
  350. label: 'showActionButtonGroup',
  351. colProps: { span: 24 },
  352. componentProps: ({ formActionType }) => {
  353. return {
  354. onChange: async (val: boolean) => {
  355. formActionType.updateSchema([
  356. { field: 'showResetButton', componentProps: { disabled: !val } },
  357. {
  358. field: 'showSubmitButton',
  359. componentProps: { disabled: !val },
  360. },
  361. {
  362. field: 'actionColOptions.span',
  363. componentProps: { disabled: !val },
  364. },
  365. ]);
  366. },
  367. };
  368. },
  369. },
  370. {
  371. field: 'showResetButton',
  372. defaultValue: true,
  373. component: 'Switch',
  374. label: 'showResetButton',
  375. colProps: { span: 24 },
  376. },
  377. {
  378. field: 'showSubmitButton',
  379. defaultValue: true,
  380. component: 'Switch',
  381. label: 'showSubmitButton',
  382. colProps: { span: 24 },
  383. },
  384. {
  385. field: 'd4',
  386. component: 'Divider',
  387. label: '操作按钮网格布局(actionColOptions)',
  388. colProps: { span: 24 },
  389. componentProps: {
  390. orientation: 'center',
  391. },
  392. },
  393. {
  394. field: 'actionColOptions.span',
  395. component: 'Slider',
  396. defaultValue: 24,
  397. label: 'span',
  398. colProps: { span: 24 },
  399. componentProps: { min: 0, max: 24 },
  400. },
  401. {
  402. field: 'd5',
  403. component: 'Divider',
  404. label: '其他事件',
  405. colProps: { span: 24 },
  406. componentProps: {
  407. orientation: 'center',
  408. },
  409. },
  410. {
  411. field: 'other',
  412. component: 'Input',
  413. label: '',
  414. colProps: { span: 24 },
  415. colSlot: 'other',
  416. },
  417. ];
  418. export default defineComponent({
  419. components: {
  420. BasicForm,
  421. CollapseContainer,
  422. PageWrapper,
  423. Drawer,
  424. Space,
  425. },
  426. setup() {
  427. const visible = ref<boolean>(false);
  428. const settingFormRef = ref();
  429. const [registerSetting] = useForm({
  430. size: 'small',
  431. schemas: formSchemas,
  432. compact: true,
  433. actionColOptions: { span: 24 },
  434. showActionButtonGroup: false,
  435. });
  436. const resetSettings = async () => {
  437. setProps({ resetButtonOptions: { disabled: false, text: '重置' } });
  438. setProps({ submitButtonOptions: { disabled: false, loading: false } });
  439. await setFieldsValue({ field9: [] });
  440. await settingFormRef.value?.resetFields();
  441. };
  442. const handleSubmitSetting = async (values) => {
  443. console.log(values);
  444. await setProps(values);
  445. visible.value = false;
  446. };
  447. const [register, { setProps, setFieldsValue, updateSchema }] = useForm({
  448. labelWidth: 120,
  449. schemas,
  450. actionColOptions: { span: 24 },
  451. fieldMapToTime: [['fieldTime', ['startTime', 'endTime'], 'YYYY-MM']],
  452. });
  453. async function handleLoad() {
  454. const promiseFn = function () {
  455. return new Promise((resolve) => {
  456. setTimeout(() => {
  457. resolve({
  458. field9: ['430000', '430100', '430102'],
  459. province: '湖南省',
  460. city: '长沙市',
  461. district: '岳麓区',
  462. });
  463. }, 1000);
  464. });
  465. };
  466. const item = await promiseFn();
  467. const { field9, province, city, district } = item as any;
  468. await updateSchema({
  469. field: 'field9',
  470. componentProps: {
  471. displayRenderArray: [province, city, district],
  472. },
  473. });
  474. await setFieldsValue({ field9 });
  475. visible.value = false;
  476. }
  477. const showDrawer = () => {
  478. visible.value = true;
  479. };
  480. const onSettings = () => {
  481. settingFormRef.value?.submit();
  482. };
  483. const withClose = (formProps: Partial<FormProps>) => {
  484. setProps(formProps);
  485. visible.value = false;
  486. };
  487. return {
  488. register,
  489. schemas,
  490. handleSubmit: (values) => {
  491. console.log(values);
  492. },
  493. setProps,
  494. handleLoad,
  495. visible,
  496. showDrawer,
  497. settingFormRef,
  498. withClose,
  499. onSettings,
  500. resetSettings,
  501. registerSetting,
  502. handleSubmitSetting,
  503. };
  504. },
  505. });
  506. </script>