diff --git a/.changeset/silver-bottles-pretend.md b/.changeset/silver-bottles-pretend.md new file mode 100644 index 0000000000..4efa7a16a1 --- /dev/null +++ b/.changeset/silver-bottles-pretend.md @@ -0,0 +1,6 @@ +--- +"@twilio-paste/data-grid": patch +"@twilio-paste/core": patch +--- + +chore(data-grid): change isCell function into a type guard and remove unnecessary type assertions diff --git a/packages/paste-core/components/data-grid/src/DataGrid.tsx b/packages/paste-core/components/data-grid/src/DataGrid.tsx index f49b752e69..6eb3db8756 100644 --- a/packages/paste-core/components/data-grid/src/DataGrid.tsx +++ b/packages/paste-core/components/data-grid/src/DataGrid.tsx @@ -98,15 +98,15 @@ export const DataGrid = React.forwardRef( // Set the actionable state setActionable(true); - const activeElement = getActiveElement() as HTMLElement; + const activeElement = getActiveElement(); // Only if it's a DataGrid cell - if (isCell(activeElement)) { + if (activeElement && isCell(activeElement)) { // Get the first focusable child const firstFocusableElement = getFirstFocusableIn(activeElement); // If there is a focusable child focus it if (firstFocusableElement) { - ensureFocus(firstFocusableElement as HTMLElement); + ensureFocus(firstFocusableElement); // First shift+tab fix upon entering actionable mode activeElement.tabIndex = actionable ? 0 : -1; } diff --git a/packages/paste-core/components/data-grid/src/utils/cell-management.ts b/packages/paste-core/components/data-grid/src/utils/cell-management.ts index c8d896a6c2..61fe8e0889 100644 --- a/packages/paste-core/components/data-grid/src/utils/cell-management.ts +++ b/packages/paste-core/components/data-grid/src/utils/cell-management.ts @@ -38,7 +38,7 @@ export const delayedSetFocusable = (element?: HTMLElement): void => { * * @returns {boolean}. */ -export const isCell = (element: Element): boolean => { +export const isCell = (element: Element): element is HTMLTableCellElement => { return element.tagName === "TD" || element.tagName === "TH"; }; @@ -73,7 +73,7 @@ export const getClosestGridCellFromCurrentFocus = (dataGridId: string): HTMLElem return null; } if (isCell(focusedElement)) { - return focusedElement as HTMLElement; + return focusedElement; } return getClosestCellFrom(focusedElement, dataGridId); };