@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/step-ag-grid.esm.js
CHANGED
|
@@ -64935,6 +64935,93 @@ const useGridContextMenu = ({
|
|
|
64935
64935
|
};
|
|
64936
64936
|
};
|
|
64937
64937
|
|
|
64938
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
64939
|
+
const subscribeStorageKey = (key, listener) => {
|
|
64940
|
+
const keyListeners = listeners.get(key) ?? /* @__PURE__ */ new Set();
|
|
64941
|
+
listeners.set(key, keyListeners);
|
|
64942
|
+
keyListeners.add(listener);
|
|
64943
|
+
return () => {
|
|
64944
|
+
keyListeners.delete(listener);
|
|
64945
|
+
if (keyListeners.size === 0) listeners.delete(key);
|
|
64946
|
+
};
|
|
64947
|
+
};
|
|
64948
|
+
const publishStorageKey = (storage, key, value, source) => {
|
|
64949
|
+
try {
|
|
64950
|
+
storage.setItem(key, value);
|
|
64951
|
+
} catch (ex) {
|
|
64952
|
+
console.error(ex);
|
|
64953
|
+
}
|
|
64954
|
+
listeners.get(key)?.forEach((listener) => listener !== source && listener(value));
|
|
64955
|
+
};
|
|
64956
|
+
|
|
64957
|
+
const columnStateStorageKey = (colIds) => isEmpty(colIds) ? null : "stepAgGrid_columnState_" + colIds.join("_");
|
|
64958
|
+
const toStoredColumnState = (columnState, userSizedColIds) => ({
|
|
64959
|
+
columns: columnState.map(({ colId, width, flex, sort, sortIndex }) => ({ colId, width, flex, sort, sortIndex })),
|
|
64960
|
+
userSizedColIds: [...userSizedColIds]
|
|
64961
|
+
});
|
|
64962
|
+
const parseStoredColumnState = (stored) => {
|
|
64963
|
+
if (!stored) return void 0;
|
|
64964
|
+
try {
|
|
64965
|
+
const parsed = JSON.parse(stored);
|
|
64966
|
+
if (Array.isArray(parsed?.columns) && parsed.columns.every((col) => typeof col?.colId === "string") && Array.isArray(parsed.userSizedColIds) && parsed.userSizedColIds.every((id) => typeof id === "string")) {
|
|
64967
|
+
return parsed;
|
|
64968
|
+
}
|
|
64969
|
+
console.error(`stored column state is invalid: ${stored}`);
|
|
64970
|
+
} catch (ex) {
|
|
64971
|
+
console.error(ex);
|
|
64972
|
+
}
|
|
64973
|
+
return void 0;
|
|
64974
|
+
};
|
|
64975
|
+
const applyStoredColumnState = (api, { columns, userSizedColIds: storedUserSizedColIds }, userSizedColIds) => {
|
|
64976
|
+
const widths = new Map(columns.map((col) => [col.colId, col.width]));
|
|
64977
|
+
storedUserSizedColIds.forEach((colId) => {
|
|
64978
|
+
const width = widths.get(colId);
|
|
64979
|
+
if (width != null) {
|
|
64980
|
+
userSizedColIds.set(colId, width);
|
|
64981
|
+
}
|
|
64982
|
+
});
|
|
64983
|
+
api.applyColumnState({ state: columns, applyOrder: false });
|
|
64984
|
+
};
|
|
64985
|
+
const useGridColumnStateStorage = ({
|
|
64986
|
+
enabled,
|
|
64987
|
+
gridApi,
|
|
64988
|
+
gridDivRef,
|
|
64989
|
+
userSizedColIds
|
|
64990
|
+
}) => {
|
|
64991
|
+
const { getColumnIds } = useGridContext();
|
|
64992
|
+
const storageKey = useMemo(() => enabled ? columnStateStorageKey(getColumnIds()) : null, [enabled, getColumnIds]);
|
|
64993
|
+
const getStorage = useCallback(() => gridDivRef.current?.ownerDocument.defaultView?.localStorage, [gridDivRef]);
|
|
64994
|
+
const apply = useCallback(
|
|
64995
|
+
(stored) => {
|
|
64996
|
+
const columnState = parseStoredColumnState(stored);
|
|
64997
|
+
if (gridApi && !gridApi.isDestroyed() && columnState) {
|
|
64998
|
+
applyStoredColumnState(gridApi, columnState, userSizedColIds.current);
|
|
64999
|
+
}
|
|
65000
|
+
},
|
|
65001
|
+
[gridApi, userSizedColIds]
|
|
65002
|
+
);
|
|
65003
|
+
const listenerRef = useRef(void 0);
|
|
65004
|
+
useEffect(() => {
|
|
65005
|
+
if (!storageKey) return;
|
|
65006
|
+
const listener = (value) => apply(value);
|
|
65007
|
+
listenerRef.current = listener;
|
|
65008
|
+
if (!isChromatic()) {
|
|
65009
|
+
apply(getStorage()?.getItem(storageKey) ?? null);
|
|
65010
|
+
}
|
|
65011
|
+
return subscribeStorageKey(storageKey, listener);
|
|
65012
|
+
}, [apply, getStorage, storageKey]);
|
|
65013
|
+
const saveColumnState = useCallback(
|
|
65014
|
+
(api) => {
|
|
65015
|
+
const storage = getStorage();
|
|
65016
|
+
if (!storageKey || !storage) return;
|
|
65017
|
+
const value = JSON.stringify(toStoredColumnState(api.getColumnState(), userSizedColIds.current.keys()));
|
|
65018
|
+
publishStorageKey(storage, storageKey, value, listenerRef.current);
|
|
65019
|
+
},
|
|
65020
|
+
[getStorage, storageKey, userSizedColIds]
|
|
65021
|
+
);
|
|
65022
|
+
return { saveColumnState };
|
|
65023
|
+
};
|
|
65024
|
+
|
|
64938
65025
|
const typedEntries = (obj) => Object.entries(obj);
|
|
64939
65026
|
|
|
64940
65027
|
const gridCopyOptions = {
|
|
@@ -65679,6 +65766,7 @@ const Grid = ({
|
|
|
65679
65766
|
// ─── Callbacks / Events ───────────────────────
|
|
65680
65767
|
onCellFocused: paramsOnCellFocused,
|
|
65681
65768
|
onColumnMoved,
|
|
65769
|
+
saveColumnState = false,
|
|
65682
65770
|
// ─── Row Behavior ─────────────────────────────
|
|
65683
65771
|
suppressColumnVirtualization = true,
|
|
65684
65772
|
// ─── Miscellaneous ────────────────────────────
|
|
@@ -65719,6 +65807,7 @@ const Grid = ({
|
|
|
65719
65807
|
[propStartCellEditing]
|
|
65720
65808
|
);
|
|
65721
65809
|
const { updatedDep, anyUpdating, updatingCols } = useContext(GridUpdatingContext);
|
|
65810
|
+
const [gridApi, setGridApi] = useState();
|
|
65722
65811
|
const gridDivRef = useRef(null);
|
|
65723
65812
|
const lastSelectedIds = useRef(void 0);
|
|
65724
65813
|
const [staleGrid, setStaleGrid] = useState(false);
|
|
@@ -65938,6 +66027,7 @@ const Grid = ({
|
|
|
65938
66027
|
const onGridReady = useCallback(
|
|
65939
66028
|
(event) => {
|
|
65940
66029
|
setApis(event.api, hasExternallySelectedItems, enableMultilineBulkEdit, dataTestId);
|
|
66030
|
+
setGridApi(event.api);
|
|
65941
66031
|
event.api.showNoRowsOverlay();
|
|
65942
66032
|
synchroniseExternallySelectedItemsToGrid();
|
|
65943
66033
|
},
|
|
@@ -66059,10 +66149,23 @@ const Grid = ({
|
|
|
66059
66149
|
hasSelectedMoreThanOneCellRef,
|
|
66060
66150
|
cellContextMenu
|
|
66061
66151
|
});
|
|
66062
|
-
const
|
|
66063
|
-
|
|
66064
|
-
|
|
66065
|
-
|
|
66152
|
+
const userSizedColIds = useRef(/* @__PURE__ */ new Map());
|
|
66153
|
+
const { saveColumnState: saveColumnStateToStorage } = useGridColumnStateStorage({
|
|
66154
|
+
enabled: saveColumnState,
|
|
66155
|
+
gridApi,
|
|
66156
|
+
gridDivRef,
|
|
66157
|
+
userSizedColIds
|
|
66158
|
+
});
|
|
66159
|
+
const onSortChanged = useCallback(
|
|
66160
|
+
(e) => {
|
|
66161
|
+
clearRangeSelection();
|
|
66162
|
+
ensureSelectedRowIsVisible();
|
|
66163
|
+
if (e.source === "uiColumnSorted") {
|
|
66164
|
+
saveColumnStateToStorage(e.api);
|
|
66165
|
+
}
|
|
66166
|
+
},
|
|
66167
|
+
[clearRangeSelection, ensureSelectedRowIsVisible, saveColumnStateToStorage]
|
|
66168
|
+
);
|
|
66066
66169
|
const onCellClicked = useCallback(
|
|
66067
66170
|
(event) => {
|
|
66068
66171
|
if (enableRangeSelection && rangeStartRef.current && rangeEndRef.current) {
|
|
@@ -66087,25 +66190,30 @@ const Grid = ({
|
|
|
66087
66190
|
},
|
|
66088
66191
|
[clearRangeSelection, enableRangeSelection, singleClickEdit, startCellEditing]
|
|
66089
66192
|
);
|
|
66090
|
-
const
|
|
66091
|
-
|
|
66092
|
-
|
|
66093
|
-
|
|
66094
|
-
|
|
66095
|
-
|
|
66096
|
-
|
|
66097
|
-
|
|
66098
|
-
|
|
66099
|
-
|
|
66100
|
-
|
|
66101
|
-
|
|
66102
|
-
|
|
66103
|
-
|
|
66104
|
-
|
|
66105
|
-
|
|
66106
|
-
|
|
66107
|
-
|
|
66108
|
-
|
|
66193
|
+
const onColumnResized = useCallback(
|
|
66194
|
+
(e) => {
|
|
66195
|
+
const colId = e.column?.getColId();
|
|
66196
|
+
if (colId == null) {
|
|
66197
|
+
return;
|
|
66198
|
+
}
|
|
66199
|
+
const width = e.column?.getActualWidth();
|
|
66200
|
+
if (width == null) {
|
|
66201
|
+
return;
|
|
66202
|
+
}
|
|
66203
|
+
switch (e.source) {
|
|
66204
|
+
case "uiColumnResized":
|
|
66205
|
+
userSizedColIds.current.set(colId, width);
|
|
66206
|
+
if (e.finished) {
|
|
66207
|
+
saveColumnStateToStorage(e.api);
|
|
66208
|
+
}
|
|
66209
|
+
break;
|
|
66210
|
+
case "autosizeColumns":
|
|
66211
|
+
userSizedColIds.current.delete(colId);
|
|
66212
|
+
break;
|
|
66213
|
+
}
|
|
66214
|
+
},
|
|
66215
|
+
[saveColumnStateToStorage]
|
|
66216
|
+
);
|
|
66109
66217
|
const columnMoved = useCallback(
|
|
66110
66218
|
(e) => {
|
|
66111
66219
|
clearRangeSelection();
|
|
@@ -66933,6 +67041,17 @@ const GridFilterHeaderIconButton = forwardRef(
|
|
|
66933
67041
|
}
|
|
66934
67042
|
);
|
|
66935
67043
|
|
|
67044
|
+
const parseInvisibleIds = (stored) => {
|
|
67045
|
+
const invisibleIds = JSON.parse(stored);
|
|
67046
|
+
if (!Array.isArray(invisibleIds)) {
|
|
67047
|
+
console.error(`stored invisible ids not an array: ${stored}`);
|
|
67048
|
+
} else if (!invisibleIds.every((id) => typeof id === "string")) {
|
|
67049
|
+
console.error(`stored invisible ids not strings: ${stored}`);
|
|
67050
|
+
} else {
|
|
67051
|
+
return invisibleIds;
|
|
67052
|
+
}
|
|
67053
|
+
return void 0;
|
|
67054
|
+
};
|
|
66936
67055
|
const GridFilterColumnsToggle = ({ saveState = true }) => {
|
|
66937
67056
|
const [loaded, setLoaded] = useState(false);
|
|
66938
67057
|
const { getColumns, getColumnIds, invisibleColumnIds, setInvisibleColumnIds } = useContext(GridContext);
|
|
@@ -66952,14 +67071,8 @@ const GridFilterColumnsToggle = ({ saveState = true }) => {
|
|
|
66952
67071
|
if (!stored) {
|
|
66953
67072
|
resetColumns();
|
|
66954
67073
|
} else {
|
|
66955
|
-
const invisibleIds =
|
|
66956
|
-
if (
|
|
66957
|
-
console.error(`stored invisible ids not an array: ${stored}`);
|
|
66958
|
-
} else if (!invisibleIds.every((id) => typeof id === "string")) {
|
|
66959
|
-
console.error(`stored invisible ids not strings: ${stored}`);
|
|
66960
|
-
} else {
|
|
66961
|
-
if (invisibleIds) setInvisibleColumnIds(invisibleIds);
|
|
66962
|
-
}
|
|
67074
|
+
const invisibleIds = parseInvisibleIds(stored);
|
|
67075
|
+
if (invisibleIds) setInvisibleColumnIds(invisibleIds);
|
|
66963
67076
|
}
|
|
66964
67077
|
} catch (ex) {
|
|
66965
67078
|
console.error(ex);
|
|
@@ -66972,14 +67085,37 @@ const GridFilterColumnsToggle = ({ saveState = true }) => {
|
|
|
66972
67085
|
window.localStorage.setItem(columnStorageKey, JSON.stringify(invisibleColumnIds));
|
|
66973
67086
|
}
|
|
66974
67087
|
}, [columnStorageKey, invisibleColumnIds, loaded, saveState]);
|
|
67088
|
+
const listenerRef = useRef(void 0);
|
|
67089
|
+
useEffect(() => {
|
|
67090
|
+
if (!columnStorageKey || !saveState) return;
|
|
67091
|
+
const listener = (value) => {
|
|
67092
|
+
try {
|
|
67093
|
+
const invisibleIds = parseInvisibleIds(value);
|
|
67094
|
+
if (invisibleIds) setInvisibleColumnIds(invisibleIds);
|
|
67095
|
+
} catch (ex) {
|
|
67096
|
+
console.error(ex);
|
|
67097
|
+
}
|
|
67098
|
+
};
|
|
67099
|
+
listenerRef.current = listener;
|
|
67100
|
+
return subscribeStorageKey(columnStorageKey, listener);
|
|
67101
|
+
}, [columnStorageKey, saveState, setInvisibleColumnIds]);
|
|
67102
|
+
const changeInvisibleColumnIds = useCallback(
|
|
67103
|
+
(invisibleIds) => {
|
|
67104
|
+
setInvisibleColumnIds(invisibleIds);
|
|
67105
|
+
if (columnStorageKey && saveState) {
|
|
67106
|
+
publishStorageKey(window.localStorage, columnStorageKey, JSON.stringify(invisibleIds), listenerRef.current);
|
|
67107
|
+
}
|
|
67108
|
+
},
|
|
67109
|
+
[columnStorageKey, saveState, setInvisibleColumnIds]
|
|
67110
|
+
);
|
|
66975
67111
|
const toggleColumn = useCallback(
|
|
66976
67112
|
(colId) => {
|
|
66977
67113
|
if (!colId || !invisibleColumnIds) return;
|
|
66978
|
-
|
|
67114
|
+
changeInvisibleColumnIds(
|
|
66979
67115
|
invisibleColumnIds.includes(colId) ? invisibleColumnIds.filter((id) => id !== colId) : [...invisibleColumnIds, colId]
|
|
66980
67116
|
);
|
|
66981
67117
|
},
|
|
66982
|
-
[
|
|
67118
|
+
[changeInvisibleColumnIds, invisibleColumnIds]
|
|
66983
67119
|
);
|
|
66984
67120
|
const numericRegExp = /^\d+$/;
|
|
66985
67121
|
const isNonManageableColumn = (col) => col.lockVisible || col.colId == null || numericRegExp.test(col.colId);
|
|
@@ -67030,7 +67166,7 @@ const GridFilterColumnsToggle = ({ saveState = true }) => {
|
|
|
67030
67166
|
onClick: (e) => {
|
|
67031
67167
|
if (e.key !== "Tab") {
|
|
67032
67168
|
e.keepOpen = e.key !== "Enter";
|
|
67033
|
-
|
|
67169
|
+
changeInvisibleColumnIds(getColumnIds("initialHide"));
|
|
67034
67170
|
}
|
|
67035
67171
|
},
|
|
67036
67172
|
children: [
|