Skip to content

Commit 84e317b

Browse files
authored
fix: Escape key should always close drop (#271)
- also resolve a Promise for open() method to know when it finished
1 parent ee56dfc commit 84e317b

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

packages/multiple-select-vanilla/src/MultipleSelectInstance.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -826,9 +826,8 @@ export class MultipleSelectInstance {
826826
}
827827

828828
this._bindEventService.bind(this.parentElm, 'keydown', ((e: KeyboardEvent) => {
829-
if (e.code === 'Escape' && !this.options.keepOpen) {
830-
this.close();
831-
this.choiceElm.focus();
829+
if (e.code === 'Escape') {
830+
this.handleEscapeKey();
832831
}
833832
}) as EventListener);
834833

@@ -1037,6 +1036,9 @@ export class MultipleSelectInstance {
10371036
e.preventDefault();
10381037
this.moveHighlightDown();
10391038
break;
1039+
case 'Escape':
1040+
this.handleEscapeKey();
1041+
break;
10401042
case 'Enter':
10411043
case ' ': {
10421044
// if we're focused on the OK button then don't execute following block
@@ -1073,6 +1075,7 @@ export class MultipleSelectInstance {
10731075
this.changeCurrentOptionHighlight();
10741076
this.okButtonElm?.focus();
10751077
}
1078+
break;
10761079
}
10771080
}
10781081
}) as EventListener,
@@ -1086,6 +1089,13 @@ export class MultipleSelectInstance {
10861089
}
10871090
}
10881091

1092+
protected handleEscapeKey() {
1093+
if (!this.options.keepOpen) {
1094+
this.close();
1095+
this.choiceElm.focus();
1096+
}
1097+
}
1098+
10891099
/**
10901100
* Checks if user reached the end of the list through mouse scrolling and/or arrow down,
10911101
* then scroll back to the top whenever that happens.
@@ -1118,14 +1128,20 @@ export class MultipleSelectInstance {
11181128
* The default delay is 0ms (which is 1 CPU cycle) when nothing is provided, to avoid a delay we can pass `-1` or `null`
11191129
* @param {number} [openDelay=0] - provide an optional delay, defaults to 0
11201130
*/
1121-
open(openDelay: number | null = 0) {
1122-
if (openDelay !== null && openDelay >= 0) {
1123-
// eslint-disable-next-line prefer-const
1124-
clearTimeout(this.openDelayTimer);
1125-
this.openDelayTimer = setTimeout(() => this.openDrop(), openDelay);
1126-
} else {
1127-
this.openDrop();
1128-
}
1131+
open(openDelay: number | null = 0): Promise<void> {
1132+
return new Promise(resolve => {
1133+
if (openDelay !== null && openDelay >= 0) {
1134+
// eslint-disable-next-line prefer-const
1135+
clearTimeout(this.openDelayTimer);
1136+
this.openDelayTimer = setTimeout(() => {
1137+
this.openDrop();
1138+
resolve();
1139+
}, openDelay);
1140+
} else {
1141+
this.openDrop();
1142+
resolve();
1143+
}
1144+
});
11291145
}
11301146

11311147
protected openDrop() {

0 commit comments

Comments
 (0)