Skip to content

Commit 3f473c3

Browse files
fix: use composedPath instead of target, fixes #278 (#279)
* fix: use `composedPath` instead of `target`
1 parent 2f1d438 commit 3f473c3

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,13 @@ export class MultipleSelectInstance {
260260
document.body,
261261
'click',
262262
((e: MouseEvent & { target: HTMLElement }) => {
263-
if (e.target === this.choiceElm || findParent(e.target, '.ms-choice') === this.choiceElm) {
263+
if (this.getEventTarget(e) === this.choiceElm || findParent(this.getEventTarget(e), '.ms-choice') === this.choiceElm) {
264264
return;
265265
}
266266

267267
if (
268-
(e.target === this.dropElm || (findParent(e.target, '.ms-drop') !== this.dropElm && e.target !== this.elm)) &&
268+
(this.getEventTarget(e) === this.dropElm ||
269+
(findParent(this.getEventTarget(e), '.ms-drop') !== this.dropElm && this.getEventTarget(e) !== this.elm)) &&
269270
this.options.isOpen
270271
) {
271272
this.close('body.click');
@@ -531,6 +532,13 @@ export class MultipleSelectInstance {
531532
return rows;
532533
}
533534

535+
protected getEventTarget(e: Event & { target: HTMLElement }): HTMLElement {
536+
if (e.composedPath) {
537+
return e.composedPath()[0] as HTMLElement;
538+
}
539+
return e.target as HTMLElement;
540+
}
541+
534542
protected getListRows(): HtmlStruct[] {
535543
const rows: HtmlStruct[] = [];
536544
this.updateData = [];
@@ -799,15 +807,15 @@ export class MultipleSelectInstance {
799807

800808
const toggleOpen = (e: MouseEvent & { target: HTMLElement }) => {
801809
e.preventDefault();
802-
if (e.target.classList.contains('ms-icon-close')) {
810+
if (this.getEventTarget(e).classList.contains('ms-icon-close')) {
803811
return;
804812
}
805813
this.options.isOpen ? this.close('toggle.close') : this.open();
806814
};
807815

808816
if (this.labelElm) {
809817
this._bindEventService.bind(this.labelElm, 'click', ((e: MouseEvent & { target: HTMLElement }) => {
810-
if (e.target.nodeName.toLowerCase() !== 'label') {
818+
if (this.getEventTarget(e).nodeName.toLowerCase() !== 'label') {
811819
return;
812820
}
813821
toggleOpen(e);
@@ -1008,7 +1016,7 @@ export class MultipleSelectInstance {
10081016
this.dropElm,
10091017
'mouseover',
10101018
((e: MouseEvent & { target: HTMLDivElement | HTMLLIElement }) => {
1011-
const liElm = (e.target.closest('.ms-select-all') || e.target.closest('li')) as HTMLLIElement;
1019+
const liElm = (this.getEventTarget(e).closest('.ms-select-all') || this.getEventTarget(e).closest('li')) as HTMLLIElement;
10121020

10131021
if (this.dropElm?.contains(liElm) && this.lastMouseOverPosition !== `${e.clientX}:${e.clientY}`) {
10141022
const optionElms = this.dropElm?.querySelectorAll<HTMLLIElement>(OPTIONS_LIST_SELECTOR) || [];
@@ -1048,7 +1056,7 @@ export class MultipleSelectInstance {
10481056
case ' ': {
10491057
// if we're focused on the OK button then don't execute following block
10501058
if (document.activeElement !== this.okButtonElm) {
1051-
const liElm = e.target.closest('.ms-select-all') || e.target.closest('li');
1059+
const liElm = this.getEventTarget(e).closest('.ms-select-all') || this.getEventTarget(e).closest('li');
10521060
if ((e.key === ' ' && this.options.filter) || (this.options.filterAcceptOnEnter && !liElm)) {
10531061
return;
10541062
}
@@ -1108,8 +1116,8 @@ export class MultipleSelectInstance {
11081116
protected infiniteScrollHandler(e: (MouseEvent & { target: HTMLElement }) | null, idx?: number, fullCount?: number) {
11091117
let needHighlightRecalc = false;
11101118

1111-
if (e?.target && this.ulElm && this.scrolledByMouse) {
1112-
const scrollPos = e.target.scrollTop + e.target.clientHeight;
1119+
if (e && this.getEventTarget(e) && this.ulElm && this.scrolledByMouse) {
1120+
const scrollPos = this.getEventTarget(e).scrollTop + this.getEventTarget(e).clientHeight;
11131121
if (scrollPos === this.ulElm.scrollHeight) {
11141122
needHighlightRecalc = true;
11151123
}

0 commit comments

Comments
 (0)