195 lines
4.7 KiB
Vue
195 lines
4.7 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onBeforeUnmount, watch } from 'vue';
|
|
import { useI18n } from 'vue-i18n';
|
|
|
|
interface Props {
|
|
// 由父组件控制显隐,使用 v-model 统一管理弹框状态。
|
|
modelValue: boolean;
|
|
// title/message 支持外部按场景透传,不传时走 i18n 默认值。
|
|
title?: string;
|
|
message?: string;
|
|
confirmText?: string;
|
|
cancelText?: string;
|
|
confirmLoading?: boolean;
|
|
}
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
title: '',
|
|
message: '',
|
|
confirmText: '',
|
|
cancelText: '',
|
|
confirmLoading: false,
|
|
});
|
|
|
|
const emit = defineEmits<{
|
|
(e: 'update:modelValue', value: boolean): void;
|
|
(e: 'confirm'): void;
|
|
(e: 'cancel'): void;
|
|
}>();
|
|
|
|
const { t } = useI18n();
|
|
|
|
const resolvedTitle = computed(() => props.title || t('common.deleteDialog.defaultTitle'));
|
|
const resolvedMessage = computed(() => props.message || t('common.deleteDialog.defaultMessage'));
|
|
const resolvedCancelText = computed(() => props.cancelText || t('common.deleteDialog.cancel'));
|
|
const resolvedConfirmText = computed(() => props.confirmText || t('common.deleteDialog.confirm'));
|
|
|
|
const closeDialog = () => {
|
|
emit('update:modelValue', false);
|
|
};
|
|
|
|
const handleCancel = () => {
|
|
// 先抛出 cancel 事件,方便父组件做状态回滚,再关闭弹框。
|
|
emit('cancel');
|
|
closeDialog();
|
|
};
|
|
|
|
const handleConfirm = () => {
|
|
emit('confirm');
|
|
};
|
|
|
|
const handleEscKey = (event: KeyboardEvent) => {
|
|
if (event.key === 'Escape' && props.modelValue) {
|
|
handleCancel();
|
|
}
|
|
};
|
|
|
|
watch(
|
|
() => props.modelValue,
|
|
(visible) => {
|
|
// 关键点:弹框打开时监听 ESC,关闭后及时解绑,避免全局事件泄漏。
|
|
if (visible) {
|
|
document.addEventListener('keydown', handleEscKey);
|
|
return;
|
|
}
|
|
document.removeEventListener('keydown', handleEscKey);
|
|
},
|
|
{ immediate: true }
|
|
);
|
|
|
|
onBeforeUnmount(() => {
|
|
document.removeEventListener('keydown', handleEscKey);
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<Teleport to="body">
|
|
<Transition name="delete-dialog-fade">
|
|
<div
|
|
v-if="modelValue"
|
|
class="fixed inset-0 z-[100] bg-black/45 flex items-center justify-center p-4"
|
|
@click.self="handleCancel"
|
|
>
|
|
<div class="delete-dialog-container w-full max-w-[416px] rounded-lg bg-white shadow-[0_6px_16px_0_rgba(0,0,0,0.08),0_3px_6px_-4px_rgba(0,0,0,0.12),0_9px_28px_8px_rgba(0,0,0,0.05)] px-6 pt-7 pb-5">
|
|
<div class="flex items-start gap-3">
|
|
<span class="delete-dialog-icon" aria-hidden="true">!</span>
|
|
<div class="min-w-0">
|
|
<h3 class="text-[16px] leading-6 font-semibold text-[rgba(0,0,0,0.88)]">
|
|
{{ resolvedTitle }}
|
|
</h3>
|
|
<p class="mt-3 text-[14px] leading-[22px] text-[rgba(0,0,0,0.65)] whitespace-pre-line">
|
|
{{ resolvedMessage }}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mt-6 flex justify-end gap-2">
|
|
<button type="button" class="delete-dialog-btn cancel" @click="handleCancel">
|
|
{{ resolvedCancelText }}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
class="delete-dialog-btn confirm"
|
|
:disabled="confirmLoading"
|
|
@click="handleConfirm"
|
|
>
|
|
{{ resolvedConfirmText }}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</Transition>
|
|
</Teleport>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.delete-dialog-icon {
|
|
width: 22px;
|
|
height: 22px;
|
|
border-radius: 9999px;
|
|
background: #faad14;
|
|
color: #fff;
|
|
font-size: 14px;
|
|
line-height: 1;
|
|
font-weight: 600;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
flex-shrink: 0;
|
|
margin-top: 2px;
|
|
}
|
|
|
|
.delete-dialog-btn {
|
|
min-width: 66px;
|
|
height: 32px;
|
|
border-radius: 6px;
|
|
font-size: 14px;
|
|
line-height: 1;
|
|
font-weight: 400;
|
|
padding: 4px 15px;
|
|
transition: all 0.2s ease;
|
|
}
|
|
|
|
.delete-dialog-btn.cancel {
|
|
color: rgba(0, 0, 0, 0.88);
|
|
background: #fff;
|
|
border: 1px solid #d9d9d9;
|
|
}
|
|
|
|
.delete-dialog-btn.cancel:hover {
|
|
border-color: #4096ff;
|
|
color: #4096ff;
|
|
}
|
|
|
|
.delete-dialog-btn.confirm {
|
|
color: #fff;
|
|
background: #1677ff;
|
|
border: 1px solid #1677ff;
|
|
}
|
|
|
|
.delete-dialog-btn.confirm:hover {
|
|
background: #4096ff;
|
|
border-color: #4096ff;
|
|
}
|
|
|
|
.delete-dialog-btn:disabled {
|
|
opacity: 0.6;
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
.delete-dialog-fade-enter-active,
|
|
.delete-dialog-fade-leave-active {
|
|
transition: opacity 0.18s ease;
|
|
}
|
|
|
|
.delete-dialog-fade-enter-from,
|
|
.delete-dialog-fade-leave-to {
|
|
opacity: 0;
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.delete-dialog-container {
|
|
max-width: 94vw;
|
|
padding: 20px 16px 16px;
|
|
}
|
|
|
|
.delete-dialog-icon {
|
|
margin-top: 1px;
|
|
}
|
|
|
|
.delete-dialog-btn {
|
|
min-width: 62px;
|
|
}
|
|
}
|
|
</style>
|