Blame view

Yi.Vben5.Vue3/apps/web-antd/src/views/demo/tree/tree-modal.vue 2.52 KB
515fceeb   “wangming”   框架初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  <script setup lang="ts">
  import { computed, ref } from 'vue';
  
  import { useVbenModal } from '@vben/common-ui';
  import { $t } from '@vben/locales';
  import { cloneDeep, listToTree } from '@vben/utils';
  
  import { useVbenForm } from '#/adapter/form';
  
  import { treeAdd, treeInfo, treeList, treeUpdate } from './api';
  import { modalSchema } from './data';
  
  const emit = defineEmits<{ reload: [] }>();
  
  const isUpdate = ref(false);
  const title = computed(() => {
    return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
  });
  
  const [BasicForm, formApi] = useVbenForm({
    commonConfig: {
      // 默认占满两列
      formItemClass: 'col-span-2',
      // 默认label宽度 px
      labelWidth: 80,
      // 通用配置项 会影响到所有表单项
      componentProps: {
        class: 'w-full',
      },
    },
    schema: modalSchema(),
    showDefaultActions: false,
    wrapperClass: 'grid-cols-2',
  });
  
  async function setupTreeSelect() {
    const listData = await treeList();
    const treeData = listToTree(listData, { id: 'id', pid: 'parentId' });
    formApi.updateSchema([
      {
        fieldName: 'parentId',
        componentProps: {
          treeData,
          treeLine: { showLeafIcon: false },
          fieldNames: { label: 'treeName', value: 'id' },
          treeDefaultExpandAll: true,
        },
      },
    ]);
  }
  
  const [BasicModal, modalApi] = useVbenModal({
    fullscreenButton: false,
    onCancel: handleCancel,
    onConfirm: handleConfirm,
    onOpenChange: async (isOpen) => {
      if (!isOpen) {
        return null;
      }
      modalApi.modalLoading(true);
  
      const { id } = modalApi.getData() as { id?: number | string };
      isUpdate.value = !!id;
  
      if (isUpdate.value && id) {
        const record = await treeInfo(id);
        await formApi.setValues(record);
      }
      await setupTreeSelect();
  
      modalApi.modalLoading(false);
    },
  });
  
  async function handleConfirm() {
    try {
      modalApi.modalLoading(true);
      const { valid } = await formApi.validate();
      if (!valid) {
        return;
      }
      // getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
      const data = cloneDeep(await formApi.getValues());
      await (isUpdate.value ? treeUpdate(data) : treeAdd(data));
      emit('reload');
      await handleCancel();
    } catch (error) {
      console.error(error);
    } finally {
      modalApi.modalLoading(false);
    }
  }
  
  async function handleCancel() {
    modalApi.close();
    await formApi.resetForm();
  }
  </script>
  
  <template>
    <BasicModal :close-on-click-modal="false" :title="title" class="w-[550px]">
      <BasicForm />
    </BasicModal>
  </template>