gaoluyang
昨天 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
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
<script lang="ts" setup>
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import { Form, FormItem, Input } from 'ant-design-vue';
 
defineOptions({ name: 'SignalMessageModal' });
 
const emit = defineEmits<{
  confirm: [data: { id: string; name: string }];
}>();
 
const formRef = ref();
const form = ref<{ id: string; name: string }>({ id: '', name: '' });
const modelType = ref<'message' | 'signal'>('message');
const isEdit = ref(false);
 
const config = computed(() => {
  return modelType.value === 'message'
    ? {
        title: isEdit.value ? '编辑消息' : '创建消息',
        idLabel: '消息 ID',
        nameLabel: '消息名称',
      }
    : {
        title: isEdit.value ? '编辑信号' : '创建信号',
        idLabel: '信号 ID',
        nameLabel: '信号名称',
      };
});
 
const [Modal, modalApi] = useVbenModal({
  onOpenChange(isOpen) {
    if (isOpen) {
      const data = modalApi.getData<{
        id?: string;
        isEdit?: boolean;
        name?: string;
        type: 'message' | 'signal';
      }>();
      modelType.value = data?.type || 'message';
      isEdit.value = data?.isEdit || false;
      form.value = {
        id: data?.id || '',
        name: data?.name || '',
      };
      // 清除校验
      setTimeout(() => {
        formRef.value?.clearValidate();
      }, 50);
    }
  },
  async onConfirm() {
    try {
      await formRef.value?.validate();
      emit('confirm', { ...form.value });
      modalApi.close();
    } catch {
      // 校验未通过
    }
  },
});
</script>
 
<template>
  <Modal :title="config.title" class="w-3/5">
    <Form
      ref="formRef"
      :model="form"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 18 }"
    >
      <FormItem
        :label="config.idLabel"
        name="id"
        :rules="[{ required: true, message: '请输入 ID' }]"
      >
        <Input v-model:value="form.id" allow-clear />
      </FormItem>
      <FormItem
        :label="config.nameLabel"
        name="name"
        :rules="[{ required: true, message: '请输入名称' }]"
      >
        <Input v-model:value="form.name" allow-clear />
      </FormItem>
    </Form>
  </Modal>
</template>