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(list): fix keyboard navigation when filterEnabled is true #7385

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
55 changes: 46 additions & 9 deletions packages/calcite-components/src/components/list/list.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,23 +369,23 @@ describe("calcite-list", () => {
await list.callMethod("setFocus");
await page.waitForChanges();

await isElementFocused(page, "#one");
expect(await isElementFocused(page, "#one")).toBe(true);

await list.press("ArrowDown");

await isElementFocused(page, "#two");
expect(await isElementFocused(page, "#two")).toBe(true);

await list.press("ArrowDown");

await isElementFocused(page, "#two");
expect(await isElementFocused(page, "#two")).toBe(true);

await list.press("ArrowUp");

await isElementFocused(page, "#one");
expect(await isElementFocused(page, "#one")).toBe(true);

await list.press("ArrowDown");

await isElementFocused(page, "#two");
expect(await isElementFocused(page, "#two")).toBe(true);

const listItemThree = await page.find("#three");
listItemThree.setProperty("disabled", false);
Expand All @@ -394,7 +394,7 @@ describe("calcite-list", () => {

await list.press("ArrowDown");

await isElementFocused(page, "#three");
expect(await isElementFocused(page, "#three")).toBe(true);

const listItemFour = await page.find("#four");
listItemFour.setProperty("closed", false);
Expand All @@ -403,15 +403,52 @@ describe("calcite-list", () => {

await list.press("ArrowDown");

await isElementFocused(page, "#four");
expect(await isElementFocused(page, "#four")).toBe(true);

await list.press("Home");

await isElementFocused(page, "#one");
expect(await isElementFocused(page, "#one")).toBe(true);

await list.press("End");

await isElementFocused(page, "#four");
expect(await isElementFocused(page, "#four")).toBe(true);
});

it("should navigate via ArrowUp, ArrowDown with filtered items", async () => {
const page = await newE2EPage();
await page.setContent(html`
<calcite-list filter-enabled filter-text="water">
<calcite-list-item id="one" value="fire" label="fire" description="fire"></calcite-list-item>
<calcite-list-item id="two" value="fire" label="fire" description="fire"></calcite-list-item>
<calcite-list-item id="three" value="fire" label="fire" description="fire"></calcite-list-item>
<calcite-list-item id="four" value="water" label="water" description="water"></calcite-list-item>
<calcite-list-item id="five" value="water" label="water" description="water"></calcite-list-item>
<calcite-list-item id="six" value="water" label="water" description="water"></calcite-list-item>
</calcite-list>
`);
await page.waitForChanges();
const list = await page.find("calcite-list");
await page.waitForTimeout(listDebounceTimeout);
await list.callMethod("setFocus");
await page.waitForChanges();

expect(await isElementFocused(page, "calcite-filter", { shadowed: true })).toBe(true);

await list.press("ArrowDown");

expect(await isElementFocused(page, "#four")).toBe(true);

await list.press("ArrowDown");

expect(await isElementFocused(page, "#five")).toBe(true);

await list.press("ArrowUp");

expect(await isElementFocused(page, "#four")).toBe(true);

await list.press("ArrowUp");

expect(await isElementFocused(page, "calcite-filter", { shadowed: true })).toBe(true);
});
});
});
18 changes: 16 additions & 2 deletions packages/calcite-components/src/components/list/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ export class List implements InteractiveComponent, LoadableComponent {
@Method()
async setFocus(): Promise<void> {
await componentFocusable(this);

if (this.filterEl && this.filterEnabled) {
driskull marked this conversation as resolved.
Show resolved Hide resolved
return this.filterEl.setFocus();
}

return this.enabledListItems.find((listItem) => listItem.active)?.setFocus();
}

Expand Down Expand Up @@ -493,7 +498,7 @@ export class List implements InteractiveComponent, LoadableComponent {
}
}
this.updateFilteredItems(emit);
this.enabledListItems = items.filter((item) => !item.disabled && !item.closed);
this.enabledListItems = this.filteredItems.filter((item) => !item.disabled && !item.closed);
this.setActiveListItem();
this.updateSelectedItems(emit);
}, debounceTimeout);
Expand Down Expand Up @@ -533,7 +538,14 @@ export class List implements InteractiveComponent, LoadableComponent {
const filteredItems = this.enabledListItems.filter((listItem) => this.isNavigable(listItem));
const currentIndex = filteredItems.findIndex((listItem) => listItem.active);

if (key === "ArrowDown") {
if (key === "ArrowDown" && event.target === this.filterEl) {
driskull marked this conversation as resolved.
Show resolved Hide resolved
event.preventDefault();
const nextIndex = 0;

if (filteredItems[nextIndex]) {
this.focusRow(filteredItems[nextIndex]);
}
} else if (key === "ArrowDown") {
event.preventDefault();
const nextIndex = currentIndex + 1;

Expand All @@ -546,6 +558,8 @@ export class List implements InteractiveComponent, LoadableComponent {

if (filteredItems[prevIndex]) {
this.focusRow(filteredItems[prevIndex]);
} else if (currentIndex === 0 && this.filterEnabled && this.filterEl) {
this.filterEl.setFocus();
driskull marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (key === "Home") {
event.preventDefault();
Expand Down