DemoModal.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="40%">
  3. <BasicForm @register="registerForm" />
  4. </BasicModal>
  5. </template>
  6. <script lang="ts" setup>
  7. import { ref, computed, unref } from 'vue';
  8. import { BasicModal, useModalInner } from '/@/components/Modal';
  9. import { BasicForm, useForm } from '/@/components/Form/index';
  10. import { formSchema } from './demo.data';
  11. import { saveOrUpdateDemo, getDemoById } from './demo.api';
  12. // 声明Emits
  13. const emit = defineEmits(['register', 'success']);
  14. const isUpdate = ref(true);
  15. //表单配置
  16. const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  17. //labelWidth: 150,
  18. schemas: formSchema,
  19. showActionButtonGroup: false,
  20. });
  21. //表单赋值
  22. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  23. //重置表单
  24. await resetFields();
  25. setModalProps({ confirmLoading: false });
  26. isUpdate.value = !!data?.isUpdate;
  27. if(data.createBy){
  28. await setFieldsValue({createBy: data.createBy})
  29. }
  30. if(data.createTime){
  31. await setFieldsValue({createTime: data.createTime})
  32. }
  33. if (unref(isUpdate)) {
  34. //获取详情
  35. data.record = await getDemoById({ id: data.record.id });
  36. //表单赋值
  37. await setFieldsValue({
  38. ...data.record,
  39. });
  40. }
  41. });
  42. //设置标题
  43. const title = computed(() => (!unref(isUpdate) ? '新增租户' : '编辑租户'));
  44. //表单提交事件
  45. async function handleSubmit(v) {
  46. try {
  47. let values = await validate();
  48. setModalProps({ confirmLoading: true });
  49. //提交表单
  50. await saveOrUpdateDemo(values, isUpdate.value);
  51. //关闭弹窗
  52. closeModal();
  53. //刷新列表
  54. emit('success', values);
  55. } finally {
  56. setModalProps({ confirmLoading: false });
  57. }
  58. }
  59. </script>