@linzjs/step-ag-grid 32.7.1 → 34.0.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/src/components/PostSortRowsHook.test.d.ts +1 -0
- package/dist/src/components/gridFilter/GridFilterColumnsMultiSelect.d.ts +6 -25
- package/dist/src/components/gridForm/GridFormTextInput.d.ts +2 -1
- package/dist/src/utils/__tests__/storybookTestUtil.ts +1 -1
- package/dist/step-ag-grid.cjs +190 -206
- package/dist/step-ag-grid.cjs.map +1 -1
- package/dist/step-ag-grid.esm.js +190 -206
- package/dist/step-ag-grid.esm.js.map +1 -1
- package/package.json +17 -17
- package/src/components/Grid.tsx +8 -10
- package/src/components/PostSortRowsHook.test.ts +300 -0
- package/src/components/PostSortRowsHook.ts +127 -24
- package/src/components/gridFilter/GridFilterColumnsMultiSelect.tsx +110 -199
- package/src/components/gridForm/GridFormTextInput.tsx +2 -1
- package/src/components/gridHook/useGridRangeSelection.ts +8 -6
- package/src/contexts/GridContextProvider.tsx +5 -9
- package/src/react-menu3/components/ControlledMenu.tsx +5 -3
- package/src/react-menu3/components/MenuList.tsx +7 -3
- package/src/react-menu3/hooks/useItems.ts +1 -1
- package/src/stories/components/ActionButton.stories.tsx +12 -11
- package/src/stories/decorators/withOpenInPanelDecorator.tsx +35 -0
- package/src/stories/grid/GridCopy.stories.tsx +38 -35
- package/src/stories/grid/GridDragRow.stories.tsx +41 -36
- package/src/stories/grid/GridFilterButtons.stories.tsx +25 -19
- package/src/stories/grid/GridFilterColumnsMultiSelect.stories.tsx +42 -37
- package/src/stories/grid/GridInlineText.stories.tsx +62 -49
- package/src/stories/grid/GridNoRowsOverlay.stories.tsx +28 -23
- package/src/stories/grid/GridNonEditableRow.stories.tsx +30 -24
- package/src/stories/grid/GridPinnedRow.stories.tsx +37 -31
- package/src/stories/grid/GridPopoutContextMenu.stories.tsx +29 -24
- package/src/stories/grid/GridPopoutEditBoolean.stories.tsx +89 -82
- package/src/stories/grid/GridPopoutEditGeneric.stories.tsx +27 -22
- package/src/stories/grid/GridPopoutEditGenericTextArea.stories.tsx +29 -24
- package/src/stories/grid/GridPopoverEditBearing.stories.tsx +30 -25
- package/src/stories/grid/GridPopoverEditDropDown.stories.tsx +29 -24
- package/src/stories/grid/GridPopoverEditMultiSelect.stories.tsx +29 -24
- package/src/stories/grid/GridPopoverEditMultiSelectGrid.stories.tsx +28 -24
- package/src/stories/grid/GridReadOnly.stories.tsx +56 -52
- package/src/stories/grid/GridSorting.stories.tsx +498 -86
- package/src/stories/grid/GridViewList.stories.tsx +31 -26
- package/src/stories/grid/GridViewSelectableList.stories.tsx +31 -26
- package/src/stories/grid/gridAutosize/GridAutoSize.stories.tsx +41 -37
- package/src/stories/grid/gridAutosize/GridFitSize.stories.tsx +40 -36
- package/src/stories/grid/gridAutosize/GridFitSizeFlex.stories.tsx +40 -36
- package/src/stories/grid/gridAutosize/ShowPanelResizingAgGrid/ShowPanelResizingStepAgGrid.tsx +14 -6
- package/src/stories/grid/gridFormInteraction/GridFormDropDownInteraction.stories.tsx +74 -74
- package/src/stories/grid/gridFormInteraction/GridFormEditBearingCorrectionInteraction.stories.tsx +59 -59
- package/src/stories/grid/gridFormInteraction/GridFormEditBearingInteraction.stories.tsx +58 -58
- package/src/stories/grid/gridFormInteraction/GridFormMultiSelectGridInteraction.stories.tsx +62 -62
- package/src/stories/grid/gridFormInteraction/GridFormMultiSelectInteraction.stories.tsx +118 -118
- package/src/stories/grid/gridFormInteraction/GridFormPopoverMenuInteraction.stories.tsx +107 -105
- package/src/stories/grid/gridFormInteraction/GridFormTextAreaInteraction.stories.tsx +72 -64
- package/src/stories/grid/gridFormInteraction/GridFormTextInputInteraction.stories.tsx +82 -73
- package/src/stories/grid/gridFormStatic/GridFormDropDown.stories.tsx +12 -13
- package/src/stories/grid/gridFormStatic/GridFormEditBearing.stories.tsx +12 -13
- package/src/stories/grid/gridFormStatic/GridFormEditBearingCorrection.stories.tsx +11 -13
- package/src/stories/grid/gridFormStatic/GridFormMessage.stories.tsx +11 -13
- package/src/stories/grid/gridFormStatic/GridFormMultiSelect.stories.tsx +11 -13
- package/src/stories/grid/gridFormStatic/GridFormPopoverMenu.stories.tsx +11 -13
- package/src/stories/grid/gridFormStatic/GridFormTextArea.stories.tsx +11 -13
- package/src/stories/grid/gridFormStatic/GridFormTextInput.stories.tsx +11 -13
- package/src/stories/grid/interactions/GridKeyboardInteractions.stories.tsx +110 -107
- package/src/stories/react-menu/ReactMenu.stories.tsx +70 -73
- package/src/styles/GridLoadableCell.scss +1 -1
- package/src/utils/__tests__/storybookTestUtil.ts +1 -1
- package/src/utils/useSharedInterval.ts +3 -3
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@linzjs/step-ag-grid",
|
|
3
3
|
"repository": "github:linz/step-ag-grid.git",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "
|
|
5
|
+
"version": "34.0.0",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"aggrid",
|
|
8
8
|
"ag-grid",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"build-storybook": "storybook build",
|
|
60
60
|
"deploy-storybook": "npx --yes -p @storybook/storybook-deployer storybook-to-ghpages",
|
|
61
61
|
"chromatic": "chromatic --exit-zero-on-changes",
|
|
62
|
-
"update": "npx npm-check-updates -i --format group -c
|
|
62
|
+
"update": "npx npm-check-updates -i --format group -c 7d",
|
|
63
63
|
"prepare": "husky"
|
|
64
64
|
},
|
|
65
65
|
"browserslist": {
|
|
@@ -75,19 +75,19 @@
|
|
|
75
75
|
]
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@chromatic-com/storybook": "^5.3.
|
|
79
|
-
"@linzjs/lui": "^
|
|
80
|
-
"@linzjs/windows": "^
|
|
78
|
+
"@chromatic-com/storybook": "^5.3.1",
|
|
79
|
+
"@linzjs/lui": "^27.0.0",
|
|
80
|
+
"@linzjs/windows": "^16.1.0",
|
|
81
81
|
"@rollup/plugin-commonjs": "^29.0.3",
|
|
82
82
|
"@rollup/plugin-json": "^6.1.0",
|
|
83
83
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
84
84
|
"rollup-plugin-esbuild": "^6.2.1",
|
|
85
|
-
"@storybook/addon-docs": "^10.
|
|
86
|
-
"@storybook/addon-links": "^10.
|
|
87
|
-
"@storybook/react-vite": "^10.
|
|
85
|
+
"@storybook/addon-docs": "^10.6.0",
|
|
86
|
+
"@storybook/addon-links": "^10.6.0",
|
|
87
|
+
"@storybook/react-vite": "^10.6.0",
|
|
88
88
|
"@testing-library/dom": "^10.4.1",
|
|
89
|
-
"@testing-library/react": "16.3.
|
|
90
|
-
"@testing-library/user-event": "^14.6.
|
|
89
|
+
"@testing-library/react": "16.3.3",
|
|
90
|
+
"@testing-library/user-event": "^14.6.7",
|
|
91
91
|
"@types/debounce-promise": "^3.1.9",
|
|
92
92
|
"@types/jest": "^30.0.0",
|
|
93
93
|
"@types/jsdom": "^28.0.3",
|
|
@@ -95,7 +95,7 @@
|
|
|
95
95
|
"@types/node": "^24.13.3",
|
|
96
96
|
"@types/react": "^18.3.31",
|
|
97
97
|
"@types/react-dom": "^18.3.7",
|
|
98
|
-
"@vitejs/plugin-react": "6.
|
|
98
|
+
"@vitejs/plugin-react": "6.1.1",
|
|
99
99
|
"@vitest/ui": "^4.1.11",
|
|
100
100
|
"chromatic": "^17.8.0",
|
|
101
101
|
"cross-env": "^10.1.0",
|
|
@@ -103,18 +103,18 @@
|
|
|
103
103
|
"jsdom": "29.1.1",
|
|
104
104
|
"lodash-es": "^4.18.1",
|
|
105
105
|
"npm-run-all": "^4.1.5",
|
|
106
|
-
"oxfmt": "^0.
|
|
107
|
-
"oxlint": "^1.
|
|
108
|
-
"postcss": "^8.5.
|
|
106
|
+
"oxfmt": "^0.67.0",
|
|
107
|
+
"oxlint": "^1.82.0",
|
|
108
|
+
"postcss": "^8.5.28",
|
|
109
109
|
"postcss-scss": "^4.0.9",
|
|
110
110
|
"react": ">=19",
|
|
111
111
|
"react-dom": "^19.2.8",
|
|
112
|
-
"rollup": "4.
|
|
112
|
+
"rollup": "4.63.1",
|
|
113
113
|
"rollup-plugin-copy": "^3.5.0",
|
|
114
114
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
115
115
|
"rollup-plugin-postcss": "^4.0.2",
|
|
116
|
-
"sass": "^1.
|
|
117
|
-
"storybook": "^10.
|
|
116
|
+
"sass": "^1.104.0",
|
|
117
|
+
"storybook": "^10.6.0",
|
|
118
118
|
"typescript": "^7.0.2",
|
|
119
119
|
"vite": "^8.2.2",
|
|
120
120
|
"vitest": "^4.1.11"
|
package/src/components/Grid.tsx
CHANGED
|
@@ -601,7 +601,7 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
601
601
|
if (sizeColumns === 'fit') {
|
|
602
602
|
delay(() => {
|
|
603
603
|
// Don't update if currently editing, that will stop the edit
|
|
604
|
-
if (!anyUpdating() &&
|
|
604
|
+
if (!anyUpdating() && !gridDivRef.current?.ownerDocument.querySelectorAll('.szh-menu--state-open').length) {
|
|
605
605
|
if (!api.isDestroyed()) {
|
|
606
606
|
api.redrawRows();
|
|
607
607
|
}
|
|
@@ -862,24 +862,22 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
862
862
|
|
|
863
863
|
const startDragYRef = useRef<number | null>(null);
|
|
864
864
|
|
|
865
|
-
const getGridRoot = useCallback(() => gridDivRef.current ?? document, []);
|
|
866
|
-
|
|
867
865
|
const clearHighlightRowClasses = useCallback(() => {
|
|
868
|
-
const gridRoot =
|
|
869
|
-
gridRoot
|
|
866
|
+
const gridRoot = gridDivRef.current;
|
|
867
|
+
gridRoot?.querySelectorAll(`.ag-row-highlight-above`)?.forEach((el) => {
|
|
870
868
|
el.classList.remove('ag-row-highlight-above');
|
|
871
869
|
});
|
|
872
|
-
gridRoot
|
|
870
|
+
gridRoot?.querySelectorAll(`.ag-row-highlight-below`)?.forEach((el) => {
|
|
873
871
|
el.classList.remove('ag-row-highlight-below');
|
|
874
872
|
});
|
|
875
|
-
}, [
|
|
873
|
+
}, []);
|
|
876
874
|
|
|
877
875
|
const clearDragRowClasses = useCallback(() => {
|
|
878
|
-
const gridRoot =
|
|
879
|
-
gridRoot
|
|
876
|
+
const gridRoot = gridDivRef.current;
|
|
877
|
+
gridRoot?.querySelectorAll(`.ag-row-dragging`)?.forEach((el) => {
|
|
880
878
|
el.classList.remove('ag-row-dragging');
|
|
881
879
|
});
|
|
882
|
-
}, [
|
|
880
|
+
}, []);
|
|
883
881
|
|
|
884
882
|
const gridElementRef = useRef<Element>(undefined);
|
|
885
883
|
const multiDragAppliedRef = useRef(false);
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { usePostSortRowsHook } from './PostSortRowsHook';
|
|
5
|
+
|
|
6
|
+
const makeApi = (
|
|
7
|
+
columnState: { colId: string; sort?: string | null; sortIndex?: number | null }[] = [],
|
|
8
|
+
colDefs: Record<
|
|
9
|
+
string,
|
|
10
|
+
{
|
|
11
|
+
equals?: (a: unknown, b: unknown) => boolean;
|
|
12
|
+
comparator?: (a: unknown, b: unknown, nodeA: unknown, nodeB: unknown, isDescending: boolean) => number;
|
|
13
|
+
}
|
|
14
|
+
> = {},
|
|
15
|
+
) => ({
|
|
16
|
+
isDestroyed: () => false,
|
|
17
|
+
getColumnState: () => columnState,
|
|
18
|
+
getQuickFilter: () => undefined,
|
|
19
|
+
getCellValue: ({ rowNode, colKey }: any) => rowNode.data[colKey],
|
|
20
|
+
getColumn: (colId: string) => ({ getColDef: () => colDefs[colId] ?? {} }),
|
|
21
|
+
applyColumnState: vi.fn(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const makeNode = (data: unknown) => ({ data }) as any;
|
|
25
|
+
|
|
26
|
+
const sortedByName = [{ colId: 'name', sort: 'asc', sortIndex: 0 }];
|
|
27
|
+
|
|
28
|
+
describe('usePostSortRowsHook - sorted-column value comparison', () => {
|
|
29
|
+
test('does not throw when a row holds a self-reference, even unsorted', () => {
|
|
30
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid: vi.fn() }));
|
|
31
|
+
const postSortRows = result.current;
|
|
32
|
+
|
|
33
|
+
const selfRefData: any = { id: '1', name: 'a' };
|
|
34
|
+
selfRefData.self = selfRefData; // circular reference, not part of the sort
|
|
35
|
+
|
|
36
|
+
const nodes = [makeNode(selfRefData), makeNode({ id: '2', name: 'b' })];
|
|
37
|
+
|
|
38
|
+
expect(() => postSortRows({ api: makeApi(sortedByName) as any, nodes } as any)).not.toThrow();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('does not throw when the sorted column value itself is self-referential', () => {
|
|
42
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid: vi.fn() }));
|
|
43
|
+
const postSortRows = result.current;
|
|
44
|
+
|
|
45
|
+
const circularName: any = { label: 'a' };
|
|
46
|
+
circularName.self = circularName;
|
|
47
|
+
const data: any = { id: '1', name: circularName };
|
|
48
|
+
|
|
49
|
+
const nodes = [makeNode(data), makeNode({ id: '2', name: { label: 'b' } })];
|
|
50
|
+
|
|
51
|
+
expect(() => postSortRows({ api: makeApi(sortedByName) as any, nodes } as any)).not.toThrow();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('detects an edit to the sorted column value', () => {
|
|
55
|
+
const setStaleGrid = vi.fn();
|
|
56
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid }));
|
|
57
|
+
const postSortRows = result.current;
|
|
58
|
+
|
|
59
|
+
const api = makeApi(sortedByName);
|
|
60
|
+
const rowA: any = { id: '1', name: 'a' };
|
|
61
|
+
const rowB: any = { id: '2', name: 'b' };
|
|
62
|
+
const nodes = [makeNode(rowA), makeNode(rowB)];
|
|
63
|
+
|
|
64
|
+
// First call backs up the initial hash/order.
|
|
65
|
+
postSortRows({ api: api as any, nodes } as any);
|
|
66
|
+
setStaleGrid.mockClear();
|
|
67
|
+
|
|
68
|
+
// Same data again: nothing changed.
|
|
69
|
+
postSortRows({ api: api as any, nodes } as any);
|
|
70
|
+
expect(setStaleGrid).not.toHaveBeenCalledWith(true);
|
|
71
|
+
|
|
72
|
+
// Edit the sorted field - AG Grid hands back the naturally re-sorted (rowA now after rowB)
|
|
73
|
+
// array, which is what should get flagged as stale (and reverted to the original order).
|
|
74
|
+
rowA.name = 'z';
|
|
75
|
+
const editedNodes = [makeNode(rowB), makeNode(rowA)];
|
|
76
|
+
postSortRows({ api: api as any, nodes: editedNodes } as any);
|
|
77
|
+
|
|
78
|
+
expect(setStaleGrid).toHaveBeenCalledWith(true);
|
|
79
|
+
expect(editedNodes.map((node) => node.data.id)).toEqual(['1', '2']);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('editing an unsorted field does not mark the sort stale', () => {
|
|
83
|
+
const setStaleGrid = vi.fn();
|
|
84
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid }));
|
|
85
|
+
const postSortRows = result.current;
|
|
86
|
+
|
|
87
|
+
const api = makeApi(sortedByName);
|
|
88
|
+
const rowA: any = { id: '1', name: 'a', other: 'x' };
|
|
89
|
+
const rowB: any = { id: '2', name: 'b', other: 'y' };
|
|
90
|
+
const nodes = [makeNode(rowA), makeNode(rowB)];
|
|
91
|
+
|
|
92
|
+
postSortRows({ api: api as any, nodes } as any);
|
|
93
|
+
setStaleGrid.mockClear();
|
|
94
|
+
|
|
95
|
+
// Edit a field that isn't part of the sort - shouldn't flag the sort as stale.
|
|
96
|
+
rowA.other = 'x-edited';
|
|
97
|
+
postSortRows({ api: api as any, nodes } as any);
|
|
98
|
+
|
|
99
|
+
expect(setStaleGrid).not.toHaveBeenCalledWith(true);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('respects a custom colDef.equals on the sorted column', () => {
|
|
103
|
+
const setStaleGrid = vi.fn();
|
|
104
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid }));
|
|
105
|
+
const postSortRows = result.current;
|
|
106
|
+
|
|
107
|
+
// Case-insensitive equality for the sorted column.
|
|
108
|
+
const api = makeApi(sortedByName, {
|
|
109
|
+
name: { equals: (a, b) => `${a}`.toLowerCase() === `${b}`.toLowerCase() },
|
|
110
|
+
});
|
|
111
|
+
const rowA: any = { id: '1', name: 'a' };
|
|
112
|
+
const rowB: any = { id: '2', name: 'b' };
|
|
113
|
+
|
|
114
|
+
// Initial natural sort order: a, b.
|
|
115
|
+
postSortRows({ api: api as any, nodes: [makeNode(rowA), makeNode(rowB)] } as any);
|
|
116
|
+
setStaleGrid.mockClear();
|
|
117
|
+
|
|
118
|
+
// Only a case change - still naturally sorts the same way - custom equals says this is unchanged.
|
|
119
|
+
rowA.name = 'A';
|
|
120
|
+
postSortRows({ api: api as any, nodes: [makeNode(rowA), makeNode(rowB)] } as any);
|
|
121
|
+
expect(setStaleGrid).not.toHaveBeenCalledWith(true);
|
|
122
|
+
|
|
123
|
+
// A real value change that would naturally re-sort rowA after rowB - AG Grid hands back nodes in
|
|
124
|
+
// that new natural order, which is what should get flagged as stale (and reverted to old order).
|
|
125
|
+
rowA.name = 'z';
|
|
126
|
+
const thirdCallNodes = [makeNode(rowB), makeNode(rowA)];
|
|
127
|
+
postSortRows({ api: api as any, nodes: thirdCallNodes } as any);
|
|
128
|
+
expect(setStaleGrid).toHaveBeenCalledWith(true);
|
|
129
|
+
expect(thirdCallNodes.map((n) => n.data.id)).toEqual(['1', '2']);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('falls back to colDef.comparator when there is no equals', () => {
|
|
133
|
+
const setStaleGrid = vi.fn();
|
|
134
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid }));
|
|
135
|
+
const postSortRows = result.current;
|
|
136
|
+
|
|
137
|
+
// Comparator sorts by the value's length only - two values of the same length are "equal".
|
|
138
|
+
const api = makeApi(sortedByName, {
|
|
139
|
+
name: { comparator: (a, b) => `${a}`.length - `${b}`.length },
|
|
140
|
+
});
|
|
141
|
+
const rowA: any = { id: '1', name: 'aa' };
|
|
142
|
+
const rowB: any = { id: '2', name: 'bbb' };
|
|
143
|
+
|
|
144
|
+
postSortRows({ api: api as any, nodes: [makeNode(rowA), makeNode(rowB)] } as any);
|
|
145
|
+
setStaleGrid.mockClear();
|
|
146
|
+
|
|
147
|
+
// Different characters, same length - the comparator says this is unchanged.
|
|
148
|
+
rowA.name = 'zz';
|
|
149
|
+
postSortRows({ api: api as any, nodes: [makeNode(rowA), makeNode(rowB)] } as any);
|
|
150
|
+
expect(setStaleGrid).not.toHaveBeenCalledWith(true);
|
|
151
|
+
|
|
152
|
+
// A length change - the comparator says this really is different, and naturally re-sorts rowA
|
|
153
|
+
// after rowB.
|
|
154
|
+
rowA.name = 'zzzz';
|
|
155
|
+
const thirdCallNodes = [makeNode(rowB), makeNode(rowA)];
|
|
156
|
+
postSortRows({ api: api as any, nodes: thirdCallNodes } as any);
|
|
157
|
+
expect(setStaleGrid).toHaveBeenCalledWith(true);
|
|
158
|
+
expect(thirdCallNodes.map((n) => n.data.id)).toEqual(['1', '2']);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('comparator receives the pre-edit snapshot node and the current live node', () => {
|
|
162
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid: vi.fn() }));
|
|
163
|
+
const postSortRows = result.current;
|
|
164
|
+
|
|
165
|
+
// Ignores the raw values entirely and only reads nodeA/nodeB, to prove which node objects
|
|
166
|
+
// the hook actually passes through when checking a row's own previous value against its
|
|
167
|
+
// current one.
|
|
168
|
+
const comparator = vi.fn((_a, _b, nodeA: any, nodeB: any) => (nodeA.data.name === nodeB.data.name ? 0 : 1));
|
|
169
|
+
const api = makeApi(sortedByName, { name: { comparator } });
|
|
170
|
+
const rowA: any = { id: '1', name: 'aa' };
|
|
171
|
+
const rowB: any = { id: '2', name: 'bbb' };
|
|
172
|
+
|
|
173
|
+
// First call backs up rowA's pre-edit data as the snapshot node.
|
|
174
|
+
postSortRows({ api: api as any, nodes: [makeNode(rowA), makeNode(rowB)] } as any);
|
|
175
|
+
comparator.mockClear();
|
|
176
|
+
|
|
177
|
+
// The row's data object is replaced (not mutated in place) - realistic for immutable-style
|
|
178
|
+
// updates, and the only way to distinguish the snapshot node from the current live node, since
|
|
179
|
+
// the hook's snapshot wraps the data object by reference rather than deep-cloning it.
|
|
180
|
+
const rowAEdited: any = { id: '1', name: 'zz' };
|
|
181
|
+
postSortRows({ api: api as any, nodes: [makeNode(rowAEdited), makeNode(rowB)] } as any);
|
|
182
|
+
|
|
183
|
+
// First call is for row 1: its own previous value vs current value.
|
|
184
|
+
expect(comparator).toHaveBeenCalled();
|
|
185
|
+
const [, , nodeA, nodeB] = comparator.mock.calls[0];
|
|
186
|
+
// nodeA is the frozen pre-edit snapshot ("aa"); nodeB is the current live node ("zz").
|
|
187
|
+
expect((nodeA as any).data.name).toBe('aa');
|
|
188
|
+
expect((nodeB as any).data.name).toBe('zz');
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test('prefers equals over comparator when a column defines both', () => {
|
|
192
|
+
const setStaleGrid = vi.fn();
|
|
193
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid }));
|
|
194
|
+
const postSortRows = result.current;
|
|
195
|
+
|
|
196
|
+
const comparator = vi.fn((a, b) => `${a}`.length - `${b}`.length);
|
|
197
|
+
const api = makeApi(sortedByName, {
|
|
198
|
+
name: { equals: (a, b) => a === b, comparator },
|
|
199
|
+
});
|
|
200
|
+
const rowA: any = { id: '1', name: 'aa' };
|
|
201
|
+
const rowB: any = { id: '2', name: 'bbb' };
|
|
202
|
+
|
|
203
|
+
postSortRows({ api: api as any, nodes: [makeNode(rowA), makeNode(rowB)] } as any);
|
|
204
|
+
setStaleGrid.mockClear();
|
|
205
|
+
comparator.mockClear();
|
|
206
|
+
|
|
207
|
+
// Same length, different value - equals (===) says changed; comparator would have said unchanged.
|
|
208
|
+
// AG Grid hands back the naturally re-sorted order for this genuine change.
|
|
209
|
+
rowA.name = 'zz';
|
|
210
|
+
const thirdCallNodes = [makeNode(rowB), makeNode(rowA)];
|
|
211
|
+
postSortRows({ api: api as any, nodes: thirdCallNodes } as any);
|
|
212
|
+
|
|
213
|
+
expect(comparator).not.toHaveBeenCalled();
|
|
214
|
+
expect(setStaleGrid).toHaveBeenCalledWith(true);
|
|
215
|
+
expect(thirdCallNodes.map((n) => n.data.id)).toEqual(['1', '2']);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('with no sort configured, retains prior array order when an update reorders the same rows', () => {
|
|
219
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid: vi.fn() }));
|
|
220
|
+
const postSortRows = result.current;
|
|
221
|
+
|
|
222
|
+
const api = makeApi([]); // no column sorted - "sort" is just array order
|
|
223
|
+
const rowA = { id: '1', name: 'a' };
|
|
224
|
+
const rowB = { id: '2', name: 'b' };
|
|
225
|
+
const rowC = { id: '3', name: 'c' };
|
|
226
|
+
|
|
227
|
+
const firstNodes = [makeNode(rowA), makeNode(rowB), makeNode(rowC)];
|
|
228
|
+
postSortRows({ api: api as any, nodes: firstNodes } as any);
|
|
229
|
+
|
|
230
|
+
// A data refresh comes back with the same rows in a different order.
|
|
231
|
+
const reordered = [makeNode(rowC), makeNode(rowA), makeNode(rowB)];
|
|
232
|
+
postSortRows({ api: api as any, nodes: reordered } as any);
|
|
233
|
+
|
|
234
|
+
expect(reordered.map((n) => n.data.id)).toEqual(['1', '2', '3']);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test('with no sort configured, a new row is placed at the end even if AgGrid handed it back in the middle', () => {
|
|
238
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid: vi.fn() }));
|
|
239
|
+
const postSortRows = result.current;
|
|
240
|
+
|
|
241
|
+
const api = makeApi([]);
|
|
242
|
+
const rowA = { id: '1', name: 'a' };
|
|
243
|
+
const rowB = { id: '2', name: 'b' };
|
|
244
|
+
|
|
245
|
+
const firstNodes = [makeNode(rowA), makeNode(rowB)];
|
|
246
|
+
postSortRows({ api: api as any, nodes: firstNodes } as any);
|
|
247
|
+
|
|
248
|
+
// New row shows up in the middle of the incoming array, plus the existing rows are reordered too.
|
|
249
|
+
const rowC = { id: '3', name: 'c' };
|
|
250
|
+
const withNewRow = [makeNode(rowB), makeNode(rowC), makeNode(rowA)];
|
|
251
|
+
postSortRows({ api: api as any, nodes: withNewRow } as any);
|
|
252
|
+
|
|
253
|
+
expect(withNewRow.map((n) => n.data.id)).toEqual(['1', '2', '3']);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
describe('usePostSortRowsHook - deferred stale-sort restoration', () => {
|
|
258
|
+
beforeEach(() => vi.useFakeTimers());
|
|
259
|
+
afterEach(() => vi.useRealTimers());
|
|
260
|
+
|
|
261
|
+
test('restores the sort state active when the row went stale, not the newly clicked one', () => {
|
|
262
|
+
const setStaleGrid = vi.fn();
|
|
263
|
+
const { result } = renderHook(() => usePostSortRowsHook({ setStaleGrid }));
|
|
264
|
+
const postSortRows = result.current;
|
|
265
|
+
|
|
266
|
+
// Mutable so it can be updated in place to simulate the user clicking the sort header,
|
|
267
|
+
// while the api (and its applyColumnState mock) stays the same single instance throughout.
|
|
268
|
+
const columnState = [{ colId: 'name', sort: 'asc', sortIndex: 0 }];
|
|
269
|
+
const api = makeApi(columnState) as any;
|
|
270
|
+
const rowA: any = { id: '1', name: 'a' };
|
|
271
|
+
const rowB: any = { id: '2', name: 'b' };
|
|
272
|
+
|
|
273
|
+
// First call backs up the initial hash/order.
|
|
274
|
+
postSortRows({ api, nodes: [makeNode(rowA), makeNode(rowB)] } as any);
|
|
275
|
+
setStaleGrid.mockClear();
|
|
276
|
+
|
|
277
|
+
// Edit the sorted field: AG Grid hands back the naturally re-sorted order, which goes stale.
|
|
278
|
+
rowA.name = 'z';
|
|
279
|
+
const staleNodes = [makeNode(rowB), makeNode(rowA)];
|
|
280
|
+
postSortRows({ api, nodes: staleNodes } as any);
|
|
281
|
+
|
|
282
|
+
expect(setStaleGrid).toHaveBeenCalledWith(true);
|
|
283
|
+
expect(api.applyColumnState).not.toHaveBeenCalled();
|
|
284
|
+
|
|
285
|
+
// User clicks the stale sort header, flipping its direction.
|
|
286
|
+
columnState[0] = { colId: 'name', sort: 'desc', sortIndex: 0 };
|
|
287
|
+
postSortRows({ api, nodes: staleNodes } as any);
|
|
288
|
+
|
|
289
|
+
expect(setStaleGrid).toHaveBeenCalledWith(false);
|
|
290
|
+
// Restoration is deferred to a fresh top-level event - not applied synchronously.
|
|
291
|
+
expect(api.applyColumnState).not.toHaveBeenCalled();
|
|
292
|
+
|
|
293
|
+
vi.runAllTimers();
|
|
294
|
+
|
|
295
|
+
// Restores the asc state that was active when the row went stale, not the desc state just clicked.
|
|
296
|
+
expect(api.applyColumnState).toHaveBeenCalledWith({
|
|
297
|
+
state: [{ colId: 'name', sort: 'asc', sortIndex: 0 }],
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { IRowNode } from 'ag-grid-community';
|
|
2
2
|
import { ColumnState } from 'ag-grid-community';
|
|
3
3
|
import { PostSortRowsParams } from 'ag-grid-community';
|
|
4
|
-
import { isEmpty } from 'lodash-es';
|
|
4
|
+
import { defer, isEmpty } from 'lodash-es';
|
|
5
5
|
import { useCallback, useContext, useRef } from 'react';
|
|
6
6
|
|
|
7
7
|
import { GridContext } from '../contexts/GridContext';
|
|
@@ -25,7 +25,9 @@ export const usePostSortRowsHook = ({ setStaleGrid }: PostSortRowsHookProps) =>
|
|
|
25
25
|
const lastSortOrderHash = useRef<string>('');
|
|
26
26
|
|
|
27
27
|
// Used to detect if sort order was the same, has the direction changed
|
|
28
|
-
const previousRowSortIndexRef = useRef<
|
|
28
|
+
const previousRowSortIndexRef = useRef<
|
|
29
|
+
Record<string, { index: number; values: unknown[]; node: IRowNode } | undefined>
|
|
30
|
+
>({});
|
|
29
31
|
|
|
30
32
|
// stale sort is when there's a sort and user edits a row
|
|
31
33
|
// this applies a class to the div wrapping the grid which in turn adds a * beside the sort arrow
|
|
@@ -40,27 +42,87 @@ export const usePostSortRowsHook = ({ setStaleGrid }: PostSortRowsHookProps) =>
|
|
|
40
42
|
return useCallback(
|
|
41
43
|
({ api, nodes }: PostSortRowsParams) => {
|
|
42
44
|
// Grid is destroyed
|
|
43
|
-
if (!api)
|
|
45
|
+
if (!api || api.isDestroyed()) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
44
48
|
|
|
45
49
|
const previousRowSortIndex = previousRowSortIndexRef.current;
|
|
46
50
|
|
|
47
|
-
const
|
|
48
|
-
return
|
|
51
|
+
const copyCurrentSortSettings = (): ColumnState[] => {
|
|
52
|
+
return api.getColumnState().map((row) => ({ colId: row.colId, sort: row.sort, sortIndex: row.sortIndex }));
|
|
49
53
|
};
|
|
50
54
|
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
// Only the currently sorted columns' values can affect where a row belongs, so that's all we
|
|
56
|
+
// need to compare to detect a change relevant to stale-sort detection.
|
|
57
|
+
const currentSortState = copyCurrentSortSettings();
|
|
58
|
+
const sortedColIds = currentSortState
|
|
59
|
+
.filter((col) => col.sort)
|
|
60
|
+
.sort((a, b) => (a.sortIndex ?? 0) - (b.sortIndex ?? 0))
|
|
61
|
+
.map((col) => col.colId);
|
|
62
|
+
|
|
63
|
+
// Use each column's own `equals` (falling back to its sort `comparator`, then to the grid's
|
|
64
|
+
// default `===`) rather than stringifying values - this respects whatever notion of equality
|
|
65
|
+
// the column already defines.
|
|
66
|
+
const sortColumnEquals: Record<string, (a: unknown, b: unknown, nodeA: IRowNode, nodeB: IRowNode) => boolean> =
|
|
67
|
+
{};
|
|
68
|
+
sortedColIds.forEach((colId) => {
|
|
69
|
+
const colDef = api.getColumn(colId)?.getColDef();
|
|
70
|
+
if (colDef?.equals) {
|
|
71
|
+
const equals = colDef.equals;
|
|
72
|
+
sortColumnEquals[colId] = (a, b) => equals(a, b);
|
|
73
|
+
} else if (colDef?.comparator) {
|
|
74
|
+
const comparator = colDef.comparator;
|
|
75
|
+
// isDescending doesn't matter here - we only care whether the comparator considers the two
|
|
76
|
+
// values equivalent (0), not which direction they'd sort in.
|
|
77
|
+
sortColumnEquals[colId] = (a, b, nodeA, nodeB) => comparator(a, b, nodeA, nodeB, false) === 0;
|
|
78
|
+
} else {
|
|
79
|
+
sortColumnEquals[colId] = (a, b) => a === b;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const getSortValues = (node: IRowNode | undefined): unknown[] =>
|
|
84
|
+
node ? sortedColIds.map((colId) => api.getCellValue({ rowNode: node, colKey: colId })) : [];
|
|
85
|
+
|
|
86
|
+
// A comparator wants the row node too (some use it, e.g. to tie-break) - but by the time this
|
|
87
|
+
// callback runs, AgGrid has already mutated the node in place with the new data, so there's no
|
|
88
|
+
// real "previous node" left to hand it. This lightweight stand-in (same id, snapshotted data) is
|
|
89
|
+
// the best available substitute; it satisfies comparators that only read a couple of fields off
|
|
90
|
+
// nodeA/nodeB and won't behave correctly for one that relies on other IRowNode behaviour.
|
|
91
|
+
const snapshotNode = (node: IRowNode | undefined): IRowNode | undefined =>
|
|
92
|
+
node ? ({ data: node.data, id: node.id } as unknown as IRowNode) : undefined;
|
|
93
|
+
|
|
94
|
+
const sortValuesChanged = (
|
|
95
|
+
previous: { values: unknown[]; node: IRowNode } | undefined,
|
|
96
|
+
node: IRowNode,
|
|
97
|
+
currentValues: unknown[],
|
|
98
|
+
) =>
|
|
99
|
+
!previous ||
|
|
100
|
+
sortedColIds.some(
|
|
101
|
+
(colId, i) => !sortColumnEquals[colId](previous.values[i], currentValues[i], previous.node, node),
|
|
102
|
+
);
|
|
53
103
|
|
|
54
104
|
const backupSortOrder = () => {
|
|
55
|
-
for (const x in previousRowSortIndex)
|
|
56
|
-
|
|
105
|
+
for (const x in previousRowSortIndex) {
|
|
106
|
+
delete previousRowSortIndex[x];
|
|
107
|
+
}
|
|
108
|
+
nodes.forEach(
|
|
109
|
+
(node, index) =>
|
|
110
|
+
(previousRowSortIndex[`${node.data.id}`] = {
|
|
111
|
+
index,
|
|
112
|
+
values: getSortValues(node),
|
|
113
|
+
node: snapshotNode(node)!,
|
|
114
|
+
}),
|
|
115
|
+
);
|
|
57
116
|
};
|
|
58
117
|
|
|
59
118
|
// Check if column is the first sorted column. Note: column is preconfigured to sort its sortIndex is null not 1
|
|
60
|
-
const isFirstSortColumn = (row: ColumnState) =>
|
|
119
|
+
const isFirstSortColumn = (row: ColumnState) => {
|
|
120
|
+
return row.sort && [0, null].includes(row.sortIndex ?? null);
|
|
121
|
+
};
|
|
61
122
|
|
|
62
|
-
const isSameColumnAndDifferentSort = (col1: ColumnState | undefined, col2: ColumnState | undefined) =>
|
|
63
|
-
col1 && col2 && col1.colId === col2.colId && col1.sort != col2.sort;
|
|
123
|
+
const isSameColumnAndDifferentSort = (col1: ColumnState | undefined, col2: ColumnState | undefined) => {
|
|
124
|
+
return col1 && col2 && col1.colId === col2.colId && col1.sort != col2.sort;
|
|
125
|
+
};
|
|
64
126
|
|
|
65
127
|
const restorePreviousSortColumnState = () => api.applyColumnState({ state: previousColSort.current });
|
|
66
128
|
|
|
@@ -78,12 +140,15 @@ export const usePostSortRowsHook = ({ setStaleGrid }: PostSortRowsHookProps) =>
|
|
|
78
140
|
previousColSort.current = copyCurrentSortSettings();
|
|
79
141
|
}
|
|
80
142
|
|
|
81
|
-
const newSortOrder = JSON.stringify(
|
|
143
|
+
const newSortOrder = JSON.stringify(currentSortState);
|
|
82
144
|
let sortOrderChanged = newSortOrder != lastSortOrderHash.current;
|
|
83
145
|
|
|
84
|
-
|
|
146
|
+
// api.getQuickFilter() returns undefined (not '') when no quick filter is set - normalize before
|
|
147
|
+
// comparing, otherwise '' (the initial ref value) never equals undefined and this looks "changed"
|
|
148
|
+
// on every single call for a grid that never uses the quick filter feature.
|
|
149
|
+
const quickFilter = api.getQuickFilter() ?? '';
|
|
85
150
|
if (previousQuickFilter.current != quickFilter) {
|
|
86
|
-
previousQuickFilter.current = quickFilter
|
|
151
|
+
previousQuickFilter.current = quickFilter;
|
|
87
152
|
sortOrderChanged = true;
|
|
88
153
|
}
|
|
89
154
|
|
|
@@ -92,7 +157,7 @@ export const usePostSortRowsHook = ({ setStaleGrid }: PostSortRowsHookProps) =>
|
|
|
92
157
|
}
|
|
93
158
|
|
|
94
159
|
if (sortOrderChanged) {
|
|
95
|
-
const thisFirstCol =
|
|
160
|
+
const thisFirstCol = currentSortState.find(isFirstSortColumn);
|
|
96
161
|
const previousFirstCol = previousColSort.current.find(isFirstSortColumn);
|
|
97
162
|
|
|
98
163
|
// Change to sort can be: (in the context of stale sort do...)
|
|
@@ -111,7 +176,12 @@ export const usePostSortRowsHook = ({ setStaleGrid }: PostSortRowsHookProps) =>
|
|
|
111
176
|
|
|
112
177
|
// trigger a recapture of sort order
|
|
113
178
|
lastSortOrderHash.current = '';
|
|
114
|
-
|
|
179
|
+
// applyColumnState() here would be a no-op if called synchronously - we're still inside AgGrid's
|
|
180
|
+
// current sort pass (this callback runs as part of it), and it won't start a fresh one re-entrantly.
|
|
181
|
+
// Defer so it runs as a new, top-level event once the current pass has finished.
|
|
182
|
+
defer(() => {
|
|
183
|
+
restorePreviousSortColumnState();
|
|
184
|
+
});
|
|
115
185
|
} else {
|
|
116
186
|
// Sort was on a different column so clear sort and sort like normal
|
|
117
187
|
if (sortWasStale.current) {
|
|
@@ -131,7 +201,7 @@ export const usePostSortRowsHook = ({ setStaleGrid }: PostSortRowsHookProps) =>
|
|
|
131
201
|
for (const node of nodes) {
|
|
132
202
|
const psr = previousRowSortIndex[`${node.data.id}`];
|
|
133
203
|
if (psr) {
|
|
134
|
-
if (psr
|
|
204
|
+
if (sortValuesChanged(psr, node, getSortValues(node))) {
|
|
135
205
|
if (firstChangedNodeIndex === -1) firstChangedNodeIndex = index;
|
|
136
206
|
changedRowCount++;
|
|
137
207
|
}
|
|
@@ -146,17 +216,22 @@ export const usePostSortRowsHook = ({ setStaleGrid }: PostSortRowsHookProps) =>
|
|
|
146
216
|
if (changedRowCount === 0 && newRowCount === 1) {
|
|
147
217
|
// insert new row at end
|
|
148
218
|
const newIndex = index - 1;
|
|
149
|
-
previousRowSortIndex[`${lastNewNode?.data.id}`] = {
|
|
219
|
+
previousRowSortIndex[`${lastNewNode?.data.id}`] = {
|
|
220
|
+
index: newIndex,
|
|
221
|
+
values: getSortValues(lastNewNode),
|
|
222
|
+
node: snapshotNode(lastNewNode)!,
|
|
223
|
+
};
|
|
150
224
|
wasStale = true;
|
|
151
225
|
} else if (changedRowCount === 2 && newRowCount === 0) {
|
|
152
226
|
// This must be a swap rows (sometimes it's not, needs further attention)
|
|
153
227
|
backupSortOrder();
|
|
154
228
|
wasStale = false;
|
|
155
229
|
} else if (changedRowCount > 1 && newRowCount === 1) {
|
|
156
|
-
// This must be
|
|
230
|
+
// This must be an insert so, insert new row near the row that changed
|
|
157
231
|
previousRowSortIndex[`${lastNewNode?.data.id}`] = {
|
|
158
232
|
index: firstChangedNodeIndex + 0.5,
|
|
159
|
-
|
|
233
|
+
values: getSortValues(lastNewNode),
|
|
234
|
+
node: snapshotNode(lastNewNode)!,
|
|
160
235
|
};
|
|
161
236
|
wasStale = true;
|
|
162
237
|
// For some reason AgGrid mis-positions the inserted row.
|
|
@@ -164,10 +239,38 @@ export const usePostSortRowsHook = ({ setStaleGrid }: PostSortRowsHookProps) =>
|
|
|
164
239
|
redrawRows();
|
|
165
240
|
}
|
|
166
241
|
} else if (changedRowCount == 1 && newRowCount === 0) {
|
|
167
|
-
// User edited one row so, do nothing, retain sort
|
|
242
|
+
// User edited one row so, do nothing, retain sort. Refresh this row's stored values (keeping its
|
|
243
|
+
// retained index) so a *different* row edited afterward isn't miscounted as "2 rows changed" against
|
|
244
|
+
// this row's now-stale values, which would misfire the swap-detection branch above and silently drop
|
|
245
|
+
// the retained order.
|
|
246
|
+
const changedNode = nodes[firstChangedNodeIndex];
|
|
247
|
+
const previousEntry = changedNode && previousRowSortIndex[`${changedNode.data.id}`];
|
|
248
|
+
if (changedNode && previousEntry) {
|
|
249
|
+
previousRowSortIndex[`${changedNode.data.id}`] = {
|
|
250
|
+
index: previousEntry.index,
|
|
251
|
+
values: getSortValues(changedNode),
|
|
252
|
+
node: snapshotNode(changedNode)!,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
168
255
|
wasStale = true;
|
|
169
|
-
} else if (
|
|
170
|
-
|
|
256
|
+
} else if (
|
|
257
|
+
changedRowCount === 0 &&
|
|
258
|
+
newRowCount === 0 &&
|
|
259
|
+
nodes.length === Object.keys(previousRowSortIndex).length
|
|
260
|
+
) {
|
|
261
|
+
// No row's sort values look changed and no rows were added/removed, but that doesn't mean nothing needs
|
|
262
|
+
// checking: AgGrid invokes postSortRows once per data update with a pass-through (not actually
|
|
263
|
+
// re-sorted) `nodes` array, then again after the forced refreshClientSideRowModel() call in
|
|
264
|
+
// GridContextProvider (needed to get AgGrid to actually run the sort stage after a plain data update)
|
|
265
|
+
// with a genuinely freshly-sorted array. By that second call the "changedRowCount == 1" branch above has
|
|
266
|
+
// already refreshed the edited row's stored values on the first (pass-through) call, so it no longer looks
|
|
267
|
+
// "changed" here - this may be our first chance to see the row's real new position. Fall through to the
|
|
268
|
+
// position-based stillStale check below instead of treating this as "nothing to do".
|
|
269
|
+
wasStale = true;
|
|
270
|
+
} else {
|
|
271
|
+
// Either too many rows changed, or rows were removed with nothing else changing (changedRowCount and
|
|
272
|
+
// newRowCount are both 0 in the deletion case) - either way, resort and refresh the backup so we don't
|
|
273
|
+
// leave stale ids for removed rows in previousRowSortIndex.
|
|
171
274
|
backupSortOrder();
|
|
172
275
|
}
|
|
173
276
|
|