Skip to content

Commit

Permalink
Lint + user column fix
Browse files Browse the repository at this point in the history
  • Loading branch information
m-rgba committed Dec 17, 2024
1 parent 134d22a commit e8ca66e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ type StyledDataGridProps = DataGridProProps & {

// Update the styled component to accept pinnedColumnsWidth
export const StyledDataGrid = styled(
({
keepBorders,
pinnedColumnsWidth,
...otherProps
}: StyledDataGridProps) => {
({keepBorders, pinnedColumnsWidth, ...otherProps}: StyledDataGridProps) => {
const slots = otherProps.slots ?? {};
if (!slots.loadingOverlay) {
slots.loadingOverlay = LoadingOverlay;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,60 +686,77 @@ export const CallsTable: FC<{

const [pinnedColumnsWidth, setPinnedColumnsWidth] = useState(0);

const calculatePinnedColumnsWidth = useCallback((pinnedFields: string[]) => {
// If columns aren't fully loaded yet, return 0
if (apiRef.current.getAllColumns().length < muiColumns.length) {
return 0;
}
let totalWidth = 0;
pinnedFields.forEach(field => {
const width = apiRef.current.getColumn(field)?.computedWidth || 100;
// console.log('calculatePinnedColumnsWidth(pinnedWidth)', field, width);
totalWidth += width;
});
return totalWidth;
}, [apiRef, muiColumns.length]);
const calculatePinnedColumnsWidth = useCallback(
(pinnedFields: string[]) => {
// If columns aren't fully loaded yet, return 0
if (apiRef.current.getAllColumns().length < muiColumns.length) {
return 0;
}
let totalWidth = 0;
pinnedFields.forEach(field => {
const width = apiRef.current.getColumn(field)?.computedWidth || 100;
// console.log('calculatePinnedColumnsWidth(pinnedWidth)', field, width);
totalWidth += width;
});
return totalWidth;
},
[apiRef, muiColumns.length]
);

// Add this effect to recalculate width when columns are ready
useEffect(() => {
const handleColumnsChange = () => {
if (apiRef.current.getAllColumns().length === muiColumns.length) {
const newWidth = calculatePinnedColumnsWidth(pinModelResolved.left || []);
const newWidth = calculatePinnedColumnsWidth(
pinModelResolved.left || []
);
setPinnedColumnsWidth(newWidth);
}
};

const unsubscribe = apiRef.current.subscribeEvent('columnsChange', handleColumnsChange);
const unsubscribe = apiRef.current.subscribeEvent(
'columnsChange',
handleColumnsChange
);
return () => {
unsubscribe();
};
}, [apiRef, calculatePinnedColumnsWidth, muiColumns.length, pinModelResolved.left]);

const handleColumnWidthChange = useCallback((newCol: any) => {
setUserDefinedColumnWidths(curr => {
const newWidths = {
...curr,
[newCol.colDef.field]: newCol.colDef.computedWidth,
};
// console.log('setUserDefinedColumnWidths(widthChange)', newWidths);
return newWidths;
});

// Recalculate pinned columns width if the changed column is pinned
if (pinModelResolved.left?.includes(newCol.colDef.field)) {
const newWidth = calculatePinnedColumnsWidth(pinModelResolved.left);
// console.log('setPinnedColumnsWidth(widthChange)', newWidth);
setPinnedColumnsWidth(newWidth);
}
}, [calculatePinnedColumnsWidth, pinModelResolved.left]);
}, [
apiRef,
calculatePinnedColumnsWidth,
muiColumns.length,
pinModelResolved.left,
]);

const handleColumnWidthChange = useCallback(
(newCol: any) => {
setUserDefinedColumnWidths(curr => {
const newWidths = {
...curr,
[newCol.colDef.field]: newCol.colDef.computedWidth,
};
return newWidths;
});
// Recalculate pinned columns width if the changed column is pinned
if (pinModelResolved.left?.includes(newCol.colDef.field)) {
const newWidth = calculatePinnedColumnsWidth(pinModelResolved.left);
setPinnedColumnsWidth(newWidth);
}
},
[
calculatePinnedColumnsWidth,
pinModelResolved.left,
setUserDefinedColumnWidths,
]
);

const onPinnedColumnsChange = useCallback(
(newModel: GridPinnedColumnFields) => {
if (!setPinModel || callsLoading) {
return;
}
setPinModel(newModel);

// Use requestAnimationFrame to ensure the DOM has updated
requestAnimationFrame(() => {
const newWidth = calculatePinnedColumnsWidth(newModel.left || []);
Expand All @@ -752,7 +769,9 @@ export const CallsTable: FC<{

// Update the initial pinned columns width when the component mounts
useEffect(() => {
const initialWidth = calculatePinnedColumnsWidth(pinModelResolved.left || []);
const initialWidth = calculatePinnedColumnsWidth(
pinModelResolved.left || []
);
setPinnedColumnsWidth(initialWidth);
}, [calculatePinnedColumnsWidth, pinModelResolved.left]);

Expand Down

0 comments on commit e8ca66e

Please sign in to comment.