Skip to content

Commit

Permalink
fix: 弹窗组件如果初始默认显示, 按esc关不掉问题修复 (Tencent#2608)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhanjiachun committed Aug 31, 2023
1 parent e955a0e commit 2e56455
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ export default mixins(ActionMixin, getConfigReceiverMixins<Vue, DialogConfig>('d
this.clearStyleFunc();
}
// 多个dialog同时存在时使用esc关闭异常 (#1209)
this.storeUid(value);
this.$nextTick(() => {
this.storeUid(value);
});
this.addKeyboardEvent(value);
if (this.isModeLess && this.draggable) {
this.$nextTick(() => {
Expand Down Expand Up @@ -238,12 +240,12 @@ export default mixins(ActionMixin, getConfigReceiverMixins<Vue, DialogConfig>('d
destroySelf() {
this.$el.parentNode?.removeChild?.(this.$el);
},

// 多个dialog情况,若有些给了默认值true,出现ESC关闭不了弹窗问题解决
storeUid(flag: boolean) {
if (flag) {
stack.push(this.uid);
} else {
stack.pop();
stack.pop(this.uid);
}
},
addKeyboardEvent(status: boolean) {
Expand All @@ -256,7 +258,7 @@ export default mixins(ActionMixin, getConfigReceiverMixins<Vue, DialogConfig>('d
}
},
keyboardEvent(e: KeyboardEvent) {
if (e.code === 'Escape') {
if (e.code === 'Escape' && stack.top === this.uid) {
emitEvent<Parameters<TdDialogProps['onEscKeydown']>>(this, 'esc-keydown', { e });
// 根据 closeOnEscKeydown 判断按下ESC时是否触发close事件
if (this.closeOnEscKeydown ?? this.global.closeOnEscKeydown) {
Expand Down
6 changes: 4 additions & 2 deletions src/dialog/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const push = (value: number) => {
data.push(value);
};

const pop = () => {
data.pop();
const pop = (value: number) => {
if (data.length && data.includes(value)) {
data.pop();
}
};

const stack = {
Expand Down

0 comments on commit 2e56455

Please sign in to comment.