@linzjs/step-ag-grid 29.11.3 → 29.12.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/contexts/GridContext.d.ts +1 -1
- package/dist/src/utils/__tests__/storybookTestUtil.ts +28 -1
- package/dist/src/utils/__tests__/testQuick.ts +2 -0
- package/dist/src/utils/util.d.ts +1 -0
- package/dist/step-ag-grid.cjs +315 -91
- package/dist/step-ag-grid.cjs.map +1 -1
- package/dist/step-ag-grid.esm.js +315 -92
- package/dist/step-ag-grid.esm.js.map +1 -1
- package/package.json +2 -1
- package/src/components/Grid.tsx +139 -66
- package/src/contexts/GridContext.tsx +3 -3
- package/src/contexts/GridContextProvider.tsx +55 -36
- package/src/stories/grid/GridPopoutEditGeneric.stories.tsx +9 -3
- package/src/stories/grid/GridSorting.stories.tsx +189 -0
- package/src/stories/grid/gridAutosize/GridAutoSize.stories.tsx +84 -0
- package/src/stories/grid/gridAutosize/GridFitSize.stories.tsx +84 -0
- package/src/stories/grid/gridAutosize/GridFitSizeFlex.stories.tsx +86 -0
- package/src/stories/grid/gridAutosize/ShowPanelResizingAgGrid/ShowPanelResizingStepAgGrid.tsx +49 -16
- package/src/utils/__tests__/storybookTestUtil.ts +28 -1
- package/src/utils/__tests__/testQuick.ts +2 -0
- package/src/utils/util.ts +19 -0
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": "29.
|
|
5
|
+
"version": "29.12.0",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"aggrid",
|
|
8
8
|
"ag-grid",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"clsx": "^2.1.1",
|
|
39
39
|
"debounce-promise": "^3.1.2",
|
|
40
40
|
"matcher": "^5.0.0",
|
|
41
|
+
"natsort": "^2.0.3",
|
|
41
42
|
"react-transition-state": "^2.3.1",
|
|
42
43
|
"usehooks-ts": "^3.1.1",
|
|
43
44
|
"uuid": "^13.0.0"
|
package/src/components/Grid.tsx
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
RowDragMoveEvent,
|
|
24
24
|
SelectionChangedEvent,
|
|
25
25
|
SelectionColumnDef,
|
|
26
|
+
ValueFormatterParams,
|
|
26
27
|
} from 'ag-grid-community';
|
|
27
28
|
import { AgGridReact } from 'ag-grid-react';
|
|
28
29
|
import clsx from 'clsx';
|
|
@@ -32,7 +33,7 @@ import { useInterval } from 'usehooks-ts';
|
|
|
32
33
|
|
|
33
34
|
import { AutoSizeColumnsResult, StartCellEditingProps, useGridContext } from '../contexts/GridContext';
|
|
34
35
|
import { GridUpdatingContext } from '../contexts/GridUpdatingContext';
|
|
35
|
-
import { fnOrVar, isNotEmpty } from '../utils/util';
|
|
36
|
+
import { compareNaturalInsensitive, fnOrVar, isNotEmpty } from '../utils/util';
|
|
36
37
|
import { clickInputWhenContainingCellClicked } from './clickInputWhenContainingCellClicked';
|
|
37
38
|
import { GridHeaderSelect } from './gridHeader';
|
|
38
39
|
import { GridContextMenuComponent, useGridContextMenu } from './gridHook';
|
|
@@ -205,68 +206,79 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
205
206
|
|
|
206
207
|
const autoSizeResultRef = useRef<AutoSizeColumnsResult | null>(null);
|
|
207
208
|
const prevRowsVisibleRef = useRef(false);
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
needsAutoSize.current = true;
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const gridRendered = gridRenderState();
|
|
216
|
-
if (gridRendered === null) {
|
|
217
|
-
// Don't resize until grid has rendered, or it has 0 rows.
|
|
218
|
-
needsAutoSize.current = true;
|
|
209
|
+
const initialContentSizeInProgressRef = useRef(false);
|
|
210
|
+
const setInitialContentSize = useCallback(async (): Promise<void> => {
|
|
211
|
+
if (initialContentSizeInProgressRef.current) {
|
|
219
212
|
return;
|
|
220
213
|
}
|
|
214
|
+
initialContentSizeInProgressRef.current = true;
|
|
215
|
+
try {
|
|
216
|
+
needsAutoSize.current = false;
|
|
221
217
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const skipHeader = sizeColumns === 'auto-skip-headers' && rowsVisible;
|
|
227
|
-
// If grid was empty and now has content we need to autosize
|
|
228
|
-
if (rowsVisible !== prevRowsVisibleRef.current && rowsVisible) {
|
|
229
|
-
prevRowsVisibleRef.current = rowsVisible;
|
|
230
|
-
autoSizeResultRef.current = null;
|
|
218
|
+
if (!gridDivRef.current?.clientWidth || rowData == null) {
|
|
219
|
+
// Don't resize grids if they are offscreen as it doesn't work.
|
|
220
|
+
needsAutoSize.current = true;
|
|
221
|
+
return;
|
|
231
222
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
userSizedColIds: new Set(userSizedColIds.current.keys()),
|
|
237
|
-
});
|
|
238
|
-
// Auto-size failed retry later
|
|
239
|
-
if (!autoSizeResult) {
|
|
223
|
+
|
|
224
|
+
const gridRendered = gridRenderState();
|
|
225
|
+
if (gridRendered === null) {
|
|
226
|
+
// Don't resize until grid has rendered, or it has 0 rows.
|
|
240
227
|
needsAutoSize.current = true;
|
|
241
228
|
return;
|
|
242
229
|
}
|
|
243
230
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
//
|
|
250
|
-
if (
|
|
251
|
-
|
|
252
|
-
|
|
231
|
+
// 1. First we autosize to get the size of the columns on an infinite grid.
|
|
232
|
+
if (sizeColumns === 'auto' || sizeColumns === 'auto-skip-headers') {
|
|
233
|
+
// You can't skip headers until the grid has content
|
|
234
|
+
const rowsVisible = gridRendered === 'rows-visible';
|
|
235
|
+
const skipHeader = sizeColumns === 'auto-skip-headers' && rowsVisible;
|
|
236
|
+
// If grid was empty and now has content we need to autosize
|
|
237
|
+
if (rowsVisible !== prevRowsVisibleRef.current && rowsVisible) {
|
|
238
|
+
prevRowsVisibleRef.current = rowsVisible;
|
|
239
|
+
autoSizeResultRef.current = null;
|
|
240
|
+
}
|
|
241
|
+
const autoSizeResult =
|
|
242
|
+
autoSizeResultRef.current ??
|
|
243
|
+
(await autoSizeColumns({
|
|
244
|
+
skipHeader,
|
|
245
|
+
userSizedColIds: new Set(userSizedColIds.current.keys()),
|
|
246
|
+
}));
|
|
247
|
+
// Auto-size failed retry later
|
|
248
|
+
if (!autoSizeResult) {
|
|
249
|
+
needsAutoSize.current = true;
|
|
250
|
+
return;
|
|
253
251
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
252
|
+
|
|
253
|
+
autoSizeResultRef.current = autoSizeResult;
|
|
254
|
+
// Calculate the auto-sized width, limit it to maxInitialWidth
|
|
255
|
+
autoSizeResult.width = maxInitialWidth ? Math.min(autoSizeResult.width, maxInitialWidth) : autoSizeResult.width;
|
|
256
|
+
if (gridRendered === 'empty') {
|
|
257
|
+
// If the grid is empty we still do an onContentSize callback, we will do another callback when grid has data
|
|
258
|
+
// We don't do this callback if we have previously had row data, or have already called back for empty
|
|
259
|
+
if (!hasSetContentSizeEmpty.current && !hasSetContentSize.current) {
|
|
260
|
+
hasSetContentSizeEmpty.current = true;
|
|
261
|
+
params.onContentSize?.(autoSizeResult);
|
|
262
|
+
}
|
|
263
|
+
} else if (gridRendered === 'rows-visible') {
|
|
264
|
+
// we have rows now so callback grid size
|
|
265
|
+
if (!hasSetContentSize.current) {
|
|
266
|
+
hasSetContentSize.current = true;
|
|
267
|
+
params.onContentSize?.(autoSizeResult);
|
|
268
|
+
}
|
|
269
|
+
} else {
|
|
270
|
+
// It should be impossible to get here
|
|
271
|
+
console.error('Unknown value returned from hasGridRendered');
|
|
259
272
|
}
|
|
260
273
|
} else {
|
|
261
|
-
|
|
262
|
-
console.error('Unknown value returned from hasGridRendered');
|
|
274
|
+
sizeColumnsToFit();
|
|
263
275
|
}
|
|
264
|
-
} else {
|
|
265
|
-
sizeColumnsToFit();
|
|
266
|
-
}
|
|
267
276
|
|
|
268
|
-
|
|
269
|
-
|
|
277
|
+
setAutoSized(true);
|
|
278
|
+
needsAutoSize.current = false;
|
|
279
|
+
} finally {
|
|
280
|
+
initialContentSizeInProgressRef.current = false;
|
|
281
|
+
}
|
|
270
282
|
}, [autoSizeColumns, gridRenderState, maxInitialWidth, params, rowData, sizeColumns, sizeColumnsToFit]);
|
|
271
283
|
|
|
272
284
|
const lastOwnerDocumentRef = useRef<Document>();
|
|
@@ -305,8 +317,7 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
305
317
|
needsAutoSize.current ||
|
|
306
318
|
(!hasSetContentSize.current && (sizeColumns === 'auto' || sizeColumns === 'auto-skip-headers'))
|
|
307
319
|
) {
|
|
308
|
-
|
|
309
|
-
setInitialContentSize();
|
|
320
|
+
void setInitialContentSize();
|
|
310
321
|
}
|
|
311
322
|
}, 200);
|
|
312
323
|
|
|
@@ -460,11 +471,14 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
460
471
|
if (previousRowDataLength.current !== length) {
|
|
461
472
|
// We need to autosize all cells again
|
|
462
473
|
autoSizeResultRef.current = null;
|
|
463
|
-
setInitialContentSize();
|
|
464
474
|
previousRowDataLength.current = length;
|
|
475
|
+
void setInitialContentSize();
|
|
476
|
+
return;
|
|
465
477
|
}
|
|
466
478
|
|
|
467
|
-
if (lastUpdatedDep.current === updatedDep || isEmpty(colIdsEdited.current))
|
|
479
|
+
if (lastUpdatedDep.current === updatedDep || isEmpty(colIdsEdited.current)) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
468
482
|
lastUpdatedDep.current = updatedDep;
|
|
469
483
|
|
|
470
484
|
// Don't update while there are spinners
|
|
@@ -474,7 +488,7 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
474
488
|
|
|
475
489
|
const skipHeader = sizeColumns === 'auto-skip-headers';
|
|
476
490
|
if ((sizeColumns === 'auto' || sizeColumns === 'auto-skip-headers') && hasSetContentSize.current) {
|
|
477
|
-
autoSizeColumns({
|
|
491
|
+
void autoSizeColumns({
|
|
478
492
|
skipHeader,
|
|
479
493
|
userSizedColIds: new Set(userSizedColIds.current.keys()),
|
|
480
494
|
colIds: colIdsEdited.current,
|
|
@@ -585,11 +599,54 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
585
599
|
children: colDef.children.map((colDef) => adjustColDefOrGroup(colDef)),
|
|
586
600
|
});
|
|
587
601
|
|
|
588
|
-
const adjustColDef = (colDef: ColDef<TData>): ColDef<TData> =>
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
sortable
|
|
592
|
-
|
|
602
|
+
const adjustColDef = (colDef: ColDef<TData>): ColDef<TData> => {
|
|
603
|
+
const flex = colDef.flex ?? (sizeColumns === 'fit' ? 1 : undefined);
|
|
604
|
+
const valueFormatter = colDef.valueFormatter;
|
|
605
|
+
const sortable = colDef.sortable && params.defaultColDef?.sortable !== false;
|
|
606
|
+
let comparator = colDef.comparator;
|
|
607
|
+
if (sortable && !comparator) {
|
|
608
|
+
comparator = (value1, value2, node1, node2) => {
|
|
609
|
+
let r = 0;
|
|
610
|
+
if (typeof valueFormatter === 'function') {
|
|
611
|
+
r = compareNaturalInsensitive(
|
|
612
|
+
valueFormatter({
|
|
613
|
+
data: node1.data,
|
|
614
|
+
value: value1,
|
|
615
|
+
node: node1,
|
|
616
|
+
colDef: adjustedColDef,
|
|
617
|
+
...NotAGridValueFormatterCall,
|
|
618
|
+
} as ValueFormatterParams<TData>),
|
|
619
|
+
valueFormatter({
|
|
620
|
+
data: node2.data,
|
|
621
|
+
value: value2,
|
|
622
|
+
node: node2,
|
|
623
|
+
colDef: adjustedColDef,
|
|
624
|
+
...NotAGridValueFormatterCall,
|
|
625
|
+
} as ValueFormatterParams<TData>),
|
|
626
|
+
);
|
|
627
|
+
} else {
|
|
628
|
+
r = compareNaturalInsensitive(value1, value2);
|
|
629
|
+
}
|
|
630
|
+
// secondary compare as primary sort column rows are equal
|
|
631
|
+
return r === 0 ? compareNaturalInsensitive(node1.data?.id, node2.data?.id) : r;
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
const adjustedColDef = {
|
|
635
|
+
...colDef,
|
|
636
|
+
// You cannot pass a width to a flex
|
|
637
|
+
width: !!colDef.flex ? undefined : colDef.width,
|
|
638
|
+
...(!!colDef.flex && { flexAutoSizeWidth: colDef.width }),
|
|
639
|
+
// If this is allowed flex columns don't size based on flex
|
|
640
|
+
suppressSizeToFit: true,
|
|
641
|
+
// Auto-sizing flex columns breaks everything
|
|
642
|
+
flex,
|
|
643
|
+
suppressAutoSize: !!flex,
|
|
644
|
+
sortable,
|
|
645
|
+
comparator,
|
|
646
|
+
} as ColDef<TData> & { flexAutoSizeWidth?: number };
|
|
647
|
+
|
|
648
|
+
return adjustedColDef;
|
|
649
|
+
};
|
|
593
650
|
|
|
594
651
|
return columnDefs.map((colDef) => adjustColDefOrGroup(colDef));
|
|
595
652
|
}, [columnDefs, params.defaultColDef?.sortable, sizeColumns]);
|
|
@@ -617,7 +674,7 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
617
674
|
if (sizeColumns === 'auto' || skipHeader) {
|
|
618
675
|
defer(() => {
|
|
619
676
|
if (hasSetContentSize.current) {
|
|
620
|
-
autoSizeColumns({
|
|
677
|
+
void autoSizeColumns({
|
|
621
678
|
skipHeader,
|
|
622
679
|
userSizedColIds: new Set(userSizedColIds.current.keys()),
|
|
623
680
|
colIds: colIdsEdited.current,
|
|
@@ -812,9 +869,9 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
812
869
|
[columnDefs, params.loading, params.noRowsMatchingOverlayText, params.noRowsOverlayText, rowData, rowHeight],
|
|
813
870
|
);
|
|
814
871
|
|
|
815
|
-
const selectionColumnDef = useMemo((): SelectionColumnDef => {
|
|
872
|
+
const selectionColumnDef = useMemo((): SelectionColumnDef | undefined => {
|
|
816
873
|
// Note this has to be 1 for hidden otherwise ag-grid does crazy things whilst resizing
|
|
817
|
-
const selectWidth = params.
|
|
874
|
+
const selectWidth = params.onRowDragEnd ? 76 : 48;
|
|
818
875
|
return {
|
|
819
876
|
suppressNavigable: params.hideSelectColumn,
|
|
820
877
|
rowDrag: !!params.onRowDragEnd,
|
|
@@ -824,6 +881,8 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
824
881
|
headerComponentParams: {
|
|
825
882
|
exportable: false,
|
|
826
883
|
},
|
|
884
|
+
suppressAutoSize: true,
|
|
885
|
+
suppressSizeToFit: true,
|
|
827
886
|
headerClass: clsx('ag-header-hide-default-select', params.onRowDragEnd && 'ag-header-select-draggable'),
|
|
828
887
|
headerComponent: rowSelection == 'multiple' ? GridHeaderSelect : undefined,
|
|
829
888
|
suppressHeaderKeyboardEvent: (e) => {
|
|
@@ -855,7 +914,7 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
855
914
|
|
|
856
915
|
const onGridSizeChanged = useCallback(
|
|
857
916
|
(event: GridSizeChangedEvent<TData>) => {
|
|
858
|
-
if (sizeColumns === 'fit') {
|
|
917
|
+
if (sizeColumns === 'fit' || (['auto', 'auto-skip-headers'].includes(sizeColumns) && hasSetContentSize.current)) {
|
|
859
918
|
event.api.sizeColumnsToFit();
|
|
860
919
|
}
|
|
861
920
|
},
|
|
@@ -883,9 +942,14 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
883
942
|
enableSelectionWithoutKeys: params.enableSelectionWithoutKeys ?? false,
|
|
884
943
|
enableClickSelection: params.enableClickSelection ?? false,
|
|
885
944
|
mode: rowSelection === 'single' ? 'singleRow' : 'multiRow',
|
|
945
|
+
...(params.hideSelectColumn && {
|
|
946
|
+
checkboxes: false,
|
|
947
|
+
headerCheckbox: false,
|
|
948
|
+
}),
|
|
886
949
|
}
|
|
887
950
|
: undefined
|
|
888
951
|
}
|
|
952
|
+
selectionColumnDef={selectionColumnDef}
|
|
889
953
|
rowHeight={rowHeight}
|
|
890
954
|
animateRows={params.animateRows ?? false}
|
|
891
955
|
rowClassRules={params.rowClassRules}
|
|
@@ -893,7 +957,7 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
893
957
|
suppressColumnVirtualisation={suppressColumnVirtualization}
|
|
894
958
|
suppressClickEdit={true}
|
|
895
959
|
onGridSizeChanged={onGridSizeChanged}
|
|
896
|
-
onColumnVisible={setInitialContentSize}
|
|
960
|
+
onColumnVisible={() => void setInitialContentSize()}
|
|
897
961
|
onRowDataUpdated={onRowDataUpdated}
|
|
898
962
|
onCellFocused={onCellFocused}
|
|
899
963
|
onCellKeyDown={onCellKeyPress}
|
|
@@ -904,7 +968,6 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
|
|
|
904
968
|
onColumnResized={onColumnResized}
|
|
905
969
|
defaultColDef={defaultColDef}
|
|
906
970
|
columnDefs={columnDefsAdjusted}
|
|
907
|
-
selectionColumnDef={selectionColumnDef}
|
|
908
971
|
rowData={rowData}
|
|
909
972
|
postSortRows={params.onRowDragEnd || !defaultPostSort ? undefined : postSortRows}
|
|
910
973
|
onModelUpdated={onModelUpdated}
|
|
@@ -940,3 +1003,13 @@ const quickFilterParser = (filterStr: string) => {
|
|
|
940
1003
|
// filter is exact matches exactly groups separated by commas
|
|
941
1004
|
return filterStr.split(',').map((str) => str.trim());
|
|
942
1005
|
};
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* Columns to indicate to user when they debug why things are broken in the default comparator if their valueFormatter
|
|
1009
|
+
* is too complicated.
|
|
1010
|
+
*/
|
|
1011
|
+
const NotAGridValueFormatterCall = {
|
|
1012
|
+
column: 'Default comparator has no access to column, write your own comparator' as unknown,
|
|
1013
|
+
api: 'Default comparator has no access to api, write your own comparator' as unknown,
|
|
1014
|
+
context: 'Default comparator has no access to context, write your own comparator' as unknown,
|
|
1015
|
+
};
|
|
@@ -45,7 +45,7 @@ export interface GridContextType<TData extends GridBaseRow> {
|
|
|
45
45
|
ensureRowVisible: (id: number | string) => boolean;
|
|
46
46
|
ensureSelectedRowIsVisible: () => void;
|
|
47
47
|
getFirstRowId: () => number;
|
|
48
|
-
autoSizeColumns: (props?: AutoSizeColumnsProps) => AutoSizeColumnsResult
|
|
48
|
+
autoSizeColumns: (props?: AutoSizeColumnsProps) => Promise<AutoSizeColumnsResult>;
|
|
49
49
|
sizeColumnsToFit: (paramsOrGridWidth?: ISizeColumnsToFitParams) => void;
|
|
50
50
|
startCellEditing: ({ rowId, colId }: StartCellEditingProps) => Promise<void>;
|
|
51
51
|
// Restores the previous focus after cell editing
|
|
@@ -153,9 +153,9 @@ export const GridContext = createContext<GridContextType<any>>({
|
|
|
153
153
|
console.error('no context provider for getFirstRowId');
|
|
154
154
|
return -1;
|
|
155
155
|
},
|
|
156
|
-
autoSizeColumns: () => {
|
|
156
|
+
autoSizeColumns: async () => {
|
|
157
157
|
console.error('no context provider for autoSizeColumns');
|
|
158
|
-
return null;
|
|
158
|
+
return Promise.resolve(null);
|
|
159
159
|
},
|
|
160
160
|
sizeColumnsToFit: () => {
|
|
161
161
|
console.error('no context provider for autoSizeAllColumns');
|
|
@@ -434,50 +434,69 @@ export const GridContextProvider = <TData extends GridBaseRow>(props: PropsWithC
|
|
|
434
434
|
* Then we size the flexed columns.
|
|
435
435
|
*/
|
|
436
436
|
const autoSizeColumns = useCallback(
|
|
437
|
-
({ skipHeader, colIds, userSizedColIds }: AutoSizeColumnsProps = {}): AutoSizeColumnsResult => {
|
|
437
|
+
async ({ skipHeader, colIds, userSizedColIds }: AutoSizeColumnsProps = {}): Promise<AutoSizeColumnsResult> => {
|
|
438
438
|
if (!gridApi || !gridApi.getColumnState()) {
|
|
439
439
|
return null;
|
|
440
440
|
}
|
|
441
|
-
const colIdsSet = colIds instanceof Set ? colIds : new Set(colIds);
|
|
442
441
|
|
|
443
|
-
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
442
|
+
try {
|
|
443
|
+
const colIdsSet = colIds instanceof Set ? colIds : new Set(colIds);
|
|
444
|
+
|
|
445
|
+
const getVisibleColStates = () => {
|
|
446
|
+
const colStates = gridApi.getColumnState();
|
|
447
|
+
return colStates.filter((colState) => {
|
|
448
|
+
const colId = colState.colId;
|
|
449
|
+
return (isEmpty(colIdsSet) || colIdsSet.has(colId)) && !userSizedColIds?.has(colId);
|
|
450
|
+
});
|
|
451
|
+
};
|
|
452
|
+
const getFlexColStates = () => getVisibleColStates().filter(colStateFlexed);
|
|
453
|
+
const getNonFlexColStates = () => getVisibleColStates().filter(colStateNotFlexed);
|
|
454
|
+
|
|
455
|
+
// You cannot autosize flex columns it will break layout randomly
|
|
456
|
+
// So, a flex column is assumed to be 150 wide, unless width is provided
|
|
457
|
+
const flexColumns = getFlexColStates().map(colStateId);
|
|
458
|
+
let width = 0;
|
|
459
|
+
flexColumns.forEach((colId) => {
|
|
460
|
+
const colDef = gridApi.getColumnDef(colId) as { flexAutoSizeWidth?: number } | undefined;
|
|
461
|
+
width += colDef?.flexAutoSizeWidth ?? 200;
|
|
448
462
|
});
|
|
449
|
-
};
|
|
450
|
-
const getFlexColStates = () => getVisibleColStates().filter(colStateFlexed);
|
|
451
|
-
const getNonFlexColStates = () => getVisibleColStates().filter(colStateNotFlexed);
|
|
452
|
-
|
|
453
|
-
// If we don't reset the flex columns auto size it causes issues with random resizing of flex columns
|
|
454
|
-
let flexColumns = getFlexColStates();
|
|
455
|
-
let width = 0;
|
|
456
|
-
if (!isEmpty(flexColumns)) {
|
|
457
|
-
gridApi.autoSizeColumns(flexColumns.map(colStateId), skipHeader);
|
|
458
|
-
const flexColumnIds = flexColumns.map(colStateId);
|
|
459
|
-
flexColumns = getVisibleColStates().filter((colState) => flexColumnIds.includes(colState.colId));
|
|
460
|
-
width += sumBy(
|
|
461
|
-
flexColumns.filter((col) => !col.hide),
|
|
462
|
-
'width',
|
|
463
|
-
);
|
|
464
|
-
gridApi.resetColumnState();
|
|
465
|
-
}
|
|
466
463
|
|
|
467
|
-
|
|
468
|
-
if (!isEmpty(nonFlexColumns)) {
|
|
469
|
-
gridApi.autoSizeColumns(nonFlexColumns.map(colStateId), skipHeader);
|
|
464
|
+
const nonFlexColumns = getNonFlexColStates();
|
|
470
465
|
const nonFlexColumnIds = nonFlexColumns.map(colStateId);
|
|
471
|
-
|
|
472
|
-
width += sumBy(
|
|
473
|
-
nonFlexColumns.filter((col) => !col.hide),
|
|
474
|
-
'width',
|
|
475
|
-
);
|
|
476
|
-
}
|
|
466
|
+
gridApi.autoSizeColumns({ colIds: nonFlexColumnIds, skipHeader });
|
|
477
467
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
468
|
+
const calcSubWidth = () => {
|
|
469
|
+
const updatedFlexColumns = getVisibleColStates().filter((colState) =>
|
|
470
|
+
nonFlexColumnIds.includes(colState.colId),
|
|
471
|
+
);
|
|
472
|
+
return sumBy(
|
|
473
|
+
updatedFlexColumns.filter((col) => !col.hide),
|
|
474
|
+
'width',
|
|
475
|
+
);
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
// ag-grid updates widths asynchronously, so we wait here for the default calculated width to change
|
|
479
|
+
let lastSubWidth = calcSubWidth();
|
|
480
|
+
const endTime = Date.now() + 1000;
|
|
481
|
+
while (Date.now() < endTime) {
|
|
482
|
+
await wait(40);
|
|
483
|
+
const newSubWidth = calcSubWidth();
|
|
484
|
+
if (lastSubWidth !== newSubWidth) {
|
|
485
|
+
lastSubWidth = newSubWidth;
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
width += lastSubWidth;
|
|
491
|
+
|
|
492
|
+
gridApi.sizeColumnsToFit();
|
|
493
|
+
return {
|
|
494
|
+
width,
|
|
495
|
+
};
|
|
496
|
+
} catch (ex) {
|
|
497
|
+
console.info('autosize failed', ex);
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
481
500
|
},
|
|
482
501
|
[gridApi],
|
|
483
502
|
);
|
|
@@ -35,13 +35,11 @@ const GridPopoutEditGenericTemplate: StoryFn<typeof Grid<IFormTestRow>> = (props
|
|
|
35
35
|
GridCell({
|
|
36
36
|
field: 'id',
|
|
37
37
|
headerName: 'Id',
|
|
38
|
-
flex: 2,
|
|
39
38
|
}),
|
|
40
39
|
GridCell(
|
|
41
40
|
{
|
|
42
41
|
field: 'name',
|
|
43
42
|
headerName: 'Popout Generic Edit',
|
|
44
|
-
flex: 1,
|
|
45
43
|
},
|
|
46
44
|
{
|
|
47
45
|
multiEdit: true,
|
|
@@ -55,7 +53,13 @@ const GridPopoutEditGenericTemplate: StoryFn<typeof Grid<IFormTestRow>> = (props
|
|
|
55
53
|
|
|
56
54
|
const [rowData] = useState([
|
|
57
55
|
{ id: 1000, name: 'IS IS DP12345', nameType: 'IS', numba: 'IX', plan: 'DP 12345' },
|
|
58
|
-
{
|
|
56
|
+
{
|
|
57
|
+
id: 1001,
|
|
58
|
+
name: 'PEG V SD523',
|
|
59
|
+
nameType: 'PEG',
|
|
60
|
+
numba: 'V',
|
|
61
|
+
plan: 'SD 523',
|
|
62
|
+
},
|
|
59
63
|
] as IFormTestRow[]);
|
|
60
64
|
|
|
61
65
|
return (
|
|
@@ -64,6 +68,8 @@ const GridPopoutEditGenericTemplate: StoryFn<typeof Grid<IFormTestRow>> = (props
|
|
|
64
68
|
externalSelectedItems={externalSelectedItems}
|
|
65
69
|
setExternalSelectedItems={setExternalSelectedItems}
|
|
66
70
|
columnDefs={columnDefs}
|
|
71
|
+
hideSelectColumn={true}
|
|
72
|
+
selectable={true}
|
|
67
73
|
rowData={rowData}
|
|
68
74
|
domLayout={'autoHeight'}
|
|
69
75
|
/>
|