Skip to content

Commit

Permalink
fix(dialog): 修改键盘事件绑定对象
Browse files Browse the repository at this point in the history
  • Loading branch information
sinbadmaster committed Dec 11, 2023
1 parent 69f2288 commit 5980e46
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
32 changes: 20 additions & 12 deletions src/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export default defineComponent({
return !isFullScreen.value ? { width: GetCSSValue(props.width) } : {}; // width全屏模式不生效
});

const divRef = ref<HTMLDivElement>();
watch(
() => props.visible,
(value) => {
Expand All @@ -204,11 +205,14 @@ export default defineComponent({
}
// 清除鼠标焦点 避免entry事件多次触发(按钮弹出弹窗 不移除焦点 立即按Entry按键 会造成弹窗关闭再弹出)
(document.activeElement as HTMLElement)?.blur();
// 获取焦点到当前dialog上
nextTick(() => {
divRef.value?.focus?.();
});
} else {
clearStyleFunc();
}
storeUid(value);
addKeyboardEvent(value);
},
);

Expand All @@ -232,17 +236,11 @@ export default defineComponent({
}
};

const addKeyboardEvent = (status: boolean) => {
if (status) {
document.addEventListener('keydown', keyboardEvent);
props.confirmOnEnter && document.addEventListener('keydown', keyboardEnterEvent);
} else {
document.removeEventListener('keydown', keyboardEvent);
props.confirmOnEnter && document.removeEventListener('keydown', keyboardEnterEvent);
}
};
// 回车出发确认事件
const keyboardEnterEvent = (e: KeyboardEvent) => {
if (!props.confirmOnEnter) {
return;
}
const eventSrc = e.target as HTMLElement;
if (eventSrc.tagName.toLowerCase() === 'input') return; // 若是input触发 则不执行
const { code } = e;
Expand All @@ -251,6 +249,7 @@ export default defineComponent({
}
};
const keyboardEvent = (e: KeyboardEvent) => {
keyboardEnterEvent(e);
if (e.code === 'Escape' && stack.top === instance.uid) {
props.onEscKeydown?.({ e });
// 根据closeOnEscKeydown判断按下ESC时是否触发close事件
Expand Down Expand Up @@ -419,7 +418,6 @@ export default defineComponent({
});

onBeforeUnmount(() => {
addKeyboardEvent(false);
destroySelf();
});

Expand All @@ -437,6 +435,8 @@ export default defineComponent({
hasEventOn,
renderDialog,
teleportElement,
keyboardEvent,
divRef,
};
},
render() {
Expand Down Expand Up @@ -465,7 +465,15 @@ export default defineComponent({
onAfterLeave={this.afterLeave}
>
{(!this.destroyOnClose || this.visible) && (
<div v-show={this.visible} class={ctxClass} style={ctxStyle} {...this.$attrs}>
<div
ref="divRef"
tabindex={0}
onKeydown={this.keyboardEvent}
v-show={this.visible}
class={ctxClass}
style={ctxStyle}
{...this.$attrs}
>
{view}
</div>
)}
Expand Down
19 changes: 11 additions & 8 deletions src/drawer/drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ export default defineComponent({
};
});

const handleEscKeydown = (e: KeyboardEvent) => {
if (props.closeOnEscKeydown ?? (globalConfig.value.closeOnEscKeydown && e.key === 'Escape')) {
props.onEscKeydown?.({ e });
closeDrawer({ trigger: 'esc', e });
}
};
// const handleEscKeydown = (e: KeyboardEvent) => {
// if (props.closeOnEscKeydown ?? (globalConfig.value.closeOnEscKeydown && e.key === 'Escape')) {
// props.onEscKeydown?.({ e });
// closeDrawer({ trigger: 'esc', e });
// }
// };

const clearStyleFunc = () => {
clearTimeout(styleTimer.value);
Expand Down Expand Up @@ -174,6 +174,9 @@ export default defineComponent({
if (!props.showInAttachedElement && props.preventScrollThrough) {
styleEl.value && document.head.appendChild(styleEl.value);
}
nextTick(() => {
drawerEle.value?.focus?.();
});
} else {
clearStyleFunc();
}
Expand Down Expand Up @@ -246,12 +249,12 @@ export default defineComponent({
document.head.appendChild(styleEl.value);
}

window.addEventListener('keydown', handleEscKeydown);
// window.addEventListener('keydown', handleEscKeydown);
});

onBeforeUnmount(() => {
clearStyleFunc();
window.removeEventListener('keydown', handleEscKeydown);
// window.removeEventListener('keydown', handleEscKeydown);
});

return () => {
Expand Down

0 comments on commit 5980e46

Please sign in to comment.