Skip to content

Commit

Permalink
fix: update Picker label via MutationObserver instead of "slotchange"
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook committed Jun 21, 2022
1 parent c8a0440 commit 196998e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/menu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"lit-html"
],
"dependencies": {
"@lit-labs/observers": "^1.0.1",
"@spectrum-web-components/action-button": "^0.8.6",
"@spectrum-web-components/base": "^0.5.7",
"@spectrum-web-components/divider": "^0.4.9",
Expand Down
20 changes: 15 additions & 5 deletions packages/menu/src/MenuItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import checkmarkStyles from '@spectrum-web-components/icon/src/spectrum-icon-che
import type { Menu } from './Menu.js';
import type { OverlayOpenCloseDetail } from '@spectrum-web-components/overlay';
import { reparentChildren } from '@spectrum-web-components/shared/src/reparent-children.js';
import { MutationController } from '@lit-labs/observers/mutation_controller.js';

/**
* Duration during which a pointing device can leave an `<sp-menu-item>` element
Expand Down Expand Up @@ -209,6 +210,18 @@ export class MenuItem extends LikeAnchor(Focusable) {
this.addEventListener('click', this.handleClickCapture, {
capture: true,
});

new MutationController(this, {
config: {
characterData: true,
childList: true,
subtree: true,
},
callback: (): boolean => {
this.breakItemChildrenCache();
return true;
},
});
}

@property({ type: Boolean })
Expand Down Expand Up @@ -255,12 +268,9 @@ export class MenuItem extends LikeAnchor(Focusable) {

protected override render(): TemplateResult {
return html`
<slot name="icon" @slotchange=${this.breakItemChildrenCache}></slot>
<slot name="icon"></slot>
<div id="label">
<slot
id="slot"
@slotchange=${this.breakItemChildrenCache}
></slot>
<slot id="slot"></slot>
</div>
<slot name="value"></slot>
${this.selected
Expand Down
16 changes: 11 additions & 5 deletions packages/picker/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,20 @@ export function runPickerTests(): void {
expect((el.button.textContent || '').trim()).to.equal(
'Select Inverse'
);
const itemUpdated = oneEvent(el, 'sp-menu-item-added-or-updated');
option2.innerHTML = 'Invert Selection';
let itemUpdated = oneEvent(el, 'sp-menu-item-added-or-updated');
const newLabel1 = 'Invert Selection';
option2.innerHTML = newLabel1;
await itemUpdated;
await elementUpdated(el);
expect(el.value).to.equal('option-2');
expect((el.button.textContent || '').trim()).to.equal(
'Invert Selection'
);
expect((el.button.textContent || '').trim()).to.equal(newLabel1);
itemUpdated = oneEvent(el, 'sp-menu-item-added-or-updated');
const newLabel2 = 'Other option';
option2.childNodes[0].textContent = newLabel2;
await itemUpdated;
await elementUpdated(el);
expect(el.value).to.equal('option-2');
expect((el.button.textContent || '').trim()).to.equal(newLabel2);
});
it('accepts new selected item content when open', async () => {
const option2 = el.querySelector('[value="option-2"') as MenuItem;
Expand Down

0 comments on commit 196998e

Please sign in to comment.