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): prevent deselecting items via keyboard in single-persist mode #7634

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
222 changes: 118 additions & 104 deletions packages/calcite-components/src/components/combobox/combobox.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,117 +378,131 @@ describe("calcite-combobox", () => {

describe("item selection", () => {
describe("toggling items", () => {
it("single-selection mode allows toggling selection once the selected item is clicked", async () => {
const page = await newE2EPage();
await page.setContent(
html`
<calcite-combobox selection-mode="single">
<calcite-combobox-item value="one" text-label="one"></calcite-combobox-item>
<calcite-combobox-item value="two" text-label="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[value=one]");

await item1.click();
expect(await combobox.getProperty("value")).toBe("one");

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

await item1.click();
expect(await combobox.getProperty("value")).toBe("");
});

it("single-persist-selection mode does not allow toggling selection once the selected item is clicked", async () => {
const page = await newE2EPage();
await page.setContent(
html`
<calcite-combobox selection-mode="single-persist">
<calcite-combobox-item value="one" text-label="one"></calcite-combobox-item>
<calcite-combobox-item value="two" text-label="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[value=one]");

await item1.click();
expect(await combobox.getProperty("value")).toBe("one");

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

await item1.click();
expect(await combobox.getProperty("value")).toBe("one");
describe("via keyboard", () => {
assertSelectionModeToggling(async (item): Promise<void> => {
await item.press("Enter");
});
});

it("multiple-selection mode allows toggling selection once the selected item is clicked", async () => {
const page = await newE2EPage();
await page.setContent(
html`
<calcite-combobox selection-mode="multiple">
<calcite-combobox-item value="one" text-label="one"></calcite-combobox-item>
<calcite-combobox-item value="two" text-label="two"></calcite-combobox-item>
</calcite-combobox>
`
);
const combobox = await page.find("calcite-combobox");
const openEvent = page.waitForEvent("calciteComboboxOpen");
await combobox.click();
await openEvent;

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

await item1.click();
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeDefined();

await item1.click();
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeNull();

await item1.click();
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeDefined();
describe("via mouse", () => {
assertSelectionModeToggling(async (item): Promise<void> => {
await item.click();
});
});

it("ancestors-selection mode allows toggling selection once the selected item is clicked", async () => {
const page = await newE2EPage();
await page.setContent(
html`
<calcite-combobox selection-mode="ancestors">
<calcite-combobox-item value="one" text-label="parent">
<calcite-combobox-item value="two" text-label="child1"></calcite-combobox-item>
<calcite-combobox-item value="three" text-label="child2"></calcite-combobox-item>
</calcite-combobox-item>
</calcite-combobox>
`
);
const combobox = await page.find("calcite-combobox");
const openEvent = page.waitForEvent("calciteComboboxOpen");
await combobox.click();
await openEvent;

const item1 = await combobox.find("calcite-combobox-item[value=one]");
async function assertSelectionModeToggling(selectItem: (item: E2EElement) => Promise<void>): Promise<void> {
it("single-selection mode allows toggling selection once the selected item is selected", async () => {
const page = await newE2EPage();
await page.setContent(
html`
<calcite-combobox selection-mode="single">
<calcite-combobox-item value="one" text-label="one"></calcite-combobox-item>
<calcite-combobox-item value="two" text-label="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[value=one]");

await selectItem(item1);
expect(await combobox.getProperty("value")).toBe("one");

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

await selectItem(item1);
expect(await combobox.getProperty("value")).toBe("");
});

await item1.click();
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeDefined();
it("single-persist-selection mode does not allow toggling selection once the selected item is selected", async () => {
const page = await newE2EPage();
await page.setContent(
html`
<calcite-combobox selection-mode="single-persist">
<calcite-combobox-item value="one" text-label="one"></calcite-combobox-item>
<calcite-combobox-item value="two" text-label="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[value=one]");

await selectItem(item1);
expect(await combobox.getProperty("value")).toBe("one");

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

await selectItem(item1);
expect(await combobox.getProperty("value")).toBe("one");
});

await item1.click();
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeNull();
it("multiple-selection mode allows toggling selection once the selected item is selected", async () => {
const page = await newE2EPage();
await page.setContent(
html`
<calcite-combobox selection-mode="multiple">
<calcite-combobox-item value="one" text-label="one"></calcite-combobox-item>
<calcite-combobox-item value="two" text-label="two"></calcite-combobox-item>
</calcite-combobox>
`
);
const combobox = await page.find("calcite-combobox");
const openEvent = page.waitForEvent("calciteComboboxOpen");
await combobox.click();
await openEvent;

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

await selectItem(item1);
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeDefined();

await selectItem(item1);
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeNull();

await selectItem(item1);
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeDefined();
});

await item1.click();
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeDefined();
});
it("ancestors-selection mode allows toggling selection once the selected item is selected", async () => {
const page = await newE2EPage();
await page.setContent(
html`
<calcite-combobox selection-mode="ancestors">
<calcite-combobox-item value="one" text-label="parent">
<calcite-combobox-item value="two" text-label="child1"></calcite-combobox-item>
<calcite-combobox-item value="three" text-label="child2"></calcite-combobox-item>
</calcite-combobox-item>
</calcite-combobox>
`
);
const combobox = await page.find("calcite-combobox");
const openEvent = page.waitForEvent("calciteComboboxOpen");
await combobox.click();
await openEvent;

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

await selectItem(item1);
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeDefined();

await selectItem(item1);
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeNull();

await selectItem(item1);
expect(await page.find("calcite-combobox >>> calcite-chip")).toBeDefined();
});
}
});

it("should select parent in ancestor selection mode", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,10 @@ export class Combobox
private emitComboboxChange = debounce(this.internalComboboxChangeEvent, 0);

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

Expand All @@ -907,6 +910,7 @@ export class Combobox
this.ignoreSelectedEventsFlag = false;
this.selectedItems = this.getSelectedItems();
this.emitComboboxChange();

if (this.textInput) {
this.textInput.value = item.textLabel;
}
Expand Down Expand Up @@ -1135,7 +1139,7 @@ export class Combobox
}

isMulti(): boolean {
return this.selectionMode !== "single" && this.selectionMode !== "single-persist";
return !isSingleLike(this.selectionMode);
}

comboboxFocusHandler = (): void => {
Expand Down
Loading