Skip to content

Commit 77c6316

Browse files
authored
Update uncontrolled widths when columns change in aria table sizing example (#5345)
1 parent 7d12c66 commit 77c6316

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

packages/@react-aria/table/test/tableResizingTests.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,23 @@ export let resizingTests = (render, rerender, Table, ControlledTable, resizeCol,
527527
expect(getColumnWidths(tree)).toStrictEqual([113, 112, 113, 112, 450]);
528528
});
529529

530+
it('onResize end called with values even if no resizing took place uncontrolled', function () {
531+
let allowsResizing = true;
532+
let columns = [
533+
{name: 'Name', id: 'name', allowsResizing},
534+
{name: 'Type', id: 'type', defaultWidth: '3fr', allowsResizing},
535+
{name: 'Level', id: 'level', allowsResizing},
536+
{name: 'Height', id: 'height', defaultWidth: '5fr', allowsResizing}
537+
];
538+
let columnNames = ['Name', 'Type', 'Level', 'Height'];
539+
let onResizeEnd = jest.fn();
540+
let tree = render(<Table columns={columns} rows={rows} onResizeEnd={onResizeEnd} />);
541+
expect(getColumnWidths(tree)).toStrictEqual([90, 270, 90, 450]);
542+
resizeCol(tree, 'Type', 0);
543+
expect(getColumnWidths(tree)).toStrictEqual([90, 270, 90, 450]);
544+
expect(onResizeEnd).toHaveBeenCalledWith(mapFromWidths(columnNames, [90, 270, '1fr', '5fr']));
545+
});
546+
530547
it('onResizeStart called with expected values', function () {
531548
let columns = [
532549
{name: 'Name', uid: 'name', width: '1fr'},

packages/@react-stately/table/src/useTableColumnResizeState.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ export function useTableColumnResizeState<T>(props: TableColumnResizeStateProps<
8181
let [uncontrolledWidths, setUncontrolledWidths] = useState(() =>
8282
columnLayout.getInitialUncontrolledWidths(uncontrolledColumns)
8383
);
84+
85+
// Update uncontrolled widths if the columns changed.
86+
let [lastColumns, setLastColumns] = useState(state.collection.columns);
87+
if (state.collection.columns !== lastColumns) {
88+
if (
89+
state.collection.columns.length !== lastColumns.length ||
90+
state.collection.columns.some((c, i) => c.key !== lastColumns[i].key)
91+
) {
92+
let newUncontrolledWidths = columnLayout.getInitialUncontrolledWidths(uncontrolledColumns);
93+
setUncontrolledWidths(newUncontrolledWidths);
94+
}
95+
setLastColumns(state.collection.columns);
96+
}
97+
8498
// combine columns back into one map that maintains same order as the columns
8599
let colWidths = useMemo(() =>
86100
columnLayout.recombineColumns(state.collection.columns, uncontrolledWidths, uncontrolledColumns, controlledColumns)

packages/react-aria-components/test/Table.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,10 +828,10 @@ describe('Table', () => {
828828
resizingTests(render, (tree, ...args) => tree.rerender(...args), ResizableTable, ControlledResizableTable, resizeCol, resizeTable);
829829

830830
function ResizableTable(props) {
831-
let {columns, rows} = props;
831+
let {columns, rows, onResizeStart, onResize, onResizeEnd, ...otherProps} = props;
832832
return (
833-
<ResizableTableContainer>
834-
<Table aria-label="Files">
833+
<ResizableTableContainer onResizeStart={onResizeStart} onResize={onResize} onResizeEnd={onResizeEnd}>
834+
<Table aria-label="Files" {...otherProps}>
835835
<MyTableHeader columns={columns}>
836836
{column => (
837837
<MyColumn {...column} isRowHeader={column.id === 'name'}>

0 commit comments

Comments
 (0)