Blame view

Yi.Vben5.Vue3/playground/src/views/system/dept/modules/form.vue 1.86 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
  <script lang="ts" setup>
  import type { SystemDeptApi } from '#/api/system/dept';
  
  import { computed, ref } from 'vue';
  
  import { useVbenModal } from '@vben/common-ui';
  
  import { Button } from 'ant-design-vue';
  
  import { useVbenForm } from '#/adapter/form';
  import { createDept, updateDept } from '#/api/system/dept';
  import { $t } from '#/locales';
  
  import { useSchema } from '../data';
  
  const emit = defineEmits(['success']);
  const formData = ref<SystemDeptApi.SystemDept>();
  const getTitle = computed(() => {
    return formData.value?.id
      ? $t('ui.actionTitle.edit', [$t('system.dept.name')])
      : $t('ui.actionTitle.create', [$t('system.dept.name')]);
  });
  
  const [Form, formApi] = useVbenForm({
    layout: 'vertical',
    schema: useSchema(),
    showDefaultActions: false,
  });
  
  function resetForm() {
    formApi.resetForm();
    formApi.setValues(formData.value || {});
  }
  
  const [Modal, modalApi] = useVbenModal({
    async onConfirm() {
      const { valid } = await formApi.validate();
      if (valid) {
        modalApi.lock();
        const data = await formApi.getValues();
        try {
          await (formData.value?.id
            ? updateDept(formData.value.id, data)
            : createDept(data));
          modalApi.close();
          emit('success');
        } finally {
          modalApi.lock(false);
        }
      }
    },
    onOpenChange(isOpen) {
      if (isOpen) {
        const data = modalApi.getData<SystemDeptApi.SystemDept>();
        if (data) {
          if (data.pid === 0) {
            data.pid = undefined;
          }
          formData.value = data;
          formApi.setValues(formData.value);
        }
      }
    },
  });
  </script>
  
  <template>
    <Modal :title="getTitle">
      <Form class="mx-4" />
      <template #prepend-footer>
        <div class="flex-auto">
          <Button type="primary" danger @click="resetForm">
            {{ $t('common.reset') }}
          </Button>
        </div>
      </template>
    </Modal>
  </template>