@linzjs/step-ag-grid 35.0.2 → 35.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/GridTheme.scss +4 -0
- package/dist/src/components/Grid.d.ts +3 -1
- package/dist/src/components/gridHook/useGridColumnStateStorage.d.ts +26 -0
- package/dist/src/components/gridHook/useGridColumnStateStorage.test.d.ts +1 -0
- package/dist/src/utils/storageKeySync.d.ts +6 -0
- package/dist/src/utils/storageKeySync.test.d.ts +1 -0
- package/dist/step-ag-grid.cjs +170 -34
- package/dist/step-ag-grid.cjs.map +1 -1
- package/dist/step-ag-grid.esm.js +170 -34
- package/dist/step-ag-grid.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Grid.tsx +54 -27
- package/src/components/gridFilter/GridFilterColumnsToggle.tsx +45 -12
- package/src/components/gridHook/useGridColumnStateStorage.test.tsx +127 -0
- package/src/components/gridHook/useGridColumnStateStorage.ts +114 -0
- package/src/stories/grid/GridColumnState.stories.tsx +161 -0
- package/src/styles/GridTheme.scss +4 -0
- package/src/utils/storageKeySync.test.ts +48 -0
- package/src/utils/storageKeySync.ts +24 -0
package/dist/GridTheme.scss
CHANGED
|
@@ -178,6 +178,10 @@ $grid-base-font-size: calc(#{lui.$base-font-size} - 1px);
|
|
|
178
178
|
&.ag-header-group-cell-with-group {
|
|
179
179
|
background-color: lui.$iceberg;
|
|
180
180
|
}
|
|
181
|
+
|
|
182
|
+
.ag-header-group-cell-label {
|
|
183
|
+
justify-content: center;
|
|
184
|
+
}
|
|
181
185
|
}
|
|
182
186
|
|
|
183
187
|
div.ag-cell-wrapper .ag-selection-checkbox {
|
|
@@ -41,6 +41,8 @@ export interface GridProps<TData extends GridBaseRow = GridBaseRow> {
|
|
|
41
41
|
data: TData;
|
|
42
42
|
}) => void;
|
|
43
43
|
onColumnMoved?: GridOptions['onColumnMoved'];
|
|
44
|
+
/** Save user column widths and sort, shared by every grid with the same column ids */
|
|
45
|
+
saveColumnState?: boolean;
|
|
44
46
|
onContentSize?: (props: {
|
|
45
47
|
width: number;
|
|
46
48
|
}) => void;
|
|
@@ -65,4 +67,4 @@ export interface GridProps<TData extends GridBaseRow = GridBaseRow> {
|
|
|
65
67
|
/**
|
|
66
68
|
* Wrapper for AgGrid to add commonly used functionality.
|
|
67
69
|
*/
|
|
68
|
-
export declare const Grid: <TData extends GridBaseRow = GridBaseRow>({ theme, 'data-testid': dataTestId, suppressReadOnlyStyle, defaultPostSort, rowData, rowHeight, rowSelection, sizeColumns, autoSelectFirstRow, enableRangeSelection, externalSelectedIds, externalSelectedItems, selectColumnPinned, selectable, setExternalSelectedIds, setExternalSelectedItems, singleClickEdit, enableMultilineBulkEdit, contextMenuSelectRow, contextMenu, onCellFocused: paramsOnCellFocused, onColumnMoved, suppressColumnVirtualization, maxInitialWidth, ...params }: GridProps<TData>) => ReactElement;
|
|
70
|
+
export declare const Grid: <TData extends GridBaseRow = GridBaseRow>({ theme, 'data-testid': dataTestId, suppressReadOnlyStyle, defaultPostSort, rowData, rowHeight, rowSelection, sizeColumns, autoSelectFirstRow, enableRangeSelection, externalSelectedIds, externalSelectedItems, selectColumnPinned, selectable, setExternalSelectedIds, setExternalSelectedItems, singleClickEdit, enableMultilineBulkEdit, contextMenuSelectRow, contextMenu, onCellFocused: paramsOnCellFocused, onColumnMoved, saveColumnState, suppressColumnVirtualization, maxInitialWidth, ...params }: GridProps<TData>) => ReactElement;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ColumnState, GridApi } from 'ag-grid-community';
|
|
2
|
+
import { RefObject } from 'react';
|
|
3
|
+
export type StoredColumn = Pick<ColumnState, 'colId' | 'width' | 'flex' | 'sort' | 'sortIndex'>;
|
|
4
|
+
export interface StoredColumnState {
|
|
5
|
+
columns: StoredColumn[];
|
|
6
|
+
/** Columns the user has resized, so auto-size leaves them alone */
|
|
7
|
+
userSizedColIds: string[];
|
|
8
|
+
}
|
|
9
|
+
export declare const columnStateStorageKey: (colIds: string[]) => string | null;
|
|
10
|
+
export declare const toStoredColumnState: (columnState: ColumnState[], userSizedColIds: Iterable<string>) => StoredColumnState;
|
|
11
|
+
export declare const parseStoredColumnState: (stored: string | null) => StoredColumnState | undefined;
|
|
12
|
+
/** Applies widths and sort only, so column visibility stays with GridFilterColumnsToggle */
|
|
13
|
+
export declare const applyStoredColumnState: (api: Pick<GridApi, 'applyColumnState'>, { columns, userSizedColIds: storedUserSizedColIds }: StoredColumnState, userSizedColIds: Map<string, number>) => void;
|
|
14
|
+
interface UseGridColumnStateStorageProps {
|
|
15
|
+
enabled: boolean;
|
|
16
|
+
gridApi: GridApi | undefined;
|
|
17
|
+
gridDivRef: RefObject<HTMLDivElement | null>;
|
|
18
|
+
userSizedColIds: {
|
|
19
|
+
current: Map<string, number>;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Saves column widths and sort per set of column ids, and keeps grids with the same columns in step */
|
|
23
|
+
export declare const useGridColumnStateStorage: ({ enabled, gridApi, gridDivRef, userSizedColIds, }: UseGridColumnStateStorageProps) => {
|
|
24
|
+
saveColumnState: (api: GridApi) => void;
|
|
25
|
+
};
|
|
26
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
type StorageKeyListener = (value: string) => void;
|
|
2
|
+
/** Notifies when another component publishes a new value for a localStorage key in this page, including popped-out windows */
|
|
3
|
+
export declare const subscribeStorageKey: (key: string, listener: StorageKeyListener) => (() => void);
|
|
4
|
+
/** Saves the value and passes it to every other subscriber of the key */
|
|
5
|
+
export declare const publishStorageKey: (storage: Storage, key: string, value: string, source?: StorageKeyListener) => void;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/step-ag-grid.cjs
CHANGED
|
@@ -64937,6 +64937,93 @@ const useGridContextMenu = ({
|
|
|
64937
64937
|
};
|
|
64938
64938
|
};
|
|
64939
64939
|
|
|
64940
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
64941
|
+
const subscribeStorageKey = (key, listener) => {
|
|
64942
|
+
const keyListeners = listeners.get(key) ?? /* @__PURE__ */ new Set();
|
|
64943
|
+
listeners.set(key, keyListeners);
|
|
64944
|
+
keyListeners.add(listener);
|
|
64945
|
+
return () => {
|
|
64946
|
+
keyListeners.delete(listener);
|
|
64947
|
+
if (keyListeners.size === 0) listeners.delete(key);
|
|
64948
|
+
};
|
|
64949
|
+
};
|
|
64950
|
+
const publishStorageKey = (storage, key, value, source) => {
|
|
64951
|
+
try {
|
|
64952
|
+
storage.setItem(key, value);
|
|
64953
|
+
} catch (ex) {
|
|
64954
|
+
console.error(ex);
|
|
64955
|
+
}
|
|
64956
|
+
listeners.get(key)?.forEach((listener) => listener !== source && listener(value));
|
|
64957
|
+
};
|
|
64958
|
+
|
|
64959
|
+
const columnStateStorageKey = (colIds) => lodashEs.isEmpty(colIds) ? null : "stepAgGrid_columnState_" + colIds.join("_");
|
|
64960
|
+
const toStoredColumnState = (columnState, userSizedColIds) => ({
|
|
64961
|
+
columns: columnState.map(({ colId, width, flex, sort, sortIndex }) => ({ colId, width, flex, sort, sortIndex })),
|
|
64962
|
+
userSizedColIds: [...userSizedColIds]
|
|
64963
|
+
});
|
|
64964
|
+
const parseStoredColumnState = (stored) => {
|
|
64965
|
+
if (!stored) return void 0;
|
|
64966
|
+
try {
|
|
64967
|
+
const parsed = JSON.parse(stored);
|
|
64968
|
+
if (Array.isArray(parsed?.columns) && parsed.columns.every((col) => typeof col?.colId === "string") && Array.isArray(parsed.userSizedColIds) && parsed.userSizedColIds.every((id) => typeof id === "string")) {
|
|
64969
|
+
return parsed;
|
|
64970
|
+
}
|
|
64971
|
+
console.error(`stored column state is invalid: ${stored}`);
|
|
64972
|
+
} catch (ex) {
|
|
64973
|
+
console.error(ex);
|
|
64974
|
+
}
|
|
64975
|
+
return void 0;
|
|
64976
|
+
};
|
|
64977
|
+
const applyStoredColumnState = (api, { columns, userSizedColIds: storedUserSizedColIds }, userSizedColIds) => {
|
|
64978
|
+
const widths = new Map(columns.map((col) => [col.colId, col.width]));
|
|
64979
|
+
storedUserSizedColIds.forEach((colId) => {
|
|
64980
|
+
const width = widths.get(colId);
|
|
64981
|
+
if (width != null) {
|
|
64982
|
+
userSizedColIds.set(colId, width);
|
|
64983
|
+
}
|
|
64984
|
+
});
|
|
64985
|
+
api.applyColumnState({ state: columns, applyOrder: false });
|
|
64986
|
+
};
|
|
64987
|
+
const useGridColumnStateStorage = ({
|
|
64988
|
+
enabled,
|
|
64989
|
+
gridApi,
|
|
64990
|
+
gridDivRef,
|
|
64991
|
+
userSizedColIds
|
|
64992
|
+
}) => {
|
|
64993
|
+
const { getColumnIds } = useGridContext();
|
|
64994
|
+
const storageKey = React14.useMemo(() => enabled ? columnStateStorageKey(getColumnIds()) : null, [enabled, getColumnIds]);
|
|
64995
|
+
const getStorage = React14.useCallback(() => gridDivRef.current?.ownerDocument.defaultView?.localStorage, [gridDivRef]);
|
|
64996
|
+
const apply = React14.useCallback(
|
|
64997
|
+
(stored) => {
|
|
64998
|
+
const columnState = parseStoredColumnState(stored);
|
|
64999
|
+
if (gridApi && !gridApi.isDestroyed() && columnState) {
|
|
65000
|
+
applyStoredColumnState(gridApi, columnState, userSizedColIds.current);
|
|
65001
|
+
}
|
|
65002
|
+
},
|
|
65003
|
+
[gridApi, userSizedColIds]
|
|
65004
|
+
);
|
|
65005
|
+
const listenerRef = React14.useRef(void 0);
|
|
65006
|
+
React14.useEffect(() => {
|
|
65007
|
+
if (!storageKey) return;
|
|
65008
|
+
const listener = (value) => apply(value);
|
|
65009
|
+
listenerRef.current = listener;
|
|
65010
|
+
if (!isChromatic()) {
|
|
65011
|
+
apply(getStorage()?.getItem(storageKey) ?? null);
|
|
65012
|
+
}
|
|
65013
|
+
return subscribeStorageKey(storageKey, listener);
|
|
65014
|
+
}, [apply, getStorage, storageKey]);
|
|
65015
|
+
const saveColumnState = React14.useCallback(
|
|
65016
|
+
(api) => {
|
|
65017
|
+
const storage = getStorage();
|
|
65018
|
+
if (!storageKey || !storage) return;
|
|
65019
|
+
const value = JSON.stringify(toStoredColumnState(api.getColumnState(), userSizedColIds.current.keys()));
|
|
65020
|
+
publishStorageKey(storage, storageKey, value, listenerRef.current);
|
|
65021
|
+
},
|
|
65022
|
+
[getStorage, storageKey, userSizedColIds]
|
|
65023
|
+
);
|
|
65024
|
+
return { saveColumnState };
|
|
65025
|
+
};
|
|
65026
|
+
|
|
64940
65027
|
const typedEntries = (obj) => Object.entries(obj);
|
|
64941
65028
|
|
|
64942
65029
|
const gridCopyOptions = {
|
|
@@ -65681,6 +65768,7 @@ const Grid = ({
|
|
|
65681
65768
|
// ─── Callbacks / Events ───────────────────────
|
|
65682
65769
|
onCellFocused: paramsOnCellFocused,
|
|
65683
65770
|
onColumnMoved,
|
|
65771
|
+
saveColumnState = false,
|
|
65684
65772
|
// ─── Row Behavior ─────────────────────────────
|
|
65685
65773
|
suppressColumnVirtualization = true,
|
|
65686
65774
|
// ─── Miscellaneous ────────────────────────────
|
|
@@ -65721,6 +65809,7 @@ const Grid = ({
|
|
|
65721
65809
|
[propStartCellEditing]
|
|
65722
65810
|
);
|
|
65723
65811
|
const { updatedDep, anyUpdating, updatingCols } = React14.useContext(GridUpdatingContext);
|
|
65812
|
+
const [gridApi, setGridApi] = React14.useState();
|
|
65724
65813
|
const gridDivRef = React14.useRef(null);
|
|
65725
65814
|
const lastSelectedIds = React14.useRef(void 0);
|
|
65726
65815
|
const [staleGrid, setStaleGrid] = React14.useState(false);
|
|
@@ -65940,6 +66029,7 @@ const Grid = ({
|
|
|
65940
66029
|
const onGridReady = React14.useCallback(
|
|
65941
66030
|
(event) => {
|
|
65942
66031
|
setApis(event.api, hasExternallySelectedItems, enableMultilineBulkEdit, dataTestId);
|
|
66032
|
+
setGridApi(event.api);
|
|
65943
66033
|
event.api.showNoRowsOverlay();
|
|
65944
66034
|
synchroniseExternallySelectedItemsToGrid();
|
|
65945
66035
|
},
|
|
@@ -66061,10 +66151,23 @@ const Grid = ({
|
|
|
66061
66151
|
hasSelectedMoreThanOneCellRef,
|
|
66062
66152
|
cellContextMenu
|
|
66063
66153
|
});
|
|
66064
|
-
const
|
|
66065
|
-
|
|
66066
|
-
|
|
66067
|
-
|
|
66154
|
+
const userSizedColIds = React14.useRef(/* @__PURE__ */ new Map());
|
|
66155
|
+
const { saveColumnState: saveColumnStateToStorage } = useGridColumnStateStorage({
|
|
66156
|
+
enabled: saveColumnState,
|
|
66157
|
+
gridApi,
|
|
66158
|
+
gridDivRef,
|
|
66159
|
+
userSizedColIds
|
|
66160
|
+
});
|
|
66161
|
+
const onSortChanged = React14.useCallback(
|
|
66162
|
+
(e) => {
|
|
66163
|
+
clearRangeSelection();
|
|
66164
|
+
ensureSelectedRowIsVisible();
|
|
66165
|
+
if (e.source === "uiColumnSorted") {
|
|
66166
|
+
saveColumnStateToStorage(e.api);
|
|
66167
|
+
}
|
|
66168
|
+
},
|
|
66169
|
+
[clearRangeSelection, ensureSelectedRowIsVisible, saveColumnStateToStorage]
|
|
66170
|
+
);
|
|
66068
66171
|
const onCellClicked = React14.useCallback(
|
|
66069
66172
|
(event) => {
|
|
66070
66173
|
if (enableRangeSelection && rangeStartRef.current && rangeEndRef.current) {
|
|
@@ -66089,25 +66192,30 @@ const Grid = ({
|
|
|
66089
66192
|
},
|
|
66090
66193
|
[clearRangeSelection, enableRangeSelection, singleClickEdit, startCellEditing]
|
|
66091
66194
|
);
|
|
66092
|
-
const
|
|
66093
|
-
|
|
66094
|
-
|
|
66095
|
-
|
|
66096
|
-
|
|
66097
|
-
|
|
66098
|
-
|
|
66099
|
-
|
|
66100
|
-
|
|
66101
|
-
|
|
66102
|
-
|
|
66103
|
-
|
|
66104
|
-
|
|
66105
|
-
|
|
66106
|
-
|
|
66107
|
-
|
|
66108
|
-
|
|
66109
|
-
|
|
66110
|
-
|
|
66195
|
+
const onColumnResized = React14.useCallback(
|
|
66196
|
+
(e) => {
|
|
66197
|
+
const colId = e.column?.getColId();
|
|
66198
|
+
if (colId == null) {
|
|
66199
|
+
return;
|
|
66200
|
+
}
|
|
66201
|
+
const width = e.column?.getActualWidth();
|
|
66202
|
+
if (width == null) {
|
|
66203
|
+
return;
|
|
66204
|
+
}
|
|
66205
|
+
switch (e.source) {
|
|
66206
|
+
case "uiColumnResized":
|
|
66207
|
+
userSizedColIds.current.set(colId, width);
|
|
66208
|
+
if (e.finished) {
|
|
66209
|
+
saveColumnStateToStorage(e.api);
|
|
66210
|
+
}
|
|
66211
|
+
break;
|
|
66212
|
+
case "autosizeColumns":
|
|
66213
|
+
userSizedColIds.current.delete(colId);
|
|
66214
|
+
break;
|
|
66215
|
+
}
|
|
66216
|
+
},
|
|
66217
|
+
[saveColumnStateToStorage]
|
|
66218
|
+
);
|
|
66111
66219
|
const columnMoved = React14.useCallback(
|
|
66112
66220
|
(e) => {
|
|
66113
66221
|
clearRangeSelection();
|
|
@@ -66935,6 +67043,17 @@ const GridFilterHeaderIconButton = React14.forwardRef(
|
|
|
66935
67043
|
}
|
|
66936
67044
|
);
|
|
66937
67045
|
|
|
67046
|
+
const parseInvisibleIds = (stored) => {
|
|
67047
|
+
const invisibleIds = JSON.parse(stored);
|
|
67048
|
+
if (!Array.isArray(invisibleIds)) {
|
|
67049
|
+
console.error(`stored invisible ids not an array: ${stored}`);
|
|
67050
|
+
} else if (!invisibleIds.every((id) => typeof id === "string")) {
|
|
67051
|
+
console.error(`stored invisible ids not strings: ${stored}`);
|
|
67052
|
+
} else {
|
|
67053
|
+
return invisibleIds;
|
|
67054
|
+
}
|
|
67055
|
+
return void 0;
|
|
67056
|
+
};
|
|
66938
67057
|
const GridFilterColumnsToggle = ({ saveState = true }) => {
|
|
66939
67058
|
const [loaded, setLoaded] = React14.useState(false);
|
|
66940
67059
|
const { getColumns, getColumnIds, invisibleColumnIds, setInvisibleColumnIds } = React14.useContext(GridContext);
|
|
@@ -66954,14 +67073,8 @@ const GridFilterColumnsToggle = ({ saveState = true }) => {
|
|
|
66954
67073
|
if (!stored) {
|
|
66955
67074
|
resetColumns();
|
|
66956
67075
|
} else {
|
|
66957
|
-
const invisibleIds =
|
|
66958
|
-
if (
|
|
66959
|
-
console.error(`stored invisible ids not an array: ${stored}`);
|
|
66960
|
-
} else if (!invisibleIds.every((id) => typeof id === "string")) {
|
|
66961
|
-
console.error(`stored invisible ids not strings: ${stored}`);
|
|
66962
|
-
} else {
|
|
66963
|
-
if (invisibleIds) setInvisibleColumnIds(invisibleIds);
|
|
66964
|
-
}
|
|
67076
|
+
const invisibleIds = parseInvisibleIds(stored);
|
|
67077
|
+
if (invisibleIds) setInvisibleColumnIds(invisibleIds);
|
|
66965
67078
|
}
|
|
66966
67079
|
} catch (ex) {
|
|
66967
67080
|
console.error(ex);
|
|
@@ -66974,14 +67087,37 @@ const GridFilterColumnsToggle = ({ saveState = true }) => {
|
|
|
66974
67087
|
window.localStorage.setItem(columnStorageKey, JSON.stringify(invisibleColumnIds));
|
|
66975
67088
|
}
|
|
66976
67089
|
}, [columnStorageKey, invisibleColumnIds, loaded, saveState]);
|
|
67090
|
+
const listenerRef = React14.useRef(void 0);
|
|
67091
|
+
React14.useEffect(() => {
|
|
67092
|
+
if (!columnStorageKey || !saveState) return;
|
|
67093
|
+
const listener = (value) => {
|
|
67094
|
+
try {
|
|
67095
|
+
const invisibleIds = parseInvisibleIds(value);
|
|
67096
|
+
if (invisibleIds) setInvisibleColumnIds(invisibleIds);
|
|
67097
|
+
} catch (ex) {
|
|
67098
|
+
console.error(ex);
|
|
67099
|
+
}
|
|
67100
|
+
};
|
|
67101
|
+
listenerRef.current = listener;
|
|
67102
|
+
return subscribeStorageKey(columnStorageKey, listener);
|
|
67103
|
+
}, [columnStorageKey, saveState, setInvisibleColumnIds]);
|
|
67104
|
+
const changeInvisibleColumnIds = React14.useCallback(
|
|
67105
|
+
(invisibleIds) => {
|
|
67106
|
+
setInvisibleColumnIds(invisibleIds);
|
|
67107
|
+
if (columnStorageKey && saveState) {
|
|
67108
|
+
publishStorageKey(window.localStorage, columnStorageKey, JSON.stringify(invisibleIds), listenerRef.current);
|
|
67109
|
+
}
|
|
67110
|
+
},
|
|
67111
|
+
[columnStorageKey, saveState, setInvisibleColumnIds]
|
|
67112
|
+
);
|
|
66977
67113
|
const toggleColumn = React14.useCallback(
|
|
66978
67114
|
(colId) => {
|
|
66979
67115
|
if (!colId || !invisibleColumnIds) return;
|
|
66980
|
-
|
|
67116
|
+
changeInvisibleColumnIds(
|
|
66981
67117
|
invisibleColumnIds.includes(colId) ? invisibleColumnIds.filter((id) => id !== colId) : [...invisibleColumnIds, colId]
|
|
66982
67118
|
);
|
|
66983
67119
|
},
|
|
66984
|
-
[
|
|
67120
|
+
[changeInvisibleColumnIds, invisibleColumnIds]
|
|
66985
67121
|
);
|
|
66986
67122
|
const numericRegExp = /^\d+$/;
|
|
66987
67123
|
const isNonManageableColumn = (col) => col.lockVisible || col.colId == null || numericRegExp.test(col.colId);
|
|
@@ -67032,7 +67168,7 @@ const GridFilterColumnsToggle = ({ saveState = true }) => {
|
|
|
67032
67168
|
onClick: (e) => {
|
|
67033
67169
|
if (e.key !== "Tab") {
|
|
67034
67170
|
e.keepOpen = e.key !== "Enter";
|
|
67035
|
-
|
|
67171
|
+
changeInvisibleColumnIds(getColumnIds("initialHide"));
|
|
67036
67172
|
}
|
|
67037
67173
|
},
|
|
67038
67174
|
children: [
|