Skip to content

Commit

Permalink
fix(data-grid): tab management issue on re-renders
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSisb committed Oct 17, 2023
1 parent ad75602 commit 4622646
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/silly-mirrors-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@twilio-paste/data-grid": patch
"@twilio-paste/core": patch
---

[Data Grid] Fix issue where form elements in the data-grid that immediately re-render on mount cause the tabIndex management system to faulter.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
SortableColumnsDataGrid,
} from "../stories/index.stories";

jest.useFakeTimers();

const checkTagName = (el: Element, name: string): void => expect(el.tagName).toBe(name.toUpperCase());

describe("Data Grid", () => {
Expand Down Expand Up @@ -177,6 +179,9 @@ describe("Data Grid", () => {
if (firstInputCell == null) {
throw new Error("cannot find firstInputCell");
}
act(() => {
jest.advanceTimersByTime(300);
});

// Focus the button before the DataGrid
beforeDataGridButton.focus();
Expand Down Expand Up @@ -231,6 +236,7 @@ describe("Data Grid", () => {

// I added this particular sequence because it was a reproducable bug in my manual tests
act(() => {
jest.advanceTimersByTime(300);
firstThCell.focus();
});
expect(firstThCell).toHaveFocus();
Expand All @@ -243,6 +249,9 @@ describe("Data Grid", () => {
userEvent.tab();
userEvent.tab();
userEvent.keyboard("{enter}");
act(() => {
jest.advanceTimersByTime(300);
});
// Bring the focus back to the DataGrid
userEvent.tab({ shift: true });
userEvent.tab({ shift: true });
Expand Down
9 changes: 6 additions & 3 deletions packages/paste-core/components/data-grid/src/DataGridCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ export const DataGridCell: React.FC<React.PropsWithChildren<DataGridCellProps>>
* When actionable mode changes, toggle the tabindex of the cell and children
*/
React.useEffect(() => {
if (cellRef.current) {
updateTabIndexForActionable(cellRef.current, dataGridState.actionable);
}
setTimeout(() => {
if (cellRef.current) {
// This delay solves issues around components that re-render immediately on mount, like the Select componenent
updateTabIndexForActionable(cellRef.current, dataGridState.actionable);
}
}, 10);
}, [dataGridState.actionable]);

return (
Expand Down

0 comments on commit 4622646

Please sign in to comment.