DeviceMaterialRelationModal.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. @register="registerModal"
  5. destroyOnClose
  6. :title="title"
  7. :bodyStyle="{
  8. padding: '20px 60px 0 0',
  9. }"
  10. :width="900"
  11. @ok="handleSubmit"
  12. >
  13. <BasicForm @register="registerForm" name="DeviceMaterialRelationForm" />
  14. </BasicModal>
  15. </template>
  16. <script lang="ts" setup>
  17. import { ref, computed, unref } from 'vue';
  18. import { BasicModal, useModalInner } from '/@/components/Modal';
  19. import { BasicForm, useForm } from '/@/components/Form/index';
  20. import { formSchema } from '../DeviceMaterialRelation.data';
  21. import { saveOrUpdate } from '../DeviceMaterialRelation.api';
  22. import { CustomScrollOptions } from '/@/views/equipmentLifecycle/common.types';
  23. // Emits声明
  24. const emit = defineEmits(['register', 'success']);
  25. const isUpdate = ref(true);
  26. const isDetail = ref(false);
  27. //表单配置
  28. const [registerForm, { setProps, resetFields, setFieldsValue, validate, scrollToField }] = useForm({
  29. labelWidth: 150,
  30. schemas: formSchema,
  31. showActionButtonGroup: false,
  32. baseColProps: { span: 24 },
  33. });
  34. //表单赋值
  35. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  36. //重置表单
  37. await resetFields();
  38. setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
  39. isUpdate.value = !!data?.isUpdate;
  40. isDetail.value = !!data?.showFooter;
  41. if (unref(isUpdate)) {
  42. //表单赋值
  43. await setFieldsValue({
  44. ...data.record,
  45. });
  46. }
  47. // 隐藏底部时禁用整个表单
  48. setProps({ disabled: !data?.showFooter });
  49. });
  50. //设置标题
  51. const title = computed(() => (!unref(isUpdate) ? '新增' : !unref(isDetail) ? '详情' : '编辑'));
  52. //表单提交事件
  53. async function handleSubmit() {
  54. try {
  55. let values = await validate();
  56. setModalProps({ confirmLoading: true });
  57. //提交表单
  58. await saveOrUpdate(values, isUpdate.value);
  59. //关闭弹窗
  60. closeModal();
  61. //刷新列表
  62. emit('success');
  63. } catch ({ errorFields }: any) {
  64. if (errorFields) {
  65. const firstField = errorFields[0];
  66. if (firstField) {
  67. scrollToField(firstField.name, { behavior: 'smooth', block: 'center' } as CustomScrollOptions);
  68. }
  69. }
  70. return Promise.reject(errorFields);
  71. } finally {
  72. setModalProps({ confirmLoading: false });
  73. }
  74. }
  75. </script>
  76. <style lang="less" scoped>
  77. /** 时间和数字输入框样式 */
  78. :deep(.ant-input-number) {
  79. width: 100%;
  80. }
  81. :deep(.ant-calendar-picker) {
  82. width: 100%;
  83. }
  84. </style>