-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
useGridDensity.tsx
90 lines (80 loc) · 3.18 KB
/
useGridDensity.tsx
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
import * as React from 'react';
import { GridDensity, GridDensityTypes } from '../../../models/gridDensity';
import { useGridLogger } from '../../utils/useGridLogger';
import { GridApiCommunity } from '../../../models/api/gridApiCommunity';
import { useGridApiMethod } from '../../utils/useGridApiMethod';
import { GridDensityApi } from '../../../models/api/gridDensityApi';
import { GridDensityState } from './densityState';
import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import { gridDensitySelector } from './densitySelector';
import { isDeepEqual } from '../../../utils/utils';
import { GridStateInitializer } from '../../utils/useGridInitializeState';
export const COMPACT_DENSITY_FACTOR = 0.7;
export const COMFORTABLE_DENSITY_FACTOR = 1.3;
// TODO v6: revise keeping headerHeight and rowHeight in state
const getUpdatedDensityState = (
newDensity: GridDensity,
newHeaderHeight: number,
newRowHeight: number,
): GridDensityState => {
switch (newDensity) {
case GridDensityTypes.Compact:
return {
value: newDensity,
headerHeight: Math.floor(newHeaderHeight * COMPACT_DENSITY_FACTOR),
rowHeight: Math.floor(newRowHeight * COMPACT_DENSITY_FACTOR),
factor: COMPACT_DENSITY_FACTOR,
};
case GridDensityTypes.Comfortable:
return {
value: newDensity,
headerHeight: Math.floor(newHeaderHeight * COMFORTABLE_DENSITY_FACTOR),
rowHeight: Math.floor(newRowHeight * COMFORTABLE_DENSITY_FACTOR),
factor: COMFORTABLE_DENSITY_FACTOR,
};
default:
return {
value: newDensity,
headerHeight: newHeaderHeight,
rowHeight: newRowHeight,
factor: 1,
};
}
};
export const densityStateInitializer: GridStateInitializer<
Pick<DataGridProcessedProps, 'density' | 'headerHeight' | 'rowHeight'>
> = (state, props) => ({
...state,
density: getUpdatedDensityState(props.density, props.headerHeight, props.rowHeight),
});
export const useGridDensity = (
apiRef: React.MutableRefObject<GridApiCommunity>,
props: Pick<DataGridProcessedProps, 'headerHeight' | 'rowHeight' | 'density'>,
): void => {
const logger = useGridLogger(apiRef, 'useDensity');
const setDensity = React.useCallback<GridDensityApi['setDensity']>(
(newDensity, newHeaderHeight = props.headerHeight, newRowHeight = props.rowHeight): void => {
logger.debug(`Set grid density to ${newDensity}`);
apiRef.current.setState((state) => {
const currentDensityState = gridDensitySelector(state);
const newDensityState = getUpdatedDensityState(newDensity, newHeaderHeight, newRowHeight);
if (isDeepEqual(currentDensityState, newDensityState)) {
return state;
}
return {
...state,
density: newDensityState,
};
});
apiRef.current.forceUpdate();
},
[logger, apiRef, props.headerHeight, props.rowHeight],
);
React.useEffect(() => {
apiRef.current.setDensity(props.density, props.headerHeight, props.rowHeight);
}, [apiRef, props.density, props.rowHeight, props.headerHeight]);
const densityApi: GridDensityApi = {
setDensity,
};
useGridApiMethod(apiRef, densityApi, 'GridDensityApi');
};