Skip to content

Commit

Permalink
fix(core/select): replace mutation observer with dom events (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielleroux committed Jan 16, 2023
1 parent 2168cf9 commit c9ec1ff
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
5 changes: 5 additions & 0 deletions packages/core/component-doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6796,6 +6796,11 @@
"capture": false,
"passive": false
},
{
"event": "ix-select-item:labelChange",
"capture": false,
"passive": false
},
{
"event": "keydown",
"target": "window",
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/components/select-item/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
* SPDX-License-Identifier: MIT
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export class IxSelectItemLabelChangeEvent extends CustomEvent<{
oldValue: string;
newValue: string;
}> {
constructor(detail: { oldValue: string; newValue: string }) {
super('ix-select-item:labelChange', {
bubbles: true,
detail,
});
}
}
12 changes: 12 additions & 0 deletions packages/core/src/components/select-item/select-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
Host,
Method,
Prop,
Watch,
} from '@stencil/core';
import { IxSelectItemLabelChangeEvent } from './events';

@Component({
tag: 'ix-select-item',
Expand Down Expand Up @@ -69,6 +71,16 @@ export class SelectItem {
}
}

@Watch('label')
labelChange(newValue: string, oldValue: string) {
this.hostElement.dispatchEvent(
new IxSelectItemLabelChangeEvent({
newValue: newValue,
oldValue: oldValue,
})
);
}

render() {
return (
<Host>
Expand Down
23 changes: 10 additions & 13 deletions packages/core/src/components/select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
State,
Watch,
} from '@stencil/core';
import { IxSelectItemLabelChangeEvent } from '../select-item/events';

@Component({
tag: 'ix-select',
Expand Down Expand Up @@ -207,19 +208,15 @@ export class Select {
}
}

componentDidLoad() {
this.labelMutationObserver = new MutationObserver(() => {
this.selectValue(
Array.isArray(this.selectedIndices)
? this.selectedIndices
: [this.selectedIndices]
);
});
this.labelMutationObserver.observe(this.hostElement, {
subtree: true,
attributes: true,
attributeFilter: ['label'],
});
@Listen('ix-select-item:labelChange')
onLabelChange(event: IxSelectItemLabelChangeEvent) {
event.preventDefault();
event.stopImmediatePropagation();
this.selectValue(
Array.isArray(this.selectedIndices)
? this.selectedIndices
: [this.selectedIndices]
);
}

disconnectedCallback() {
Expand Down

0 comments on commit c9ec1ff

Please sign in to comment.