From 7af715f5153927e16e79dac606471c7373dbb83a Mon Sep 17 00:00:00 2001 From: Jeremias Peier Date: Tue, 24 Dec 2024 11:07:58 +0100 Subject: [PATCH] fix(sbb-select): respect text node mutations for displayed value --- .../option/option/option-base-element.ts | 18 +++++++++++------- src/elements/select/select.spec.ts | 13 +++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/elements/option/option/option-base-element.ts b/src/elements/option/option/option-base-element.ts index 59b5ea729b..5cc2a0098b 100644 --- a/src/elements/option/option/option-base-element.ts +++ b/src/elements/option/option/option-base-element.ts @@ -22,6 +22,10 @@ const inertAriaGroups = isSafari; /** Configuration for the attribute to look at if component is nested in an option group */ const optionObserverConfig: MutationObserverInit = { attributeFilter: ['data-group-disabled', 'data-negative'], + attributes: true, + childList: true, + subtree: true, + characterData: true, }; export @@ -193,6 +197,12 @@ abstract class SbbOptionBaseElement extends SbbDisabledMixin( this.updateAriaDisabled(); } else if (mutation.attributeName === 'data-negative') { this.negative = this.hasAttribute('data-negative'); + } else { + /** @internal */ + this.dispatchEvent(new Event('optionLabelChanged', { bubbles: true })); + + // We return because there should be only one event triggered per mutationList + return; } } } @@ -256,19 +266,13 @@ abstract class SbbOptionBaseElement extends SbbDisabledMixin( return nothing; } - private _handleSlotChange(): void { - this.handleHighlightState(); - /** @internal */ - this.dispatchEvent(new Event('optionLabelChanged', { bubbles: true })); - } - protected override render(): TemplateResult { return html`
${this.renderIcon()} - + ${this.renderLabel()} ${this._inertAriaGroups && this.getAttribute('data-group-label') ? html` diff --git a/src/elements/select/select.spec.ts b/src/elements/select/select.spec.ts index d6d727de6b..2e3603a902 100644 --- a/src/elements/select/select.spec.ts +++ b/src/elements/select/select.spec.ts @@ -485,6 +485,19 @@ describe(`sbb-select`, () => { expect(displayValue.textContent!.trim()).to.be.equal('First modified'); + // To test the updated value, we need to create a modifiable textNode + const textNode = document.createTextNode('Initial value'); + firstOption.innerHTML = ''; + firstOption.appendChild(textNode); + await waitForLitRender(element); + + textNode.data = 'First modified again'; + + await waitForLitRender(element); + displayValue = element.shadowRoot!.querySelector('.sbb-select__trigger')!; + + expect(displayValue.textContent!.trim()).to.be.equal('First modified again'); + // Deselection element.value = ''; await waitForLitRender(element);