Skip to content

Commit

Permalink
fix(sbb-select): respect text node mutations for displayed value
Browse files Browse the repository at this point in the history
  • Loading branch information
jeripeierSBB committed Dec 24, 2024
1 parent 3752a57 commit 7af715f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/elements/option/option/option-base-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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`
<div class="sbb-option__container">
<div class="sbb-option">
${this.renderIcon()}
<span class="sbb-option__label">
<slot @slotchange=${this._handleSlotChange}></slot>
<slot @slotchange=${this.handleHighlightState}></slot>
${this.renderLabel()}
${this._inertAriaGroups && this.getAttribute('data-group-label')
? html` <sbb-screen-reader-only>
Expand Down
13 changes: 13 additions & 0 deletions src/elements/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7af715f

Please sign in to comment.