merge.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <script lang="ts" setup>
  2. import type { StepItem } from 'antdv-next';
  3. import { ref } from 'vue';
  4. import { Page } from '@vben/common-ui';
  5. import { Button, Card, message, Steps, Switch } from 'antdv-next';
  6. import { useVbenForm } from '#/adapter/form';
  7. const currentTab = ref(0);
  8. function onFirstSubmit(values: Record<string, any>) {
  9. message.success({
  10. content: `form1 values: ${JSON.stringify(values)}`,
  11. });
  12. currentTab.value = 1;
  13. }
  14. function onSecondReset() {
  15. currentTab.value = 0;
  16. }
  17. function onSecondSubmit(values: Record<string, any>) {
  18. message.success({
  19. content: `form2 values: ${JSON.stringify(values)}`,
  20. });
  21. }
  22. const [FirstForm, firstFormApi] = useVbenForm({
  23. commonConfig: {
  24. componentProps: {
  25. class: 'w-full',
  26. },
  27. },
  28. handleSubmit: onFirstSubmit,
  29. layout: 'horizontal',
  30. resetButtonOptions: {
  31. show: false,
  32. },
  33. schema: [
  34. {
  35. component: 'Input',
  36. componentProps: {
  37. placeholder: '请输入',
  38. },
  39. fieldName: 'formFirst',
  40. label: '表单1字段',
  41. rules: 'required',
  42. },
  43. ],
  44. submitButtonOptions: {
  45. content: '下一步',
  46. },
  47. wrapperClass: 'grid-cols-1 md:grid-cols-1 lg:grid-cols-1',
  48. });
  49. const [SecondForm, secondFormApi] = useVbenForm({
  50. commonConfig: {
  51. componentProps: {
  52. class: 'w-full',
  53. },
  54. },
  55. handleReset: onSecondReset,
  56. handleSubmit: onSecondSubmit,
  57. layout: 'horizontal',
  58. resetButtonOptions: {
  59. content: '上一步',
  60. },
  61. schema: [
  62. {
  63. component: 'Input',
  64. componentProps: {
  65. placeholder: '请输入',
  66. },
  67. fieldName: 'formSecond',
  68. label: '表单2字段',
  69. rules: 'required',
  70. },
  71. ],
  72. wrapperClass: 'grid-cols-1 md:grid-cols-1 lg:grid-cols-1',
  73. });
  74. const stepsItems: StepItem[] = [{ title: '表单1' }, { title: '表单2' }];
  75. const needMerge = ref(true);
  76. async function handleMergeSubmit() {
  77. const values = await firstFormApi
  78. .merge(secondFormApi)
  79. .submitAllForm(needMerge.value);
  80. message.success({
  81. content: `merged form values: ${JSON.stringify(values)}`,
  82. });
  83. }
  84. </script>
  85. <template>
  86. <Page
  87. description="表单组件合并示例:在某些场景下,例如分步表单,需要合并多个表单并统一提交。默认情况下,使用 Object.assign 规则合并表单。如果需要特殊处理数据,可以传入 false。"
  88. title="表单组件"
  89. >
  90. <Card title="基础示例">
  91. <template #extra>
  92. <Switch
  93. v-model:checked="needMerge"
  94. checked-children="开启字段合并"
  95. class="mr-4"
  96. un-checked-children="关闭字段合并"
  97. />
  98. <Button type="primary" @click="handleMergeSubmit">合并提交</Button>
  99. </template>
  100. <div class="mx-auto max-w-lg">
  101. <Steps :current="currentTab" :items="stepsItems" class="steps" />
  102. <div class="p-20">
  103. <FirstForm v-show="currentTab === 0" />
  104. <SecondForm v-show="currentTab === 1" />
  105. </div>
  106. </div>
  107. </Card>
  108. </Page>
  109. </template>