Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(combobox): correctly select an item with selection-mode=‘single-persist’ when items have the same values #10366

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,43 @@ describe("calcite-combobox", () => {
expect(await combobox.getProperty("open")).toBe(true);
});

it("single-persist-selection mode correctly selects different items with the same value", async () => {
const page = await newE2EPage();
await page.setContent(html`
<calcite-combobox selection-mode="single-persist">
<calcite-combobox-item value="one" heading="one"></calcite-combobox-item>
<calcite-combobox-item value="one" heading="two"></calcite-combobox-item>
</calcite-combobox>
`);

const combobox = await page.find("calcite-combobox");

const firstOpenEvent = page.waitForEvent("calciteComboboxOpen");
await combobox.click();
await firstOpenEvent;

const item1 = await combobox.find("calcite-combobox-item[heading=one]");
const item2 = await combobox.find("calcite-combobox-item[heading=two]");

await item1.click();
await page.waitForChanges();
expect(await combobox.getProperty("value")).toBe("one");
expect(await item1.getProperty("selected")).toBe(true);
expect(await item2.getProperty("selected")).toBe(false);
expect(await combobox.getProperty("open")).toBe(false);

const secondOpenEvent = page.waitForEvent("calciteComboboxOpen");
await combobox.click();
await secondOpenEvent;

await item2.click();
await page.waitForChanges();
expect(await combobox.getProperty("value")).toBe("one");
expect(await item1.getProperty("selected")).toBe(false);
expect(await item2.getProperty("selected")).toBe(true);
expect(await combobox.getProperty("open")).toBe(false);
});

it("multiple-selection mode allows toggling selection once the selected item is selected", async () => {
const page = await newE2EPage();
await page.setContent(html`
Expand Down
10 changes: 7 additions & 3 deletions packages/calcite-components/src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ export class Combobox
break;
case "Enter":
if (this.open && this.activeItemIndex > -1) {
this.toggleSelection(this.filteredItems[this.activeItemIndex]);
const item = this.filteredItems[this.activeItemIndex];
this.toggleSelection(item, !item.selected);
event.preventDefault();
} else if (this.activeChipIndex > -1) {
this.removeActiveChip();
Expand Down Expand Up @@ -1132,10 +1133,13 @@ export class Combobox

private emitComboboxChange = debounce(this.internalComboboxChangeEvent, 0);

toggleSelection(item: HTMLCalciteComboboxItemElement, value = !item.selected): void {
toggleSelection(item: HTMLCalciteComboboxItemElement, value: boolean): void {
if (
!item ||
(this.selectionMode === "single-persist" && item.selected && item.value === this.value)
(this.selectionMode === "single-persist" &&
item.selected &&
item.value === this.value &&
!value)
) {
return;
}
Expand Down
Loading