Skip to content

fix(combobox): fix navigating initially when multiple items are selected #9613

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

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/calcite-components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,7 @@ export namespace Components {
*/
"items": object[];
/**
* Specifies the fields to match against when filtering.
* Specifies the fields to match against when filtering. This will only apply when `value` is an object. If not set, all fields will be matched.
*/
"matchFields": string[];
/**
Expand Down Expand Up @@ -9622,7 +9622,7 @@ declare namespace LocalJSX {
*/
"items"?: object[];
/**
* Specifies the fields to match against when filtering.
* Specifies the fields to match against when filtering. This will only apply when `value` is an object. If not set, all fields will be matched.
*/
"matchFields"?: string[];
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,82 @@ describe("calcite-combobox", () => {
}
});

describe("keyboard navigation with chips", () => {
let page: E2EPage;
beforeEach(async () => {
page = await newE2EPage();
await page.setContent(html`
<calcite-combobox id="myCombobox" placeholder="Select a field">
<calcite-combobox-item value="Natural Resources" text-label="Natural Resources"></calcite-combobox-item>
<calcite-combobox-item value="Agriculture" text-label="Agriculture"></calcite-combobox-item>
<calcite-combobox-item value="Forestry" text-label="Forestry"></calcite-combobox-item>
<calcite-combobox-item selected value="Mining" text-label="Mining"></calcite-combobox-item>
<calcite-combobox-item value="Business" text-label="Business"></calcite-combobox-item>
<calcite-combobox-item selected value="Education" text-label="Education"></calcite-combobox-item>
<calcite-combobox-item selected value="Utilities" text-label="Utilities"></calcite-combobox-item>
<calcite-combobox-item value="Transportation" text-label="Transportation"></calcite-combobox-item>
</calcite-combobox>
`);
});

it("should navigate chips with arrow keys", async () => {
const comboboxId = "myCombobox";
const inputId = "input";
const chipId = "chip";

const getActiveElementId = () => page.evaluate(() => document.activeElement.id);

const getDataTestId = () =>
page.$eval(`#${comboboxId}`, (myCombobox) => myCombobox.shadowRoot.activeElement.getAttribute("data-test-id"));

await page.keyboard.press("Tab");
await page.waitForChanges();

expect(await getActiveElementId()).toBe(comboboxId);
expect(await getDataTestId()).toBe(`${chipId}-0`);

await page.keyboard.press("ArrowRight");
await page.waitForChanges();
expect(await getActiveElementId()).toBe(comboboxId);
expect(await getDataTestId()).toBe(`${chipId}-1`);

await page.keyboard.press("ArrowRight");
await page.waitForChanges();
expect(await getActiveElementId()).toBe(comboboxId);
expect(await getDataTestId()).toBe(`${chipId}-2`);

await page.keyboard.press("ArrowRight");
await page.waitForChanges();
expect(await getActiveElementId()).toBe(comboboxId);
expect(await getDataTestId()).toBe(inputId);

await page.keyboard.press("ArrowRight");
await page.waitForChanges();
expect(await getActiveElementId()).toBe(comboboxId);
expect(await getDataTestId()).toBe(inputId);

await page.keyboard.press("ArrowLeft");
await page.waitForChanges();
expect(await getActiveElementId()).toBe(comboboxId);
expect(await getDataTestId()).toBe(`${chipId}-2`);

await page.keyboard.press("ArrowLeft");
await page.waitForChanges();
expect(await getActiveElementId()).toBe(comboboxId);
expect(await getDataTestId()).toBe(`${chipId}-1`);

await page.keyboard.press("ArrowLeft");
await page.waitForChanges();
expect(await getActiveElementId()).toBe(comboboxId);
expect(await getDataTestId()).toBe(`${chipId}-0`);

await page.keyboard.press("ArrowLeft");
await page.waitForChanges();
expect(await getActiveElementId()).toBe(comboboxId);
expect(await getDataTestId()).toBe(`${chipId}-0`);
});
});

describe("keyboard navigation in all selection-display mode", () => {
let page: E2EPage;
const scrollablePageSizeInPx = 2400;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1387,12 +1387,14 @@ export class Combobox
appearance={readOnly ? "outline" : "solid"}
class={chipClasses}
closable={!readOnly}
data-test-id={`chip-${i}`}
icon={item.icon}
iconFlipRtl={item.iconFlipRtl}
id={item.guid ? `${chipUidPrefix}${item.guid}` : null}
key={item.textLabel}
messageOverrides={{ dismissLabel: messages.removeTag }}
onCalciteChipClose={() => this.calciteChipCloseHandler(item)}
onFocusin={() => (this.activeChipIndex = i)}
scale={scale}
selected={item.selected}
title={label}
Expand Down Expand Up @@ -1597,6 +1599,7 @@ export class Combobox
"input--hidden": showLabel,
"input--icon": this.showingInlineIcon && !!this.placeholderIcon,
}}
data-test-id="input"
disabled={disabled}
id={`${inputUidPrefix}${guid}`}
key="input"
Expand Down
Loading