@linzjs/step-ag-grid 35.2.0 → 35.2.2
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/step-ag-grid.cjs +7 -5
- package/dist/step-ag-grid.cjs.map +1 -1
- package/dist/step-ag-grid.esm.js +7 -5
- package/dist/step-ag-grid.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Grid.tsx +5 -3
- package/src/contexts/GridPopoverContextProvider.tsx +7 -2
- package/src/stories/grid/GridColumnState.stories.tsx +101 -10
package/package.json
CHANGED
package/src/components/Grid.tsx
CHANGED
|
@@ -834,15 +834,17 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
834
834
|
return r;
|
|
835
835
|
};
|
|
836
836
|
}
|
|
837
|
+
// ag-grid re-applies colDef flex/width on every columnDefs update, which would undo user/restored widths
|
|
838
|
+
const userWidth = userSizedColIds.current.get(colDef.colId ?? colDef.field ?? '');
|
|
837
839
|
const adjustedColDef = {
|
|
838
840
|
...colDef,
|
|
839
|
-
//
|
|
840
|
-
width: colDef.flex ? undefined : colDef.width,
|
|
841
|
+
// Flex columns ignore width, so only a user-sized (no longer flex) column gets one
|
|
842
|
+
width: userWidth ?? (colDef.flex ? undefined : colDef.width),
|
|
841
843
|
...(!!colDef.flex && { flexAutoSizeWidth: colDef.width }),
|
|
842
844
|
// If this is allowed flex columns don't size based on flex
|
|
843
845
|
suppressSizeToFit: true,
|
|
844
846
|
// Auto-sizing flex columns breaks everything
|
|
845
|
-
flex,
|
|
847
|
+
flex: userWidth != null ? undefined : flex,
|
|
846
848
|
suppressAutoSize: !!flex,
|
|
847
849
|
sortable,
|
|
848
850
|
comparator,
|
|
@@ -31,6 +31,9 @@ export const GridPopoverContextProvider = (props2: PropsWithChildren<GridPopover
|
|
|
31
31
|
|
|
32
32
|
const field = props.colDef?.field ?? '';
|
|
33
33
|
const colId = props.colDef?.colId ?? field ?? '';
|
|
34
|
+
// Must match the key GridCellRenderer uses in checkUpdating, otherwise columns without a field
|
|
35
|
+
// (e.g. action menus) never show the updating spinner
|
|
36
|
+
const updatingKey = props.colDef?.field ?? props.colDef?.colId ?? '';
|
|
34
37
|
|
|
35
38
|
const updateValue = useCallback(
|
|
36
39
|
async (saveFn: (selectedRows: any[]) => Promise<boolean>, tabDirection: 1 | 0 | -1): Promise<boolean> => {
|
|
@@ -39,9 +42,11 @@ export const GridPopoverContextProvider = (props2: PropsWithChildren<GridPopover
|
|
|
39
42
|
return true;
|
|
40
43
|
}
|
|
41
44
|
hasSaved.current = true;
|
|
42
|
-
return saving
|
|
45
|
+
return saving
|
|
46
|
+
? false
|
|
47
|
+
: await updatingCells({ selectedRows, field: updatingKey }, saveFn, setSaving, tabDirection);
|
|
43
48
|
},
|
|
44
|
-
[
|
|
49
|
+
[saving, selectedRows, updatingCells, updatingKey],
|
|
45
50
|
);
|
|
46
51
|
|
|
47
52
|
return (
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Meta, StoryObj } from '@storybook/react-vite';
|
|
2
|
-
import { ReactElement, useState } from 'react';
|
|
2
|
+
import { ReactElement, useEffect, useState } from 'react';
|
|
3
3
|
import { expect, userEvent, waitFor, within } from 'storybook/test';
|
|
4
4
|
|
|
5
5
|
import {
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
} from '../..';
|
|
14
14
|
import { GridFilterColumnsToggle } from '../../components';
|
|
15
15
|
import { clickColumnHeaderToSort, waitForGridRows } from '../../utils/__tests__/storybookTestUtil';
|
|
16
|
+
import { ChromaticAllowGridAutosize } from '../../utils/isChromatic';
|
|
16
17
|
|
|
17
18
|
interface ITestRow {
|
|
18
19
|
id: number;
|
|
@@ -82,6 +83,70 @@ const TabbedGrids = (): ReactElement => {
|
|
|
82
83
|
);
|
|
83
84
|
};
|
|
84
85
|
|
|
86
|
+
const flexColumnDefs: ColDefT<ITestRow>[] = [
|
|
87
|
+
GridCell<ITestRow, ITestRow['id']>({ field: 'id', headerName: 'Id', flex: 1 }),
|
|
88
|
+
GridCell<ITestRow, ITestRow['name']>({ field: 'name', headerName: 'Name', flex: 1 }),
|
|
89
|
+
GridCell<ITestRow, ITestRow['description']>({ field: 'description', headerName: 'Description', flex: 2 }),
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
// Mounts on first show and loads its rows after, like a lazy tab fetching its data
|
|
93
|
+
const LazyLoadingGrid = ({ label, rowData }: { label: string; rowData: ITestRow[] }): ReactElement => {
|
|
94
|
+
const [loaded, setLoaded] = useState(false);
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
const timeout = setTimeout(() => setLoaded(true), 300);
|
|
97
|
+
return () => clearTimeout(timeout);
|
|
98
|
+
}, []);
|
|
99
|
+
return (
|
|
100
|
+
<GridUpdatingContextProvider>
|
|
101
|
+
<GridContextProvider>
|
|
102
|
+
<GridWrapper maxHeight={300}>
|
|
103
|
+
<Grid
|
|
104
|
+
data-testid={label}
|
|
105
|
+
columnDefs={flexColumnDefs}
|
|
106
|
+
rowData={loaded ? rowData : []}
|
|
107
|
+
loading={!loaded}
|
|
108
|
+
saveColumnState={true}
|
|
109
|
+
/>
|
|
110
|
+
</GridWrapper>
|
|
111
|
+
</GridContextProvider>
|
|
112
|
+
</GridUpdatingContextProvider>
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const LazyTabbedGrids = (): ReactElement => {
|
|
117
|
+
const [activeTab, setActiveTab] = useState(tabs[0].label);
|
|
118
|
+
const [visitedTabs, setVisitedTabs] = useState([tabs[0].label]);
|
|
119
|
+
return (
|
|
120
|
+
<>
|
|
121
|
+
<div style={{ display: 'flex', gap: 8, marginBottom: 8 }}>
|
|
122
|
+
{tabs.map((tab) => (
|
|
123
|
+
<button
|
|
124
|
+
key={tab.label}
|
|
125
|
+
role="tab"
|
|
126
|
+
aria-selected={tab.label === activeTab}
|
|
127
|
+
onClick={() => {
|
|
128
|
+
setActiveTab(tab.label);
|
|
129
|
+
setVisitedTabs((visited) => (visited.includes(tab.label) ? visited : [...visited, tab.label]));
|
|
130
|
+
}}
|
|
131
|
+
>
|
|
132
|
+
{tab.label}
|
|
133
|
+
</button>
|
|
134
|
+
))}
|
|
135
|
+
</div>
|
|
136
|
+
{tabs.map((tab) => (
|
|
137
|
+
<div
|
|
138
|
+
key={tab.label}
|
|
139
|
+
role="tabpanel"
|
|
140
|
+
aria-label={tab.label}
|
|
141
|
+
style={{ display: tab.label === activeTab ? 'flex' : 'none', flexDirection: 'column', flex: 1 }}
|
|
142
|
+
>
|
|
143
|
+
{visitedTabs.includes(tab.label) && <LazyLoadingGrid label={tab.label} rowData={tab.rowData} />}
|
|
144
|
+
</div>
|
|
145
|
+
))}
|
|
146
|
+
</>
|
|
147
|
+
);
|
|
148
|
+
};
|
|
149
|
+
|
|
85
150
|
const meta: Meta<typeof TabbedGrids> = {
|
|
86
151
|
title: 'Components / Grids',
|
|
87
152
|
component: TabbedGrids,
|
|
@@ -108,6 +173,17 @@ const headerCell = (canvasElement: HTMLElement, label: string, colId: string) =>
|
|
|
108
173
|
panel(canvasElement, label).querySelector<HTMLElement>(`.ag-header-cell[col-id="${colId}"]`)!;
|
|
109
174
|
const showTab = (canvasElement: HTMLElement, label: string) =>
|
|
110
175
|
userEvent.click(within(canvasElement).getByRole('tab', { name: label }));
|
|
176
|
+
const dragResize = async (header: HTMLElement, delta: number) => {
|
|
177
|
+
const handle = header.querySelector<HTMLElement>('.ag-header-cell-resize')!;
|
|
178
|
+
const { left, top, width, height } = handle.getBoundingClientRect();
|
|
179
|
+
const start = { clientX: left + width / 2, clientY: top + height / 2 };
|
|
180
|
+
await userEvent.pointer([
|
|
181
|
+
{ keys: '[MouseLeft>]', target: handle, coords: start },
|
|
182
|
+
{ target: handle, coords: { ...start, clientX: start.clientX + delta / 2 } },
|
|
183
|
+
{ target: handle, coords: { ...start, clientX: start.clientX + delta } },
|
|
184
|
+
{ keys: '[/MouseLeft]', target: handle, coords: { ...start, clientX: start.clientX + delta } },
|
|
185
|
+
]);
|
|
186
|
+
};
|
|
111
187
|
|
|
112
188
|
export const GridColumnStateSharedBetweenTabs: Story = {
|
|
113
189
|
play: async ({ canvasElement }) => {
|
|
@@ -121,15 +197,7 @@ export const GridColumnStateSharedBetweenTabs: Story = {
|
|
|
121
197
|
|
|
122
198
|
const nameHeader = headerCell(canvasElement, 'Second tab', 'name');
|
|
123
199
|
const widthBefore = nameHeader.getBoundingClientRect().width;
|
|
124
|
-
|
|
125
|
-
const { left, top, width, height } = handle.getBoundingClientRect();
|
|
126
|
-
const start = { clientX: left + width / 2, clientY: top + height / 2 };
|
|
127
|
-
await userEvent.pointer([
|
|
128
|
-
{ keys: '[MouseLeft>]', target: handle, coords: start },
|
|
129
|
-
{ target: handle, coords: { ...start, clientX: start.clientX + 60 } },
|
|
130
|
-
{ target: handle, coords: { ...start, clientX: start.clientX + 120 } },
|
|
131
|
-
{ keys: '[/MouseLeft]', target: handle, coords: { ...start, clientX: start.clientX + 120 } },
|
|
132
|
-
]);
|
|
200
|
+
await dragResize(nameHeader, 120);
|
|
133
201
|
await waitFor(() => expect(nameHeader.getBoundingClientRect().width).toBeGreaterThan(widthBefore + 60));
|
|
134
202
|
const resizedWidth = Math.round(nameHeader.getBoundingClientRect().width);
|
|
135
203
|
|
|
@@ -159,3 +227,26 @@ export const GridColumnVisibilitySharedBetweenTabs: Story = {
|
|
|
159
227
|
await waitFor(() => expect(headerCell(canvasElement, 'Second tab', 'name')).toBeNull());
|
|
160
228
|
},
|
|
161
229
|
};
|
|
230
|
+
|
|
231
|
+
export const GridColumnStateRestoredOnLazyLoadedTab: Story = {
|
|
232
|
+
render: () => <LazyTabbedGrids />,
|
|
233
|
+
// Chromatic skips restoring saved state, which this story tests; beforeEach clears it so nothing leaks in
|
|
234
|
+
decorators: [ChromaticAllowGridAutosize],
|
|
235
|
+
play: async ({ canvasElement }) => {
|
|
236
|
+
await waitForGridRows({ grid: panel(canvasElement, 'First tab') });
|
|
237
|
+
|
|
238
|
+
const nameHeader = headerCell(canvasElement, 'First tab', 'name');
|
|
239
|
+
const widthBefore = nameHeader.getBoundingClientRect().width;
|
|
240
|
+
await dragResize(nameHeader, 120);
|
|
241
|
+
await waitFor(() => expect(nameHeader.getBoundingClientRect().width).toBeGreaterThan(widthBefore + 60));
|
|
242
|
+
const resizedWidth = Math.round(nameHeader.getBoundingClientRect().width);
|
|
243
|
+
|
|
244
|
+
await showTab(canvasElement, 'Second tab');
|
|
245
|
+
await waitForGridRows({ grid: panel(canvasElement, 'Second tab') });
|
|
246
|
+
// Give a reverted width time to show up after the rows load
|
|
247
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
248
|
+
expect(Math.round(headerCell(canvasElement, 'Second tab', 'name').getBoundingClientRect().width)).toBe(
|
|
249
|
+
resizedWidth,
|
|
250
|
+
);
|
|
251
|
+
},
|
|
252
|
+
};
|