index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <script lang="ts" setup>
  2. import { message } from 'ant-design-vue';
  3. import { useVbenForm } from '#/adapter/form';
  4. const [Form] = useVbenForm({
  5. // 提交函数
  6. handleSubmit: onSubmit,
  7. schema: [
  8. {
  9. component: 'Input',
  10. defaultValue: 'hidden value',
  11. dependencies: {
  12. show: false,
  13. // 随意一个字段改变时,都会触发
  14. triggerFields: ['field1Switch'],
  15. },
  16. fieldName: 'hiddenField',
  17. label: '隐藏字段',
  18. },
  19. {
  20. component: 'Switch',
  21. defaultValue: true,
  22. fieldName: 'field1Switch',
  23. help: '通过Dom控制销毁',
  24. label: '显示字段1',
  25. },
  26. {
  27. component: 'Switch',
  28. defaultValue: true,
  29. fieldName: 'field2Switch',
  30. help: '通过css控制隐藏',
  31. label: '显示字段2',
  32. },
  33. {
  34. component: 'Switch',
  35. fieldName: 'field3Switch',
  36. label: '禁用字段3',
  37. },
  38. {
  39. component: 'Switch',
  40. fieldName: 'field4Switch',
  41. label: '字段4必填',
  42. },
  43. {
  44. component: 'Input',
  45. dependencies: {
  46. if(values) {
  47. return !!values.field1Switch;
  48. },
  49. // 只有指定的字段改变时,才会触发
  50. triggerFields: ['field1Switch'],
  51. },
  52. // 字段名
  53. fieldName: 'field1',
  54. // 界面显示的label
  55. label: '字段1',
  56. },
  57. {
  58. component: 'Input',
  59. dependencies: {
  60. show(values) {
  61. return !!values.field2Switch;
  62. },
  63. triggerFields: ['field2Switch'],
  64. },
  65. fieldName: 'field2',
  66. label: '字段2',
  67. },
  68. {
  69. component: 'Input',
  70. dependencies: {
  71. disabled(values) {
  72. return !!values.field3Switch;
  73. },
  74. triggerFields: ['field3Switch'],
  75. },
  76. fieldName: 'field3',
  77. label: '字段3',
  78. },
  79. {
  80. component: 'Input',
  81. dependencies: {
  82. required(values) {
  83. return !!values.field4Switch;
  84. },
  85. triggerFields: ['field4Switch'],
  86. },
  87. fieldName: 'field4',
  88. label: '字段4',
  89. },
  90. {
  91. component: 'Input',
  92. dependencies: {
  93. rules(values) {
  94. if (values.field1 === '123') {
  95. return 'required';
  96. }
  97. return null;
  98. },
  99. triggerFields: ['field1'],
  100. },
  101. fieldName: 'field5',
  102. help: '当字段1的值为`123`时,必填',
  103. label: '动态rules',
  104. },
  105. {
  106. component: 'Select',
  107. componentProps: {
  108. allowClear: true,
  109. class: 'w-full',
  110. filterOption: true,
  111. options: [
  112. {
  113. label: '选项1',
  114. value: '1',
  115. },
  116. {
  117. label: '选项2',
  118. value: '2',
  119. },
  120. ],
  121. placeholder: '请选择',
  122. showSearch: true,
  123. },
  124. dependencies: {
  125. componentProps(values) {
  126. if (values.field2 === '123') {
  127. return {
  128. options: [
  129. {
  130. label: '选项1',
  131. value: '1',
  132. },
  133. {
  134. label: '选项2',
  135. value: '2',
  136. },
  137. {
  138. label: '选项3',
  139. value: '3',
  140. },
  141. ],
  142. };
  143. }
  144. return {};
  145. },
  146. triggerFields: ['field2'],
  147. },
  148. fieldName: 'field6',
  149. help: '当字段2的值为`123`时,更改下拉选项',
  150. label: '动态配置',
  151. },
  152. ],
  153. // 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
  154. wrapperClass: 'grid-cols-1 md:grid-cols-2',
  155. });
  156. function onSubmit(values: Record<string, any>) {
  157. message.success({
  158. content: `form values: ${JSON.stringify(values)}`,
  159. });
  160. }
  161. </script>
  162. <template>
  163. <Form />
  164. </template>