scroll-to-error-test.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <script lang="ts" setup>
  2. import { ref } from 'vue';
  3. import { Page } from '@vben/common-ui';
  4. import { Button, Card, Switch } from 'ant-design-vue';
  5. import { useVbenForm } from '#/adapter/form';
  6. defineOptions({
  7. name: 'ScrollToErrorTest',
  8. });
  9. const scrollEnabled = ref(true);
  10. const [Form, formApi] = useVbenForm({
  11. scrollToFirstError: scrollEnabled.value,
  12. schema: [
  13. {
  14. component: 'Input',
  15. componentProps: {
  16. placeholder: '请输入用户名',
  17. },
  18. fieldName: 'username',
  19. label: '用户名',
  20. rules: 'required',
  21. },
  22. {
  23. component: 'Input',
  24. componentProps: {
  25. placeholder: '请输入邮箱',
  26. },
  27. fieldName: 'email',
  28. label: '邮箱',
  29. rules: 'required',
  30. },
  31. {
  32. component: 'Input',
  33. componentProps: {
  34. placeholder: '请输入手机号',
  35. },
  36. fieldName: 'phone',
  37. label: '手机号',
  38. rules: 'required',
  39. },
  40. {
  41. component: 'Input',
  42. componentProps: {
  43. placeholder: '请输入地址',
  44. },
  45. fieldName: 'address',
  46. label: '地址',
  47. rules: 'required',
  48. },
  49. {
  50. component: 'Input',
  51. componentProps: {
  52. placeholder: '请输入备注',
  53. },
  54. fieldName: 'remark',
  55. label: '备注',
  56. rules: 'required',
  57. },
  58. {
  59. component: 'Input',
  60. componentProps: {
  61. placeholder: '请输入公司名称',
  62. },
  63. fieldName: 'company',
  64. label: '公司名称',
  65. rules: 'required',
  66. },
  67. {
  68. component: 'Input',
  69. componentProps: {
  70. placeholder: '请输入职位',
  71. },
  72. fieldName: 'position',
  73. label: '职位',
  74. rules: 'required',
  75. },
  76. {
  77. component: 'Select',
  78. componentProps: {
  79. options: [
  80. { label: '男', value: 'male' },
  81. { label: '女', value: 'female' },
  82. ],
  83. placeholder: '请选择性别',
  84. },
  85. fieldName: 'gender',
  86. label: '性别',
  87. rules: 'selectRequired',
  88. },
  89. ],
  90. showDefaultActions: false,
  91. });
  92. // 测试 validateAndSubmitForm(验证并提交)
  93. async function testValidateAndSubmit() {
  94. await formApi.validateAndSubmitForm();
  95. }
  96. // 测试 validate(手动验证整个表单)
  97. async function testValidate() {
  98. await formApi.validate();
  99. }
  100. // 测试 validateField(验证单个字段)
  101. async function testValidateField() {
  102. await formApi.validateField('username');
  103. }
  104. // 切换滚动功能
  105. function toggleScrollToError() {
  106. formApi.setState({ scrollToFirstError: scrollEnabled.value });
  107. }
  108. // 填充部分数据测试
  109. async function fillPartialData() {
  110. await formApi.resetForm();
  111. await formApi.setFieldValue('username', '测试用户');
  112. await formApi.setFieldValue('email', 'test@example.com');
  113. }
  114. </script>
  115. <template>
  116. <Page
  117. description="测试表单验证失败时自动滚动到错误字段的功能"
  118. title="滚动到错误字段测试"
  119. >
  120. <Card title="功能测试">
  121. <template #extra>
  122. <div class="flex items-center gap-2">
  123. <Switch
  124. v-model:checked="scrollEnabled"
  125. @change="toggleScrollToError"
  126. />
  127. <span>启用滚动到错误字段</span>
  128. </div>
  129. </template>
  130. <div class="space-y-4">
  131. <div class="rounded bg-blue-50 p-4">
  132. <h3 class="mb-2 font-medium">测试说明:</h3>
  133. <ul class="list-inside list-disc space-y-1 text-sm">
  134. <li>所有验证方法在验证失败时都会自动滚动到第一个错误字段</li>
  135. <li>可以通过右上角的开关控制是否启用自动滚动功能</li>
  136. </ul>
  137. </div>
  138. <div class="rounded border p-4">
  139. <h4 class="mb-3 font-medium">验证方法测试:</h4>
  140. <div class="flex flex-wrap gap-2">
  141. <Button type="primary" @click="testValidateAndSubmit">
  142. 测试 validateAndSubmitForm()
  143. </Button>
  144. <Button @click="testValidate"> 测试 validate() </Button>
  145. <Button @click="testValidateField"> 测试 validateField() </Button>
  146. </div>
  147. <div class="mt-2 text-xs text-gray-500">
  148. <p>• validateAndSubmitForm(): 验证表单并提交</p>
  149. <p>• validate(): 手动验证整个表单</p>
  150. <p>• validateField(): 验证单个字段(这里测试用户名字段)</p>
  151. </div>
  152. </div>
  153. <div class="rounded border p-4">
  154. <h4 class="mb-3 font-medium">数据填充测试:</h4>
  155. <div class="flex flex-wrap gap-2">
  156. <Button @click="fillPartialData"> 填充部分数据 </Button>
  157. <Button @click="() => formApi.resetForm()"> 清空表单 </Button>
  158. </div>
  159. <div class="mt-2 text-xs text-gray-500">
  160. <p>• 填充部分数据后验证,会滚动到第一个错误字段</p>
  161. </div>
  162. </div>
  163. <Form />
  164. </div>
  165. </Card>
  166. </Page>
  167. </template>