-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathuseGridState.ts
More file actions
99 lines (87 loc) · 2.97 KB
/
useGridState.ts
File metadata and controls
99 lines (87 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { useCallback, useMemo, useState } from 'react';
import { GridColDef, GridColumnVisibilityModel } from '@mui/x-data-grid';
export interface GridColumnSizingModel {
[field: string]: number;
}
interface GridState {
columnVisibility: GridColumnVisibilityModel;
columnSizing: GridColumnSizingModel;
}
export const useGridState = (
storageKey: string,
columns: GridColDef[],
defaultHidden: GridColumnVisibilityModel = {}
) => {
const [gridState, setGridState] = useState<GridState>(() => {
// Initialize default column visibility
const defaultVisibility = columns?.reduce((acc, col) => ({
...acc,
[col.field]: defaultHidden[col.field] === false ? false : true
}), {});
// Initialize default column sizing from column definitions
const defaultSizing = columns?.reduce((acc, col) => ({
...acc,
[col.field]: col.width || 150 // Use column width or default to 150
}), {});
// Load saved state from localStorage
const saved = localStorage.getItem(storageKey);
const savedState = saved ? JSON.parse(saved) : {};
return {
columnVisibility: {
...defaultVisibility,
...(savedState.columnVisibility || {})
},
columnSizing: savedState.columnSizing || {}
};
});
const saveToStorage = useCallback((newState: GridState) => {
localStorage.setItem(storageKey, JSON.stringify(newState));
}, [storageKey]);
const handleColumnVisibilityChange = useCallback((newModel: GridColumnVisibilityModel) => {
setGridState(prev => {
const newState = { ...prev, columnVisibility: newModel };
saveToStorage(newState);
return newState;
});
}, [saveToStorage]);
const handleColumnWidthChange = useCallback((params: any) => {
setGridState(prev => {
const newState = {
...prev,
columnSizing: {
...prev.columnSizing,
[params.field || params.colDef?.field]: params.width
}
};
saveToStorage(newState);
return newState;
});
}, [saveToStorage]);
const resetColumnSizes = useCallback(() => {
// reset to empty, restores original flex/width definition
setGridState(prev => {
const newState = { ...prev, columnSizing: {} };
saveToStorage(newState);
return newState;
});
}, [saveToStorage]);
// Memoize columns with applied widths so objects are stable between renders
const columnsWithSizing = useMemo(() => columns?.map(col => {
const userWidth = gridState.columnSizing[col.field];
if (userWidth !== undefined) {
const { flex, minWidth, ...colWithoutFlex } = col as any;
return { ...colWithoutFlex, width: userWidth };
}
return col;
}),
[columns, gridState.columnSizing]
);
return {
columnVisibility: gridState.columnVisibility,
columnSizing: gridState.columnSizing,
columnsWithSizing,
onColumnVisibilityChange: handleColumnVisibilityChange,
onColumnWidthChange: handleColumnWidthChange,
resetColumnSizes
};
};