Skip to content

Commit

Permalink
column resize event trigger after dom update
Browse files Browse the repository at this point in the history
  • Loading branch information
shaharyar-shamshi committed Feb 26, 2024
1 parent ce71551 commit 4bcd99b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,21 @@ export const useGridColumnResize = (

const columnVirtualizationDisabled = useColumnVirtualizationDisabled(apiRef);
const isAutosizingRef = React.useRef(false);

React.useEffect(() => {
if (colDefRef.current && !isAutosizingRef.current) {
const columnsState = gridColumnsStateSelector(apiRef.current.state);
const column: GridStateColDef = columnsState.lookup[colDefRef.current.field];
const width: number = column.width as number;
apiRef.current.publishEvent('columnWidthChange', {
element: apiRef.current.getColumnHeaderElement(colDefRef.current.field),
colDef: { ...colDefRef.current, width },
width,
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isAutosizingRef.current, apiRef]);

const autosizeColumns = React.useCallback<GridColumnResizeApi['autosizeColumns']>(
async (userOptions) => {
const root = apiRef.current.rootElementRef?.current;
Expand Down Expand Up @@ -723,17 +738,6 @@ export const useGridColumnResize = (
}

apiRef.current.updateColumns(newColumns);

if (colDefRef.current) {
const columnsState = gridColumnsStateSelector(apiRef.current.state);
const column: GridStateColDef = columnsState.lookup[colDefRef.current.field];
const width: number = column.width as number;
apiRef.current.publishEvent('columnWidthChange', {
element: apiRef.current.getColumnHeaderElement(colDefRef.current.field),
colDef: { ...colDefRef.current, width },
width,
});
}
} finally {
apiRef.current.unstable_setColumnVirtualization(true);
isAutosizingRef.current = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ describe('<DataGridPro /> - Columns', () => {
await microtasks();
expect(onColumnWidthChange.callCount).to.equal(2);
expect(onColumnWidthChange.args[1][0].width).to.equal(116);
expect(onColumnWidthChange.args[1][0].element.clientWidth).to.equal(116);
expect(onColumnWidthChange.args[1][0].colDef.width).to.equal(116);
});

it('should not affect other cell elements that are not part of the main DataGrid instance', () => {
Expand Down

0 comments on commit 4bcd99b

Please sign in to comment.