Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

perf(Table): build the row-index column once per plugin (#10654)

Humberto Virtudes1378a95f8347
diff --git a/.changeset/table-row-index-stable-column.md b/.changeset/table-row-index-stable-column.md
new file mode 100644
index 0000000000..4984da6975
--- /dev/null
+++ b/.changeset/table-row-index-stable-column.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[perf] useTableRowIndex builds its number column once per plugin, so a parent re-render no longer re-renders every row of a table that shows row numbers.
+
+@humbertovirtudes
diff --git a/packages/core/src/Table/plugins/rowIndex/useTableRowIndex.test.tsx b/packages/core/src/Table/plugins/rowIndex/useTableRowIndex.test.tsx
index 989dca73fa..d2977421bf 100644
--- a/packages/core/src/Table/plugins/rowIndex/useTableRowIndex.test.tsx
+++ b/packages/core/src/Table/plugins/rowIndex/useTableRowIndex.test.tsx
@@ -1,9 +1,10 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
import {describe, it, expect} from 'vitest';
-import {render, screen, within} from '@testing-library/react';
+import {act, render, screen, within} from '@testing-library/react';
+import {useState} from 'react';
import {Table} from '../../Table';
-import type {TableColumn} from '../../types';
+import type {TableColumn, TablePlugin} from '../../types';
import {useTableRowIndex} from './useTableRowIndex';
interface Row extends Record<string, unknown> {
@@ -169,4 +170,34 @@ describe('useTableRowIndex', () => {
expect(within(rows[1]).getByText('1')).toBeInTheDocument();
expect(within(rows[2]).getByText('2')).toBeInTheDocument();
});
+
+ it('keeps unchanged rows from re-rendering when the parent re-renders', () => {
+ let cellTransforms = 0;
+ const audit: TablePlugin<Row> = {
+ rowModel: 'flat',
+ transformBodyCell(props) {
+ cellTransforms++;
+ return props;
+ },
+ };
+ let rerender: () => void = () => {};
+ function Parent() {
+ const [, setTick] = useState(0);
+ rerender = () => setTick(value => value + 1);
+ const rowIndex = useTableRowIndex<Row>();
+ return (
+ <Table
+ data={data}
+ columns={columns}
+ idKey="id"
+ plugins={{rowIndex, audit}}
+ />
+ );
+ }
+ render(<Parent />);
+ const afterMount = cellTransforms;
+ expect(afterMount).toBeGreaterThan(0);
+ act(() => rerender());
+ expect(cellTransforms).toBe(afterMount);
+ });
});
diff --git a/packages/core/src/Table/plugins/rowIndex/useTableRowIndex.tsx b/packages/core/src/Table/plugins/rowIndex/useTableRowIndex.tsx
index 66811bf034..ee527266c0 100644
--- a/packages/core/src/Table/plugins/rowIndex/useTableRowIndex.tsx
+++ b/packages/core/src/Table/plugins/rowIndex/useTableRowIndex.tsx
@@ -91,6 +91,22 @@ export function useTableRowIndex<T extends Record<string, unknown>>(
// the index cell's renderer reads the position that its own transform
// stored.
const positions = new WeakMap<T, number>();
+ // One column object per plugin, so a Table re-render keeps its resolved
+ // columns and leaves unchanged rows alone.
+ const indexColumn: TableColumn<T> = {
+ key: INDEX_COLUMN_KEY,
+ header: label,
+ width: INDEX_COLUMN_WIDTH,
+ align: 'end',
+ resizable: false,
+ renderCell: (item: T) => {
+ const idx = lookup == null ? positions.get(item) : lookup(item);
+ if (idx == null) {
+ return null;
+ }
+ return <span {...stylex.props(styles.index)}>{idx + startFrom}</span>;
+ },
+ };
return {
rowModel: 'flat',
transformBodyCell(props, column, item, _columnIndex, _columns, context) {
@@ -100,22 +116,6 @@ export function useTableRowIndex<T extends Record<string, unknown>>(
return props;
},
transformColumns(columns) {
- const indexColumn: TableColumn<T> = {
- key: INDEX_COLUMN_KEY,
- header: label,
- width: INDEX_COLUMN_WIDTH,
- align: 'end',
- resizable: false,
- renderCell: (item: T) => {
- const idx = lookup == null ? positions.get(item) : lookup(item);
- if (idx == null) {
- return null;
- }
- return (
- <span {...stylex.props(styles.index)}>{idx + startFrom}</span>
- );
- },
- };
return [indexColumn, ...columns];
},
};