diff --git a/packages/terra-compact-interactive-list/CHANGELOG.md b/packages/terra-compact-interactive-list/CHANGELOG.md index df7d270fd7c..28b53f2fe91 100644 --- a/packages/terra-compact-interactive-list/CHANGELOG.md +++ b/packages/terra-compact-interactive-list/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Fixed + * Added check for cell element existence before focusing. + ## 1.6.0 - (February 28, 2024) * Changed diff --git a/packages/terra-compact-interactive-list/src/CompactInteractiveList.jsx b/packages/terra-compact-interactive-list/src/CompactInteractiveList.jsx index 6f4d8631c7d..5801a399c66 100644 --- a/packages/terra-compact-interactive-list/src/CompactInteractiveList.jsx +++ b/packages/terra-compact-interactive-list/src/CompactInteractiveList.jsx @@ -154,14 +154,16 @@ const CompactInteractiveList = (props) => { const focusCell = ({ row, cell }) => { // add 1 to the row number to accommodate for hidden header - const focusedCellElement = listRef.current.children[row + 1].children[cell]; - const interactiveChildren = getFocusableElements(focusedCellElement); - if (interactiveChildren?.length > 0 && interactiveChildren[0]) { - // currently a cell can have only one interact-able element in it, which gets auto-focused. - interactiveChildren[0].focus(); - } else { - // cell gets focus if there is no interact-able elements in it. - focusedCellElement.focus(); + const focusedCellElement = listRef.current?.children[row + 1]?.children[cell]; + if (focusedCellElement) { + const interactiveChildren = getFocusableElements(focusedCellElement); + if (interactiveChildren?.length > 0 && interactiveChildren[0]) { + // currently a cell can have only one interact-able element in it, which gets auto-focused. + interactiveChildren[0].focus(); + } else { + // cell gets focus if there is no interact-able elements in it. + focusedCellElement.focus(); + } } };