forked from halo-dev/halo
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Dialog.vue
137 lines (129 loc) · 2.99 KB
/
Dialog.vue
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<script lang="ts" setup>
import { VModal } from "../modal";
import { VButton } from "../button";
import {
IconCheckboxCircle,
IconClose,
IconErrorWarning,
IconForbidLine,
IconInformation,
} from "../../icons/icons";
import { computed, ref } from "vue";
import type { Type } from "@/components/dialog/interface";
import type { Type as ButtonType } from "@/components/button/interface";
const props = withDefaults(
defineProps<{
type?: Type;
title?: string;
description?: string;
confirmText?: string;
confirmType?: ButtonType;
showCancel?: boolean;
cancelText?: string;
visible?: boolean;
onConfirm?: () => void;
onCancel?: () => void;
}>(),
{
type: "info",
title: "提示",
description: "",
confirmText: "确定",
confirmType: "primary",
showCancel: true,
cancelText: "取消",
visible: false,
onConfirm: () => {
return;
},
onCancel: () => {
return;
},
}
);
const emit = defineEmits<{
(event: "update:visible", visible: boolean): void;
(event: "close"): void;
}>();
const icons = {
success: {
icon: IconCheckboxCircle,
color: "green",
},
info: {
icon: IconInformation,
color: "blue",
},
warning: {
icon: IconErrorWarning,
color: "orange",
},
error: {
icon: IconForbidLine,
color: "red",
},
};
const icon = computed(() => icons[props.type]);
const loading = ref(false);
const handleCancel = () => {
if (props.onCancel) {
props.onCancel();
}
handleClose();
};
const handleConfirm = async () => {
if (props.onConfirm) {
loading.value = true;
try {
await props.onConfirm();
} catch (error) {
console.error("Failed to execute onConfirm: ", error);
} finally {
loading.value = false;
}
}
handleClose();
};
const handleClose = () => {
loading.value = false;
emit("update:visible", false);
emit("close");
};
</script>
<template>
<VModal
:visible="visible"
:width="450"
:layer-closable="true"
@close="handleCancel()"
>
<div class="flex justify-between items-start py-2 mb-2">
<div class="flex flex-row items-center gap-3">
<component
:is="icon.icon"
:class="`text-${icon.color}-500`"
class="w-6 h-6"
></component>
<div class="text-base text-gray-900 font-bold">{{ title }}</div>
</div>
<div>
<IconClose class="cursor-pointer" @click="handleCancel" />
</div>
</div>
<div class="flex items-center gap-4">
<div class="flex-1 flex items-stretch">
<div class="text-sm text-gray-700 break-all">{{ description }}</div>
</div>
</div>
<template #footer>
<div class="flex flex-col sm:flex-row gap-[10px]">
<VButton :loading="loading" :type="confirmType" @click="handleConfirm">
{{ confirmText }}
</VButton>
<VButton v-if="showCancel" @click="handleCancel">
{{ cancelText }}
</VButton>
</div>
</template>
</VModal>
</template>