修复表单弹窗误关闭
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user