@linzjs/step-ag-grid 29.11.1 → 29.11.3

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": "29.11.1",
5
+ "version": "29.11.3",
6
6
  "keywords": [
7
7
  "aggrid",
8
8
  "ag-grid",
@@ -14,7 +14,7 @@ import {
14
14
  GetRowIdParams,
15
15
  GridOptions,
16
16
  GridReadyEvent,
17
- IColumnLimit,
17
+ GridSizeChangedEvent,
18
18
  ModelUpdatedEvent,
19
19
  ModuleRegistry,
20
20
  RowClickedEvent,
@@ -203,7 +203,6 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
203
203
  const hasSetContentSizeEmpty = useRef(false);
204
204
  const needsAutoSize = useRef(true);
205
205
 
206
- const requiresInitialSizeToFitRef = useRef(false);
207
206
  const autoSizeResultRef = useRef<AutoSizeColumnsResult | null>(null);
208
207
  const prevRowsVisibleRef = useRef(false);
209
208
  const setInitialContentSize = useCallback(() => {
@@ -250,14 +249,12 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
250
249
  // We don't do this callback if we have previously had row data, or have already called back for empty
251
250
  if (!hasSetContentSizeEmpty.current && !hasSetContentSize.current) {
252
251
  hasSetContentSizeEmpty.current = true;
253
- requiresInitialSizeToFitRef.current = true;
254
252
  params.onContentSize?.(autoSizeResult);
255
253
  }
256
254
  } else if (gridRendered === 'rows-visible') {
257
255
  // we have rows now so callback grid size
258
256
  if (!hasSetContentSize.current) {
259
257
  hasSetContentSize.current = true;
260
- requiresInitialSizeToFitRef.current = true;
261
258
  params.onContentSize?.(autoSizeResult);
262
259
  }
263
260
  } else {
@@ -607,9 +604,10 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
607
604
  * When cell editing has completed the colId as needing auto-sizing
608
605
  */
609
606
  useEffect(() => {
610
- if (lastUpdatedDep.current === updatedDep) return;
607
+ if (lastUpdatedDep.current === updatedDep) {
608
+ return;
609
+ }
611
610
  lastUpdatedDep.current = updatedDep;
612
-
613
611
  const colIds = updatingCols();
614
612
  // Updating possibly completed
615
613
  if (isEmpty(colIds)) {
@@ -648,32 +646,6 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
648
646
  prevLoading.current = newLoading;
649
647
  }, [params.loading, rowData, showNoRowsOverlay]);
650
648
 
651
- /**
652
- * Resize columns to fit if required on window/container resize
653
- */
654
- const onGridResize = useCallback(
655
- (event: AgGridEvent<TData>) => {
656
- if (sizeColumns === 'fit' || (!hasSetContentSizeEmpty.current && !hasSetContentSize.current)) {
657
- return;
658
- }
659
- if (sizeColumns !== 'none') {
660
- // Flex columns can expand to fit after resize, but they cannot shrink less than use resized value
661
- // Double click column resize handle to reset this behaviour
662
- const columnLimits = [
663
- ...userSizedColIds.current.entries().map(
664
- ([c, w]): IColumnLimit => ({
665
- key: c,
666
- minWidth: w,
667
- maxWidth: w,
668
- }),
669
- ),
670
- ];
671
- defer(() => event.api.sizeColumnsToFit({ columnLimits }));
672
- }
673
- },
674
- [sizeColumns],
675
- );
676
-
677
649
  /**
678
650
  * Set of column I'd's that are prevented from auto-sizing as they are user set
679
651
  */
@@ -682,33 +654,29 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
682
654
  /**
683
655
  * Lock/unlock column width on user edit/reset.
684
656
  */
685
- const onColumnResized = useCallback(
686
- (e: ColumnResizedEvent) => {
687
- const colId = e.column?.getColId();
688
- if (colId == null) {
689
- return;
690
- }
691
- const width = e.column?.getActualWidth();
692
- if (width == null) {
693
- return;
694
- }
695
-
696
- switch (e.source) {
697
- case 'uiColumnResized':
698
- userSizedColIds.current.set(colId, width);
699
- const colDef = e.column?.getColDef();
657
+ const onColumnResized = useCallback((e: ColumnResizedEvent) => {
658
+ const colId = e.column?.getColId();
659
+ if (colId == null) {
660
+ return;
661
+ }
662
+ const width = e.column?.getActualWidth();
663
+ if (width == null) {
664
+ return;
665
+ }
666
+ switch (e.source) {
667
+ case 'uiColumnResized':
668
+ userSizedColIds.current.set(colId, width);
669
+ /*const colDef = e.column?.getColDef();
700
670
  if (!colDef?.flex) {
701
671
  onGridResize(e);
702
- }
703
- break;
704
- case 'autosizeColumns':
705
- userSizedColIds.current.delete(colId);
706
- onGridResize(e);
707
- break;
708
- }
709
- },
710
- [onGridResize],
711
- );
672
+ }*/
673
+ break;
674
+ case 'autosizeColumns':
675
+ userSizedColIds.current.delete(colId);
676
+ //onGridResize(e);
677
+ break;
678
+ }
679
+ }, []);
712
680
 
713
681
  const gridContextMenu = useGridContextMenu({ contextMenu: params.contextMenu, contextMenuSelectRow });
714
682
 
@@ -885,6 +853,15 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
885
853
  selectable,
886
854
  ]);
887
855
 
856
+ const onGridSizeChanged = useCallback(
857
+ (event: GridSizeChangedEvent<TData>) => {
858
+ if (sizeColumns === 'fit') {
859
+ event.api.sizeColumnsToFit();
860
+ }
861
+ },
862
+ [sizeColumns],
863
+ );
864
+
888
865
  return (
889
866
  <div
890
867
  data-testid={dataTestId}
@@ -909,22 +886,13 @@ export const Grid = <TData extends GridBaseRow = GridBaseRow>({
909
886
  }
910
887
  : undefined
911
888
  }
912
- onDisplayedColumnsChanged={() => {
913
- // This happens after an autosize event, if we don't wait for it size columns to fit doesn't work
914
- if (requiresInitialSizeToFitRef.current) {
915
- requiresInitialSizeToFitRef.current = false;
916
- delay(() => {
917
- sizeColumnsToFit();
918
- }, 200);
919
- }
920
- }}
921
889
  rowHeight={rowHeight}
922
890
  animateRows={params.animateRows ?? false}
923
891
  rowClassRules={params.rowClassRules}
924
892
  getRowId={getRowId}
925
- onGridSizeChanged={onGridResize}
926
893
  suppressColumnVirtualisation={suppressColumnVirtualization}
927
894
  suppressClickEdit={true}
895
+ onGridSizeChanged={onGridSizeChanged}
928
896
  onColumnVisible={setInitialContentSize}
929
897
  onRowDataUpdated={onRowDataUpdated}
930
898
  onCellFocused={onCellFocused}
@@ -10,20 +10,7 @@ import {
10
10
  RowNode,
11
11
  } from 'ag-grid-community';
12
12
  import debounce from 'debounce-promise';
13
- import {
14
- compact,
15
- defer,
16
- delay,
17
- difference,
18
- filter,
19
- isEmpty,
20
- last,
21
- partition,
22
- pull,
23
- remove,
24
- sortBy,
25
- sumBy,
26
- } from 'lodash-es';
13
+ import { compact, defer, delay, difference, filter, isEmpty, last, pull, remove, sortBy, sumBy } from 'lodash-es';
27
14
  import { PropsWithChildren, ReactElement, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
28
15
 
29
16
  import { ColDefT, GridBaseRow } from '../components';
@@ -34,8 +21,9 @@ import { waitForCondition } from '../utils/waitForCondition';
34
21
  import { AutoSizeColumnsProps, AutoSizeColumnsResult, GridContext, GridFilterExternal } from './GridContext';
35
22
  import { GridUpdatingContext } from './GridUpdatingContext';
36
23
 
37
- const colStateId = (colState: ColumnState) => colState.colId;
38
- const colStateFlexed = (colState: ColumnState) => !!colState.flex;
24
+ export const colStateId = (colState: ColumnState) => colState.colId;
25
+ export const colStateFlexed = (colState: ColumnState) => !!colState.flex;
26
+ export const colStateNotFlexed = (colState: ColumnState) => !colState.flex;
39
27
 
40
28
  /**
41
29
  * Context for AgGrid operations.
@@ -451,27 +439,44 @@ export const GridContextProvider = <TData extends GridBaseRow>(props: PropsWithC
451
439
  return null;
452
440
  }
453
441
  const colIdsSet = colIds instanceof Set ? colIds : new Set(colIds);
454
- const colStates = gridApi.getColumnState();
455
442
 
456
- const relevantCols = colStates?.filter((colState) => {
457
- const colId = colState.colId;
458
- return (isEmpty(colIdsSet) || colIdsSet.has(colId)) && !userSizedColIds?.has(colId);
459
- });
460
- const [flexColumns, nonFlexColumns] = partition(relevantCols, colStateFlexed);
443
+ const getVisibleColStates = () => {
444
+ const colStates = gridApi.getColumnState();
445
+ return colStates.filter((colState) => {
446
+ const colId = colState.colId;
447
+ return (isEmpty(colIdsSet) || colIdsSet.has(colId)) && !userSizedColIds?.has(colId);
448
+ });
449
+ };
450
+ const getFlexColStates = () => getVisibleColStates().filter(colStateFlexed);
451
+ const getNonFlexColStates = () => getVisibleColStates().filter(colStateNotFlexed);
452
+
461
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;
462
456
  if (!isEmpty(flexColumns)) {
463
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
464
  gridApi.resetColumnState();
465
465
  }
466
466
 
467
+ let nonFlexColumns = getNonFlexColStates();
467
468
  if (!isEmpty(nonFlexColumns)) {
468
469
  gridApi.autoSizeColumns(nonFlexColumns.map(colStateId), skipHeader);
470
+ const nonFlexColumnIds = nonFlexColumns.map(colStateId);
471
+ nonFlexColumns = getVisibleColStates().filter((colState) => nonFlexColumnIds.includes(colState.colId));
472
+ width += sumBy(
473
+ nonFlexColumns.filter((col) => !col.hide),
474
+ 'width',
475
+ );
469
476
  }
477
+
470
478
  return {
471
- width: sumBy(
472
- gridApi.getColumnState().filter((col) => !col.hide),
473
- 'width',
474
- ),
479
+ width,
475
480
  };
476
481
  },
477
482
  [gridApi],
@@ -19,13 +19,11 @@ export default {
19
19
  },
20
20
  decorators: [
21
21
  (Story) => (
22
- <div style={{ width: 1024, height: 400 }}>
23
- <GridUpdatingContextProvider>
24
- <GridContextProvider>
25
- <Story />
26
- </GridContextProvider>
27
- </GridUpdatingContextProvider>
28
- </div>
22
+ <GridUpdatingContextProvider>
23
+ <GridContextProvider>
24
+ <Story />
25
+ </GridContextProvider>
26
+ </GridUpdatingContextProvider>
29
27
  ),
30
28
  ],
31
29
  } as Meta<typeof Grid>;