form-modal-demo.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <script lang="ts" setup>
  2. import { useVbenModal } from '@vben/common-ui';
  3. import { message } from 'antdv-next';
  4. import { useVbenForm } from '#/adapter/form';
  5. defineOptions({
  6. name: 'FormModelDemo',
  7. });
  8. const [Form, formApi] = useVbenForm({
  9. handleSubmit: onSubmit,
  10. schema: [
  11. {
  12. component: 'Input',
  13. componentProps: {
  14. placeholder: '请输入',
  15. },
  16. fieldName: 'field1',
  17. label: '字段1',
  18. rules: 'required',
  19. },
  20. {
  21. component: 'Input',
  22. componentProps: {
  23. placeholder: '请输入',
  24. },
  25. fieldName: 'field2',
  26. label: '字段2',
  27. rules: 'required',
  28. },
  29. {
  30. component: 'Select',
  31. componentProps: {
  32. options: [
  33. { label: '选项1', value: '1' },
  34. { label: '选项2', value: '2' },
  35. ],
  36. placeholder: '请输入',
  37. },
  38. fieldName: 'field3',
  39. label: '字段3',
  40. rules: 'required',
  41. },
  42. ],
  43. showDefaultActions: false,
  44. });
  45. const [Modal, modalApi] = useVbenModal({
  46. fullscreenButton: false,
  47. onCancel() {
  48. modalApi.close();
  49. },
  50. onConfirm: async () => {
  51. await formApi.validateAndSubmitForm();
  52. // modalApi.close();
  53. },
  54. onOpenChange(isOpen: boolean) {
  55. if (isOpen) {
  56. const { values } = modalApi.getData<Record<string, any>>();
  57. if (values) {
  58. formApi.setValues(values);
  59. }
  60. }
  61. },
  62. title: '内嵌表单示例',
  63. });
  64. function onSubmit(values: Record<string, any>) {
  65. message.loading({
  66. content: '正在提交中...',
  67. duration: 0,
  68. key: 'is-form-submitting',
  69. });
  70. modalApi.lock();
  71. setTimeout(() => {
  72. modalApi.close();
  73. message.success({
  74. content: `提交成功:${JSON.stringify(values)}`,
  75. duration: 2,
  76. key: 'is-form-submitting',
  77. });
  78. }, 3000);
  79. }
  80. </script>
  81. <template>
  82. <Modal>
  83. <Form />
  84. </Modal>
  85. </template>