@linzjs/step-ag-grid 35.2.1 → 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/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": "35.2.1",
5
+ "version": "35.2.2",
6
6
  "keywords": [
7
7
  "aggrid",
8
8
  "ag-grid",
@@ -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
- // You cannot pass a width to a flex
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,
@@ -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
- const handle = nameHeader.querySelector<HTMLElement>('.ag-header-cell-resize')!;
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
+ };