Skip to content

Commit

Permalink
fix(segmented-control): Make check state update correctly (#9733)
Browse files Browse the repository at this point in the history
**Related Issue:** #6860 

## Summary
Made the checked state of segmented control update when the clearing value is undefined.
  • Loading branch information
josercarcamo authored and github-actions[bot] committed Jul 30, 2024
1 parent e3cfd48 commit 602c922
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ describe("calcite-segmented-control", () => {
expect(selectedValue).toBe("3");
});

it("allows items to be selected", async () => {
async function getSelectedItemValue(page: E2EPage): Promise<string> {
return page.$eval(
"calcite-segmented-control",
(segmentedControl: HTMLCalciteSegmentedControlElement) => segmentedControl.selectedItem.value,
);
}
async function getSelectedItemValue(page: E2EPage): Promise<string> {
return page.$eval(
"calcite-segmented-control",
(segmentedControl: HTMLCalciteSegmentedControlElement) => segmentedControl.selectedItem.value,
);
}

it("allows items to be selected", async () => {
const page = await newE2EPage();
await page.setContent(
`<calcite-segmented-control>
Expand Down Expand Up @@ -193,6 +193,29 @@ describe("calcite-segmented-control", () => {
expect(await getSelectedItemValue(page)).toBe("2");
});

it("updates selection when cleared with undefined", async () => {
const page = await newE2EPage();
await page.setContent(
`<calcite-segmented-control>
<calcite-segmented-control-item value="1" checked>one</calcite-segmented-control-item>
<calcite-segmented-control-item value="2">two</calcite-segmented-control-item>
</calcite-segmented-control>`,
);
await page.waitForChanges();
expect(await getSelectedItemValue(page)).toBe("1");

const [first, second] = await page.findAll("calcite-segmented-control-item");
first.setProperty("checked", undefined);
second.setProperty("checked", true);
await page.waitForChanges();
expect(await getSelectedItemValue(page)).toBe("2");

first.setProperty("checked", true);
second.setProperty("checked", undefined);
await page.waitForChanges();
expect(await getSelectedItemValue(page)).toBe("1");
});

it("does not emit extraneous events (edge case from #3210)", async () => {
const page = await newE2EPage();
await page.setContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ export class SegmentedControl
@Listen("calciteInternalSegmentedControlItemChange")
protected handleSelected(event: Event): void {
event.preventDefault();
this.selectItem(event.target as HTMLCalciteSegmentedControlItemElement);
const el = event.target as HTMLCalciteSegmentedControlItemElement;
if (el.checked) {
this.selectItem(el);
}
event.stopPropagation();
}

Expand Down

0 comments on commit 602c922

Please sign in to comment.