Skip to content

Commit

Permalink
fix(swatch): normalize repeat selection of same item in "selects=single"
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook committed Jan 23, 2023
1 parent 5c019c5 commit ee0fb0c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/swatch/src/SwatchGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ export class SwatchGroup extends SizedMixin(SpectrumElement, {
if (this.selects === 'single') {
const { target } = event;
target.tabIndex = 0;
target.selected = true;
if (this.selectedSet.has(target.value)) {
return;
}
this.selectedSet.clear();
this.selectedSet.add(target.value);
this.rovingTabindexController.elements.forEach((child) => {
Expand Down
13 changes: 11 additions & 2 deletions packages/swatch/stories/swatch-group.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,16 @@ export default {
},
},
decorators: [
(story: () => TemplateResult): TemplateResult => html`
(
story: () => TemplateResult,
{
args: { selected = [] },
}: {
args: {
selected: string[];
};
}
): TemplateResult => html`
<div
@change=${async (event: Event & { target: SwatchGroup }) => {
await 0;
Expand All @@ -111,7 +120,7 @@ export default {
}}
>
${story()}
<div>Selected: []</div>
<div>Selected: ${JSON.stringify(selected)}</div>
</div>
`,
],
Expand Down
6 changes: 6 additions & 0 deletions packages/swatch/test/swatch-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ describe('Swatch Group', () => {
expect(changeSpy.calledOnce).to.be.true;
expect(el.selected).to.deep.equal([selectedChild.value]);
expect(selectedChild.selected).to.be.true;

selectedChild.click();

expect(changeSpy.calledOnce).to.be.true;
expect(el.selected).to.deep.equal([selectedChild.value]);
expect(selectedChild.selected).to.be.true;
});
it('can have `change` events prevented', async () => {
el.selects = 'single';
Expand Down

0 comments on commit ee0fb0c

Please sign in to comment.