From ee0fb0c60cda72f4a7896444f27ffab06c49986d Mon Sep 17 00:00:00 2001 From: Westbrook Johnson Date: Mon, 23 Jan 2023 09:35:43 -0500 Subject: [PATCH] fix(swatch): normalize repeat selection of same item in "selects=single" --- packages/swatch/src/SwatchGroup.ts | 4 ++++ packages/swatch/stories/swatch-group.stories.ts | 13 +++++++++++-- packages/swatch/test/swatch-group.test.ts | 6 ++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/swatch/src/SwatchGroup.ts b/packages/swatch/src/SwatchGroup.ts index d5f0a36ba4..ff350b1f32 100644 --- a/packages/swatch/src/SwatchGroup.ts +++ b/packages/swatch/src/SwatchGroup.ts @@ -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) => { diff --git a/packages/swatch/stories/swatch-group.stories.ts b/packages/swatch/stories/swatch-group.stories.ts index ef3c3c1fbf..f605884804 100644 --- a/packages/swatch/stories/swatch-group.stories.ts +++ b/packages/swatch/stories/swatch-group.stories.ts @@ -98,7 +98,16 @@ export default { }, }, decorators: [ - (story: () => TemplateResult): TemplateResult => html` + ( + story: () => TemplateResult, + { + args: { selected = [] }, + }: { + args: { + selected: string[]; + }; + } + ): TemplateResult => html`
{ await 0; @@ -111,7 +120,7 @@ export default { }} > ${story()} -
Selected: []
+
Selected: ${JSON.stringify(selected)}
`, ], diff --git a/packages/swatch/test/swatch-group.test.ts b/packages/swatch/test/swatch-group.test.ts index b77b0e4910..be6338bd2e 100644 --- a/packages/swatch/test/swatch-group.test.ts +++ b/packages/swatch/test/swatch-group.test.ts @@ -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';