miaopanel
1<script setup lang="ts">
2import { computed, onMounted, onUnmounted, ref } from 'vue'
3
4export interface ConfirmOption {
5 value: string
6 label: string
7 description?: string
8 danger?: boolean
9}
10
11const props = withDefaults(
12 defineProps<{
13 title: string
14 message?: string
15 confirmText?: string
16 cancelText?: string
17 danger?: boolean
18 options?: ConfirmOption[]
19 requireText?: string
20 /** Per-option confirmation phrase, e.g. typing the site name for a hard delete. */
21 requireTextMap?: Record<string, string>
22 busy?: boolean
23 error?: string
24 }>(),
25 {
26 message: '',
27 confirmText: '确认',
28 cancelText: '取消',
29 danger: false,
30 options: () => [],
31 requireText: '',
32 requireTextMap: () => ({}),
33 busy: false,
34 error: '',
35 },
36)
37
38const emit = defineEmits<{
39 (e: 'close'): void
40 (e: 'confirm', payload: { option: string; text: string }): void
41}>()
42
43const selected = ref(props.options[0]?.value ?? '')
44const typed = ref('')
45
46const requiredPhrase = computed(
47 () => props.requireTextMap[selected.value] ?? props.requireText,
48)
49const typedOk = computed(
50 () => !requiredPhrase.value || typed.value.trim() === requiredPhrase.value,
51)
52const canConfirm = computed(() => !props.busy && typedOk.value)
53
54function close() {
55 if (!props.busy) emit('close')
56}
57
58function confirm() {
59 if (!canConfirm.value) return
60 emit('confirm', { option: selected.value, text: typed.value.trim() })
61}
62
63function onKey(e: KeyboardEvent) {
64 if (e.key === 'Escape') close()
65 if (e.key === 'Enter' && canConfirm.value && !(e.target instanceof HTMLTextAreaElement)) confirm()
66}
67
68onMounted(() => window.addEventListener('keydown', onKey))
69onUnmounted(() => window.removeEventListener('keydown', onKey))
70</script>
71
72<template>
73 <div class="overlay" @click.self="close">
74 <div class="modal dialog" role="dialog" aria-modal="true">
75 <div class="modal-head">
76 <h3>{{ title }}</h3>
77 <button class="icon-btn" :disabled="busy" title="关闭" @click="close">×</button>
78 </div>
79
80 <p v-if="message" class="dialog-message">{{ message }}</p>
81
82 <div v-if="options.length" class="dialog-options">
83 <label v-for="opt in options" :key="opt.value" class="option" :class="{ danger: opt.danger }">
84 <input v-model="selected" type="radio" :value="opt.value" :disabled="busy" />
85 <span>
86 <b>{{ opt.label }}</b>
87 <small v-if="opt.description">{{ opt.description }}</small>
88 </span>
89 </label>
90 </div>
91
92 <label v-if="requiredPhrase" class="field dialog-typed">
93 请输入 <b class="mono">{{ requiredPhrase }}</b> 以确认
94 <input v-model="typed" class="input mono" :disabled="busy" autocomplete="off" />
95 </label>
96
97 <div v-if="error" class="alert danger">{{ error }}</div>
98
99 <div class="modal-foot">
100 <div class="spacer"></div>
101 <button class="btn btn-ghost" :disabled="busy" @click="close">{{ cancelText }}</button>
102 <button
103 class="btn"
104 :class="danger ? 'btn-danger' : 'btn-primary'"
105 :disabled="!canConfirm"
106 @click="confirm"
107 >
108 <span v-if="busy" class="spin"></span>
109 {{ busy ? '处理中…' : confirmText }}
110 </button>
111 </div>
112 </div>
113 </div>
114</template>
115
116<style scoped>
117.dialog {
118 width: min(520px, 100%);
119}
120.dialog-message {
121 margin: 0 0 12px;
122 color: #334e55;
123 line-height: 1.7;
124 white-space: pre-wrap;
125}
126.dialog-options {
127 display: flex;
128 flex-direction: column;
129 gap: 8px;
130 margin-bottom: 12px;
131}
132.option {
133 display: flex;
134 gap: 10px;
135 align-items: flex-start;
136 padding: 10px 12px;
137 border: 1px solid var(--line);
138 border-radius: 10px;
139 cursor: pointer;
140}
141.option:hover {
142 border-color: rgba(52, 211, 153, 0.45);
143 background: #f7fcfb;
144}
145.option.danger {
146 border-color: rgba(239, 68, 68, 0.25);
147}
148.option span {
149 display: flex;
150 flex-direction: column;
151 gap: 2px;
152}
153.option small {
154 color: var(--muted);
155 font-size: 12px;
156}
157.option input {
158 margin-top: 3px;
159}
160.dialog-typed {
161 margin-bottom: 12px;
162}
163</style>