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

chore(data-grid): change isCell function into a type guard and remove unnecessary type assertions #3516

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/silver-bottles-pretend.md
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions packages/paste-core/components/data-grid/src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ export const DataGrid = React.forwardRef<HTMLTableElement, DataGridProps>(
// 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
abereghici marked this conversation as resolved.
Show resolved Hide resolved
return element.tagName === "TD" || element.tagName === "TH";
};

Expand Down Expand Up @@ -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);
};
Loading