修复表单弹窗误关闭

This commit is contained in:
2026-07-01 11:17:52 +08:00
parent ac6008788b
commit 975cdb20cb
6 changed files with 132 additions and 10 deletions
+48 -4
View File
@@ -1,46 +1,84 @@
<template>
<Teleport to="body">
<Transition name="modal-fade">
<div v-if="modelValue" class="modal-mask" @click="close">
<div v-if="modelValue" class="modal-mask">
<section class="modal-panel" @click.stop @focusin.capture="scrollFocusedControlIntoView">
<div class="modal-panel__head">
<h2>{{ title }}</h2>
<button class="modal-panel__close" type="button" :disabled="closeDisabled" @click="close">×</button>
<button class="modal-panel__close" type="button" aria-label="关闭" :disabled="closeDisabled" @click="requestClose">
<X :size="18" aria-hidden="true" />
</button>
</div>
<slot />
<div v-if="$slots.footer" class="modal-panel__footer">
<slot name="footer" />
</div>
</section>
<div v-if="discardConfirmVisible" class="modal-confirm" @click.stop>
<section class="modal-confirm__panel" role="alertdialog" aria-modal="true" :aria-labelledby="confirmTitleId">
<h3 :id="confirmTitleId">{{ confirmTitle }}</h3>
<p>{{ confirmMessage }}</p>
<div class="modal-confirm__actions">
<button class="secondary-btn" type="button" @click="keepEditing">继续编辑</button>
<button class="danger-btn" type="button" @click="discardAndClose">不保存</button>
</div>
</section>
</div>
</div>
</Transition>
</Teleport>
</template>
<script setup lang="ts">
import { nextTick } from 'vue'
import { computed, nextTick, ref, watch } from 'vue'
import { X } from 'lucide-vue-next'
const props = defineProps<{
modelValue: boolean
title: string
closeDisabled?: boolean
hasUnsavedChanges?: boolean
unsavedConfirmTitle?: string
unsavedConfirmMessage?: string
}>()
const emit = defineEmits<{
'update:modelValue': [value: boolean]
}>()
const discardConfirmVisible = ref(false)
const confirmTitleId = `modal-confirm-title-${Math.random().toString(36).slice(2)}`
const confirmTitle = computed(() => props.unsavedConfirmTitle || '内容还没保存')
const confirmMessage = computed(() => props.unsavedConfirmMessage || '关闭后本次填写的内容不会保存,确定关闭吗?')
const close = (): void => {
discardConfirmVisible.value = false
emit('update:modelValue', false)
}
const requestClose = (): void => {
if (props.closeDisabled) {
return
}
emit('update:modelValue', false)
if (props.hasUnsavedChanges) {
discardConfirmVisible.value = true
return
}
close()
}
const isFormControl = (target: EventTarget | null): target is HTMLElement => {
return target instanceof HTMLElement && target.matches('input, textarea, select')
}
const keepEditing = (): void => {
discardConfirmVisible.value = false
}
const discardAndClose = (): void => {
close()
}
const scrollFocusedControlIntoView = async (event: FocusEvent): Promise<void> => {
if (!isFormControl(event.target)) {
return
@@ -55,4 +93,10 @@ const scrollFocusedControlIntoView = async (event: FocusEvent): Promise<void> =>
})
}, 260)
}
watch(() => props.modelValue, value => {
if (!value) {
discardConfirmVisible.value = false
}
})
</script>
+41
View File
@@ -516,6 +516,47 @@ a {
background: var(--card-bg);
}
.modal-confirm {
position: absolute;
inset: 0;
z-index: 2;
display: grid;
place-items: center;
padding: 20px;
background: rgba(38, 35, 31, 0.28);
}
.modal-confirm__panel {
width: min(100%, 340px);
padding: 18px;
border-radius: 16px;
background: var(--card-bg);
box-shadow: 0 18px 46px rgba(38, 35, 31, 0.18);
}
.modal-confirm__panel h3,
.modal-confirm__panel p {
margin: 0;
}
.modal-confirm__panel h3 {
color: var(--ink);
font-size: 17px;
}
.modal-confirm__panel p {
margin-top: 8px;
color: var(--muted);
line-height: 1.6;
}
.modal-confirm__actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 16px;
}
.toast-host {
position: fixed;
top: calc(16px + env(safe-area-inset-top));
@@ -31,7 +31,7 @@
</div>
</div>
<BaseModal v-model="productModalVisible" title="选择预约作品">
<BaseModal v-model="productModalVisible" title="选择预约作品" :has-unsaved-changes="manualProductChanged">
<div class="product-modal">
<input v-model.trim="productKeyword" class="search-input" placeholder="搜索作品" @input="filterProducts" />
<div class="list">
@@ -143,6 +143,14 @@ const timeSelectTip = ref('请先选择美甲师并填写时长')
const conflictTip = ref('')
const conflictLevel = ref<'free' | 'danger'>('free')
const loading = ref(false)
const manualProductChanged = computed(() => {
return Boolean(
form.manualProductTitle
|| form.manualProductPrice
|| form.manualProductTags
|| form.manualProductDescription
)
})
const selectedProductTitle = computed(() => {
if (selectedProduct.value) {
+3 -2
View File
@@ -21,7 +21,7 @@
<button class="fab" type="button" @click="openCreate">+</button>
<BaseModal v-model="createModalVisible" title="新增客户">
<BaseModal v-model="createModalVisible" title="新增客户" :has-unsaved-changes="createFormChanged">
<div class="modal-form">
<div class="field">
<label class="required">客户姓名</label>
@@ -40,7 +40,7 @@
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { computed, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import BaseModal from '@/components/BaseModal.vue'
import EmptyState from '@/components/EmptyState.vue'
@@ -58,6 +58,7 @@ const customers = ref<UserInfo[]>([])
const createModalVisible = ref(false)
const creating = ref(false)
const createForm = reactive({ nickname: '', phone: '' })
const createFormChanged = computed(() => Boolean(createForm.nickname || createForm.phone))
const loadCustomers = async (): Promise<void> => {
loading.value = true
+5 -2
View File
@@ -21,7 +21,7 @@
<button class="fab" type="button" @click="openCreate">+</button>
<BaseModal v-model="createModalVisible" title="新增员工">
<BaseModal v-model="createModalVisible" title="新增员工" :has-unsaved-changes="createFormChanged">
<div class="modal-form">
<div class="field"><label class="required">姓名</label><input v-model.trim="createForm.nickname" placeholder="请输入姓名" /></div>
<div class="field"><label class="required">手机号</label><input v-model.trim="createForm.phone" inputmode="numeric" placeholder="请输入手机号" /></div>
@@ -35,7 +35,7 @@
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { computed, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import BaseModal from '@/components/BaseModal.vue'
import EmptyState from '@/components/EmptyState.vue'
@@ -58,6 +58,9 @@ const createForm = reactive({
password: '',
wechatId: ''
})
const createFormChanged = computed(() => {
return Boolean(createForm.nickname || createForm.phone || createForm.account || createForm.password || createForm.wechatId)
})
const loadEmployees = async (): Promise<void> => {
loading.value = true
+26 -1
View File
@@ -24,7 +24,12 @@
</button>
</section>
<BaseModal v-model="editModalVisible" title="个人信息" :close-disabled="profileSaving || avatarUploading">
<BaseModal
v-model="editModalVisible"
title="个人信息"
:close-disabled="profileSaving || avatarUploading"
:has-unsaved-changes="profileFormChanged"
>
<div class="profile-avatar-row">
<img v-if="profileForm.avatar" class="profile-avatar profile-avatar--image" :src="profileForm.avatar" alt="头像" />
<div v-else class="profile-avatar">头像</div>
@@ -109,6 +114,13 @@ const profileForm = reactive({
wechatId: '',
signature: ''
})
const profileBaseline = reactive({
nickname: '',
avatar: '',
contactPhone: '',
wechatId: '',
signature: ''
})
const menus: ProfileMenu[] = [
{ title: '个人信息', action: 'editProfile' },
@@ -164,9 +176,22 @@ const openEditProfile = (): void => {
profileForm.contactPhone = profile.contactPhone || ''
profileForm.wechatId = profile.wechatId || ''
profileForm.signature = profile.signature || ''
profileBaseline.nickname = profileForm.nickname
profileBaseline.avatar = profileForm.avatar
profileBaseline.contactPhone = profileForm.contactPhone
profileBaseline.wechatId = profileForm.wechatId
profileBaseline.signature = profileForm.signature
editModalVisible.value = true
}
const profileFormChanged = computed(() => {
return profileForm.nickname !== profileBaseline.nickname
|| profileForm.avatar !== profileBaseline.avatar
|| profileForm.contactPhone !== profileBaseline.contactPhone
|| profileForm.wechatId !== profileBaseline.wechatId
|| profileForm.signature !== profileBaseline.signature
})
const go = async (item: ProfileMenu): Promise<void> => {
if (item.action === 'editProfile') {
openEditProfile()