Skip to content

Commit

Permalink
fix(list-item): focus on the first focusable element within the compo…
Browse files Browse the repository at this point in the history
…nent when using arrow keys. #7974
  • Loading branch information
driskull committed Nov 29, 2023
1 parent 7aeecd5 commit ef17a85
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions packages/calcite-components/src/components/list-item/list-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
VNode,
Watch,
} from "@stencil/core";
import { getElementDir, slotChangeHasAssignedElement, toAriaBoolean } from "../../utils/dom";
import {
focusFirstTabbable,
getElementDir,
slotChangeHasAssignedElement,
toAriaBoolean,
} from "../../utils/dom";
import {
connectInteractive,
disconnectInteractive,
Expand Down Expand Up @@ -344,7 +349,7 @@ export class ListItem
const focusIndex = focusMap.get(parentListEl);

if (typeof focusIndex === "number") {
const cells = [actionsStartEl, contentEl, actionsEndEl].filter(Boolean);
const cells = [actionsStartEl, contentEl, actionsEndEl].filter((el) => el && !el.hidden);
if (cells[focusIndex]) {
this.focusCell(cells[focusIndex]);
} else {
Expand Down Expand Up @@ -737,7 +742,7 @@ export class ListItem
const composedPath = event.composedPath();
const { containerEl, contentEl, actionsStartEl, actionsEndEl, open, openable } = this;

const cells = [actionsStartEl, contentEl, actionsEndEl].filter(Boolean);
const cells = [actionsStartEl, contentEl, actionsEndEl].filter((el) => el && !el.hidden);
const currentIndex = cells.findIndex((cell) => composedPath.includes(cell));

if (
Expand Down Expand Up @@ -790,16 +795,18 @@ export class ListItem
focusMap.set(parentListEl, null);
}

[actionsStartEl, contentEl, actionsEndEl].filter(Boolean).forEach((tableCell, cellIndex) => {
const tabIndexAttr = "tabindex";
if (tableCell === focusEl) {
tableCell.setAttribute(tabIndexAttr, "0");
saveFocusIndex && focusMap.set(parentListEl, cellIndex);
} else {
tableCell.removeAttribute(tabIndexAttr);
}
});
[actionsStartEl, contentEl, actionsEndEl]
.filter((el) => el && !el.hidden)
.forEach((tableCell, cellIndex) => {
const tabIndexAttr = "tabindex";
if (tableCell === focusEl) {
tableCell.setAttribute(tabIndexAttr, "0");
saveFocusIndex && focusMap.set(parentListEl, cellIndex);
} else {
tableCell.removeAttribute(tabIndexAttr);
}
});

focusEl?.focus();
focusFirstTabbable(focusEl);
};
}

0 comments on commit ef17a85

Please sign in to comment.