From 3dfd3e17ac15b4d70644d7738dfb0d22f96607a4 Mon Sep 17 00:00:00 2001 From: Alexandru Bereghici Date: Wed, 4 Oct 2023 15:31:38 +0300 Subject: [PATCH 1/3] chore(data-grid): change isCell function into a type guard and remove unnecessary type assertions --- .changeset/silver-bottles-pretend.md | 5 +++++ packages/paste-core/components/data-grid/src/DataGrid.tsx | 6 +++--- .../components/data-grid/src/utils/cell-management.ts | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 .changeset/silver-bottles-pretend.md diff --git a/.changeset/silver-bottles-pretend.md b/.changeset/silver-bottles-pretend.md new file mode 100644 index 0000000000..d9dcfc7380 --- /dev/null +++ b/.changeset/silver-bottles-pretend.md @@ -0,0 +1,5 @@ +--- +"@twilio-paste/data-grid": 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..5e625c9eb1 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); }; From 86c7b1cf5bb1c3919883e3ce8f294d203720945b Mon Sep 17 00:00:00 2001 From: Alexandru Bereghici Date: Thu, 5 Oct 2023 00:01:11 +0300 Subject: [PATCH 2/3] Update .changeset/silver-bottles-pretend.md Co-authored-by: Shadi --- .changeset/silver-bottles-pretend.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/silver-bottles-pretend.md b/.changeset/silver-bottles-pretend.md index d9dcfc7380..4efa7a16a1 100644 --- a/.changeset/silver-bottles-pretend.md +++ b/.changeset/silver-bottles-pretend.md @@ -1,5 +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 From 25c84ea6bc0f2fcbdda3a99622695726d5d838c4 Mon Sep 17 00:00:00 2001 From: Alexandru Bereghici Date: Thu, 5 Oct 2023 11:25:27 +0300 Subject: [PATCH 3/3] chore: format cell-management file --- .../components/data-grid/src/utils/cell-management.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5e625c9eb1..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 @@ -73,7 +73,7 @@ export const getClosestGridCellFromCurrentFocus = (dataGridId: string): HTMLElem return null; } if (isCell(focusedElement)) { - return focusedElement + return focusedElement; } return getClosestCellFrom(focusedElement, dataGridId); };