Skip to content

Commit b3ae32f

Browse files
authored
feat: add reason param to onClose(reason) (#273)
1 parent 8bba016 commit b3ae32f

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"test:e2e": "playwright test --config playwright/playwright.config.ts",
5454
"test:e2e:debug": "playwright test --config playwright/playwright.config.ts --ui --debug",
5555
"test:e2e:ui": "playwright test --config playwright/playwright.config.ts --ui",
56-
"test:report": "playwright show-report"
56+
"test:report": "playwright show-report",
57+
"playwright:install": "pnpm exec playwright install"
5758
},
5859
"packageManager": "[email protected]",
5960
"devDependencies": {

packages/demo/src/events/events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export default class Example {
1212
onOpen: () => {
1313
this.log('onOpen event fire!\n');
1414
},
15-
onClose: () => {
16-
this.log('onClose event fire!\n');
15+
onClose: reason => {
16+
this.log(`onClose event fire! Reason: "${reason}"\n`);
1717
},
1818
onCheckAll: () => {
1919
this.log('onCheckAll event fire!\n');

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import Constants from './constants';
55
import type { HtmlStruct, MultipleSelectLocales, OptGroupRowData, OptionDataObject, OptionRowData } from './interfaces';
6-
import type { MultipleSelectOption } from './interfaces/multipleSelectOption.interface';
6+
import type { CloseReason, MultipleSelectOption } from './interfaces/multipleSelectOption.interface';
77
import { BindingEventService, VirtualScroll } from './services';
88
import { compareObjects, deepCopy, findByParam, removeDiacritics, removeUndefined, setDataKeys, stripScripts } from './utils';
99
import {
@@ -267,7 +267,7 @@ export class MultipleSelectInstance {
267267
(e.target === this.dropElm || (findParent(e.target, '.ms-drop') !== this.dropElm && e.target !== this.elm)) &&
268268
this.options.isOpen
269269
) {
270-
this.close();
270+
this.close('body.click');
271271
}
272272
}) as EventListener,
273273
undefined,
@@ -372,7 +372,7 @@ export class MultipleSelectInstance {
372372

373373
if (this.options.openOnHover && this.parentElm) {
374374
this._bindEventService.bind(this.parentElm, 'mouseover', () => this.open(null));
375-
this._bindEventService.bind(this.parentElm, 'mouseout', () => this.close());
375+
this._bindEventService.bind(this.parentElm, 'mouseout', () => this.close('hover.mouseout'));
376376
}
377377
}
378378

@@ -801,7 +801,7 @@ export class MultipleSelectInstance {
801801
if (e.target.classList.contains('ms-icon-close')) {
802802
return;
803803
}
804-
this[this.options.isOpen ? 'close' : 'open']();
804+
this.options.isOpen ? this.close('toggle.close') : this.open();
805805
};
806806

807807
if (this.labelElm) {
@@ -864,7 +864,7 @@ export class MultipleSelectInstance {
864864
((e: KeyboardEvent) => {
865865
// Ensure shift-tab causes lost focus from filter as with clicking away
866866
if (e.code === 'Tab' && e.shiftKey) {
867-
this.close();
867+
this.close('key.shift+tab');
868868
}
869869
}) as EventListener,
870870
undefined,
@@ -891,7 +891,7 @@ export class MultipleSelectInstance {
891891
} else {
892892
this.selectAllElm?.click();
893893
}
894-
this.close();
894+
this.close(`key.${e.code.toLowerCase() as 'enter' | 'space'}`);
895895
this.focus();
896896
return;
897897
}
@@ -966,7 +966,7 @@ export class MultipleSelectInstance {
966966
const option = findByParam(this.data, '_key', selectElm.dataset.key);
967967
const close = () => {
968968
if (this.options.single && this.options.isOpen && !this.options.keepOpen) {
969-
this.close();
969+
this.close('selection');
970970
}
971971
};
972972

@@ -1068,7 +1068,7 @@ export class MultipleSelectInstance {
10681068
this.focusSelectAllOrList();
10691069
this.highlightCurrentOption();
10701070
} else {
1071-
this.close();
1071+
this.close('key.shift+tab');
10721072
this.choiceElm.focus();
10731073
}
10741074
} else {
@@ -1091,7 +1091,7 @@ export class MultipleSelectInstance {
10911091

10921092
protected handleEscapeKey() {
10931093
if (!this.options.keepOpen) {
1094-
this.close();
1094+
this.close('key.escape');
10951095
this.choiceElm.focus();
10961096
}
10971097
}
@@ -1339,7 +1339,7 @@ export class MultipleSelectInstance {
13391339
}
13401340
}
13411341

1342-
close() {
1342+
close(reason?: CloseReason) {
13431343
this.options.isOpen = false;
13441344
this.parentElm.classList.remove('ms-parent-open');
13451345
this.choiceElm?.querySelector('div.ms-icon-caret')?.classList.remove('open');
@@ -1351,7 +1351,7 @@ export class MultipleSelectInstance {
13511351
this.dropElm.style.top = 'auto';
13521352
this.dropElm.style.left = 'auto';
13531353
}
1354-
this.options.onClose();
1354+
this.options.onClose(reason);
13551355
}
13561356

13571357
/**

packages/multiple-select-vanilla/src/interfaces/multipleSelectOption.interface.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ export interface MultipleSelectView {
88
instance: any;
99
}
1010

11+
export type CloseReason =
12+
| 'body.click'
13+
| 'hover.mouseout'
14+
| 'key.shift+tab'
15+
| 'key.escape'
16+
| 'key.enter'
17+
| 'key.space'
18+
| 'selection'
19+
| 'toggle.close'
20+
| 'custom';
21+
1122
export type CSSStyleDeclarationReadonly =
1223
| 'length'
1324
| 'parentRule'
@@ -239,7 +250,7 @@ export interface MultipleSelectOption extends MultipleSelectLocale {
239250
onClear: () => void;
240251

241252
/** Fires when the dropdown list is close. */
242-
onClose: () => void;
253+
onClose: (reason?: CloseReason) => void;
243254

244255
/** Fires when all the options are checked by either clicking the `Select all` checkbox, or when the `checkall` method is programatically called. */
245256
onCheckAll: () => void;

playwright/e2e/events.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test.describe('Events Demo', () => {
1717
'onBlur event fire!',
1818
'onOpen event fire!',
1919
'onFocus event fire!',
20-
'onClose event fire!',
20+
'onClose event fire! Reason: "toggle.close"',
2121
].join('\n'),
2222
);
2323

@@ -31,7 +31,7 @@ test.describe('Events Demo', () => {
3131
'onBlur event fire!',
3232
'onOpen event fire!',
3333
'onFocus event fire!',
34-
'onClose event fire!',
34+
'onClose event fire! Reason: "toggle.close"',
3535
'onBlur event fire!',
3636
'onOpen event fire!',
3737
'onOptgroupClick event fire! view: {"label":"Group 1","selected":true,"children":[null,{"text":"Option 1","value":"1","selected":true,"disabled":false},null,{"text":"Option 2","value":"2","selected":true,"disabled":false},null,{"text":"Option 3","value":"3","selected":true,"disabled":false},null]}',
@@ -46,7 +46,7 @@ test.describe('Events Demo', () => {
4646
'onBlur event fire!',
4747
'onOpen event fire!',
4848
'onFocus event fire!',
49-
'onClose event fire!',
49+
'onClose event fire! Reason: "toggle.close"',
5050
'onBlur event fire!',
5151
'onOpen event fire!',
5252
'onOptgroupClick event fire! view: {"label":"Group 1","selected":true,"children":[null,{"text":"Option 1","value":"1","selected":true,"disabled":false},null,{"text":"Option 2","value":"2","selected":true,"disabled":false},null,{"text":"Option 3","value":"3","selected":true,"disabled":false},null]}',
@@ -62,7 +62,7 @@ test.describe('Events Demo', () => {
6262
'onBlur event fire!',
6363
'onOpen event fire!',
6464
'onFocus event fire!',
65-
'onClose event fire!',
65+
'onClose event fire! Reason: "toggle.close"',
6666
'onBlur event fire!',
6767
'onOpen event fire!',
6868
'onOptgroupClick event fire! view: {"label":"Group 1","selected":true,"children":[null,{"text":"Option 1","value":"1","selected":true,"disabled":false},null,{"text":"Option 2","value":"2","selected":true,"disabled":false},null,{"text":"Option 3","value":"3","selected":true,"disabled":false},null]}',
@@ -80,7 +80,7 @@ test.describe('Events Demo', () => {
8080
'onBlur event fire!',
8181
'onOpen event fire!',
8282
'onFocus event fire!',
83-
'onClose event fire!',
83+
'onClose event fire! Reason: "toggle.close"',
8484
'onBlur event fire!',
8585
'onOpen event fire!',
8686
'onOptgroupClick event fire! view: {"label":"Group 1","selected":true,"children":[null,{"text":"Option 1","value":"1","selected":true,"disabled":false},null,{"text":"Option 2","value":"2","selected":true,"disabled":false},null,{"text":"Option 3","value":"3","selected":true,"disabled":false},null]}',

0 commit comments

Comments
 (0)