DynamicForm.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <PageWrapper title="动态表单示例">
  3. <div class="mb-4">
  4. <a-button @click="changeLabel3" class="mr-2">更改字段3label</a-button>
  5. <a-button @click="changeLabel34" class="mr-2">同时更改字段3,4label</a-button>
  6. <a-button @click="appendField" class="mr-2">往字段3后面插入字段10</a-button>
  7. <a-button @click="deleteField" class="mr-2">删除字段11</a-button>
  8. </div>
  9. <CollapseContainer title="动态表单示例,动态根据表单内其他值改变">
  10. <BasicForm @register="register" />
  11. </CollapseContainer>
  12. <CollapseContainer class="mt-5" title="componentProps动态改变">
  13. <BasicForm @register="register1" />
  14. </CollapseContainer>
  15. </PageWrapper>
  16. </template>
  17. <script lang="ts">
  18. import { defineComponent } from 'vue';
  19. import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  20. import { CollapseContainer } from '/@/components/Container/index';
  21. import { PageWrapper } from '/@/components/Page';
  22. const schemas: FormSchema[] = [
  23. {
  24. field: 'field1',
  25. component: 'Input',
  26. label: '字段1',
  27. colProps: {
  28. span: 8,
  29. },
  30. show: ({ values }) => {
  31. return !!values.field5;
  32. },
  33. },
  34. {
  35. field: 'field2',
  36. component: 'Input',
  37. label: '字段2',
  38. colProps: {
  39. span: 8,
  40. },
  41. ifShow: ({ values }) => {
  42. return !!values.field6;
  43. },
  44. },
  45. {
  46. field: 'field3',
  47. component: 'DatePicker',
  48. label: '字段3',
  49. colProps: {
  50. span: 8,
  51. },
  52. dynamicDisabled: ({ values }) => {
  53. return !!values.field7;
  54. },
  55. },
  56. {
  57. field: 'field4',
  58. component: 'Select',
  59. label: '字段4',
  60. colProps: {
  61. span: 8,
  62. },
  63. dynamicRules: ({ values }) => {
  64. return values.field8 ? [{ required: true, message: '字段4必填' }] : [];
  65. },
  66. componentProps: {
  67. options: [
  68. {
  69. label: '选项1',
  70. value: '1',
  71. key: '1',
  72. },
  73. {
  74. label: '选项2',
  75. value: '2',
  76. key: '2',
  77. },
  78. ],
  79. },
  80. },
  81. {
  82. field: 'field11',
  83. component: 'DatePicker',
  84. label: '字段11',
  85. colProps: {
  86. span: 8,
  87. },
  88. },
  89. {
  90. field: 'field5',
  91. component: 'Switch',
  92. label: '是否显示字段1(css控制)',
  93. colProps: {
  94. span: 8,
  95. },
  96. labelWidth: 200,
  97. },
  98. {
  99. field: 'field6',
  100. component: 'Switch',
  101. label: '是否显示字段2(dom控制)',
  102. colProps: {
  103. span: 8,
  104. },
  105. labelWidth: 200,
  106. },
  107. {
  108. field: 'field7',
  109. component: 'Switch',
  110. label: '是否禁用字段3',
  111. colProps: {
  112. span: 8,
  113. },
  114. labelWidth: 200,
  115. },
  116. {
  117. field: 'field8',
  118. component: 'Switch',
  119. label: '字段4是否必填',
  120. colProps: {
  121. span: 8,
  122. },
  123. labelWidth: 200,
  124. },
  125. ];
  126. const schemas1: FormSchema[] = [
  127. {
  128. field: 'f1',
  129. component: 'Input',
  130. label: 'F1',
  131. colProps: {
  132. span: 12,
  133. },
  134. labelWidth: 200,
  135. componentProps: ({ formModel }) => {
  136. return {
  137. placeholder: '同步f2的值为f1',
  138. onChange: (e: ChangeEvent) => {
  139. formModel.f2 = e.target.value;
  140. },
  141. };
  142. },
  143. },
  144. {
  145. field: 'f2',
  146. component: 'Input',
  147. label: 'F2',
  148. colProps: {
  149. span: 12,
  150. },
  151. labelWidth: 200,
  152. componentProps: { disabled: true },
  153. },
  154. {
  155. field: 'f3',
  156. component: 'Input',
  157. label: 'F3',
  158. colProps: {
  159. span: 12,
  160. },
  161. labelWidth: 200,
  162. // @ts-ignore
  163. componentProps: ({ formActionType, tableAction }) => {
  164. return {
  165. placeholder: '值改变时执行查询,查看控制台',
  166. onChange: async () => {
  167. const { validate } = formActionType;
  168. // tableAction只适用于在表格内开启表单的例子
  169. // const { reload } = tableAction;
  170. const res = await validate();
  171. console.log(res);
  172. },
  173. };
  174. },
  175. },
  176. ];
  177. export default defineComponent({
  178. components: { BasicForm, CollapseContainer, PageWrapper },
  179. setup() {
  180. const [
  181. register,
  182. { setProps, updateSchema, appendSchemaByField, removeSchemaByFiled },
  183. ] = useForm({
  184. labelWidth: 120,
  185. schemas,
  186. actionColOptions: {
  187. span: 24,
  188. },
  189. });
  190. const [register1] = useForm({
  191. labelWidth: 120,
  192. schemas: schemas1,
  193. actionColOptions: {
  194. span: 24,
  195. },
  196. });
  197. function changeLabel3() {
  198. updateSchema({
  199. field: 'field3',
  200. label: '字段3 New',
  201. });
  202. }
  203. function changeLabel34() {
  204. updateSchema([
  205. {
  206. field: 'field3',
  207. label: '字段3 New++',
  208. },
  209. {
  210. field: 'field4',
  211. label: '字段4 New++',
  212. },
  213. ]);
  214. }
  215. function appendField() {
  216. appendSchemaByField(
  217. {
  218. field: 'field10',
  219. label: '字段10',
  220. component: 'Input',
  221. colProps: {
  222. span: 8,
  223. },
  224. },
  225. 'field3'
  226. );
  227. }
  228. function deleteField() {
  229. removeSchemaByFiled('field11');
  230. }
  231. return {
  232. register,
  233. register1,
  234. schemas,
  235. setProps,
  236. changeLabel3,
  237. changeLabel34,
  238. appendField,
  239. deleteField,
  240. };
  241. },
  242. });
  243. </script>