@linzjs/step-ag-grid 29.11.3 → 29.11.4

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.
@@ -38,7 +38,7 @@ export interface GridContextType<TData extends GridBaseRow> {
38
38
  ensureRowVisible: (id: number | string) => boolean;
39
39
  ensureSelectedRowIsVisible: () => void;
40
40
  getFirstRowId: () => number;
41
- autoSizeColumns: (props?: AutoSizeColumnsProps) => AutoSizeColumnsResult;
41
+ autoSizeColumns: (props?: AutoSizeColumnsProps) => Promise<AutoSizeColumnsResult>;
42
42
  sizeColumnsToFit: (paramsOrGridWidth?: ISizeColumnsToFitParams) => void;
43
43
  startCellEditing: ({ rowId, colId }: StartCellEditingProps) => Promise<void>;
44
44
  resetFocusedCellAfterCellEditing: () => void;
@@ -509,9 +509,9 @@ const GridContext = React.createContext({
509
509
  console.error('no context provider for getFirstRowId');
510
510
  return -1;
511
511
  },
512
- autoSizeColumns: () => {
512
+ autoSizeColumns: async () => {
513
513
  console.error('no context provider for autoSizeColumns');
514
- return null;
514
+ return Promise.resolve(null);
515
515
  },
516
516
  sizeColumnsToFit: () => {
517
517
  console.error('no context provider for autoSizeAllColumns');
@@ -2787,66 +2787,77 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
2787
2787
  const needsAutoSize = React.useRef(true);
2788
2788
  const autoSizeResultRef = React.useRef(null);
2789
2789
  const prevRowsVisibleRef = React.useRef(false);
2790
- const setInitialContentSize = React.useCallback(() => {
2791
- if (!gridDivRef.current?.clientWidth || rowData == null) {
2792
- // Don't resize grids if they are offscreen as it doesn't work.
2793
- needsAutoSize.current = true;
2794
- return;
2795
- }
2796
- const gridRendered = gridRenderState();
2797
- if (gridRendered === null) {
2798
- // Don't resize until grid has rendered, or it has 0 rows.
2799
- needsAutoSize.current = true;
2790
+ const initialContentSizeInProgressRef = React.useRef(false);
2791
+ const setInitialContentSize = React.useCallback(async () => {
2792
+ if (initialContentSizeInProgressRef.current) {
2800
2793
  return;
2801
2794
  }
2802
- // 1. First we autosize to get the size of the columns on an infinite grid.
2803
- if (sizeColumns === 'auto' || sizeColumns === 'auto-skip-headers') {
2804
- // You can't skip headers until the grid has content
2805
- const rowsVisible = gridRendered === 'rows-visible';
2806
- const skipHeader = sizeColumns === 'auto-skip-headers' && rowsVisible;
2807
- // If grid was empty and now has content we need to autosize
2808
- if (rowsVisible !== prevRowsVisibleRef.current && rowsVisible) {
2809
- prevRowsVisibleRef.current = rowsVisible;
2810
- autoSizeResultRef.current = null;
2795
+ initialContentSizeInProgressRef.current = true;
2796
+ try {
2797
+ needsAutoSize.current = false;
2798
+ if (!gridDivRef.current?.clientWidth || rowData == null) {
2799
+ // Don't resize grids if they are offscreen as it doesn't work.
2800
+ needsAutoSize.current = true;
2801
+ return;
2811
2802
  }
2812
- const autoSizeResult = autoSizeResultRef.current ??
2813
- autoSizeColumns({
2814
- skipHeader,
2815
- userSizedColIds: new Set(userSizedColIds.current.keys()),
2816
- });
2817
- // Auto-size failed retry later
2818
- if (!autoSizeResult) {
2803
+ const gridRendered = gridRenderState();
2804
+ if (gridRendered === null) {
2805
+ // Don't resize until grid has rendered, or it has 0 rows.
2819
2806
  needsAutoSize.current = true;
2820
2807
  return;
2821
2808
  }
2822
- autoSizeResultRef.current = autoSizeResult;
2823
- // Calculate the auto-sized width, limit it to maxInitialWidth
2824
- autoSizeResult.width = maxInitialWidth ? Math.min(autoSizeResult.width, maxInitialWidth) : autoSizeResult.width;
2825
- if (gridRendered === 'empty') {
2826
- // If the grid is empty we still do an onContentSize callback, we will do another callback when grid has data
2827
- // We don't do this callback if we have previously had row data, or have already called back for empty
2828
- if (!hasSetContentSizeEmpty.current && !hasSetContentSize.current) {
2829
- hasSetContentSizeEmpty.current = true;
2830
- params.onContentSize?.(autoSizeResult);
2809
+ // 1. First we autosize to get the size of the columns on an infinite grid.
2810
+ if (sizeColumns === 'auto' || sizeColumns === 'auto-skip-headers') {
2811
+ // You can't skip headers until the grid has content
2812
+ const rowsVisible = gridRendered === 'rows-visible';
2813
+ const skipHeader = sizeColumns === 'auto-skip-headers' && rowsVisible;
2814
+ // If grid was empty and now has content we need to autosize
2815
+ if (rowsVisible !== prevRowsVisibleRef.current && rowsVisible) {
2816
+ prevRowsVisibleRef.current = rowsVisible;
2817
+ autoSizeResultRef.current = null;
2831
2818
  }
2832
- }
2833
- else if (gridRendered === 'rows-visible') {
2834
- // we have rows now so callback grid size
2835
- if (!hasSetContentSize.current) {
2836
- hasSetContentSize.current = true;
2837
- params.onContentSize?.(autoSizeResult);
2819
+ const autoSizeResult = autoSizeResultRef.current ??
2820
+ (await autoSizeColumns({
2821
+ skipHeader,
2822
+ userSizedColIds: new Set(userSizedColIds.current.keys()),
2823
+ }));
2824
+ // Auto-size failed retry later
2825
+ if (!autoSizeResult) {
2826
+ needsAutoSize.current = true;
2827
+ return;
2828
+ }
2829
+ autoSizeResultRef.current = autoSizeResult;
2830
+ // Calculate the auto-sized width, limit it to maxInitialWidth
2831
+ autoSizeResult.width = maxInitialWidth ? Math.min(autoSizeResult.width, maxInitialWidth) : autoSizeResult.width;
2832
+ if (gridRendered === 'empty') {
2833
+ // If the grid is empty we still do an onContentSize callback, we will do another callback when grid has data
2834
+ // We don't do this callback if we have previously had row data, or have already called back for empty
2835
+ if (!hasSetContentSizeEmpty.current && !hasSetContentSize.current) {
2836
+ hasSetContentSizeEmpty.current = true;
2837
+ params.onContentSize?.(autoSizeResult);
2838
+ }
2839
+ }
2840
+ else if (gridRendered === 'rows-visible') {
2841
+ // we have rows now so callback grid size
2842
+ if (!hasSetContentSize.current) {
2843
+ hasSetContentSize.current = true;
2844
+ params.onContentSize?.(autoSizeResult);
2845
+ }
2846
+ }
2847
+ else {
2848
+ // It should be impossible to get here
2849
+ console.error('Unknown value returned from hasGridRendered');
2838
2850
  }
2839
2851
  }
2840
2852
  else {
2841
- // It should be impossible to get here
2842
- console.error('Unknown value returned from hasGridRendered');
2853
+ sizeColumnsToFit();
2843
2854
  }
2855
+ setAutoSized(true);
2856
+ needsAutoSize.current = false;
2844
2857
  }
2845
- else {
2846
- sizeColumnsToFit();
2858
+ finally {
2859
+ initialContentSizeInProgressRef.current = false;
2847
2860
  }
2848
- setAutoSized(true);
2849
- needsAutoSize.current = false;
2850
2861
  }, [autoSizeColumns, gridRenderState, maxInitialWidth, params, rowData, sizeColumns, sizeColumnsToFit]);
2851
2862
  const lastOwnerDocumentRef = React.useRef();
2852
2863
  const wasVisibleRef = React.useRef(false);
@@ -2880,8 +2891,7 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
2880
2891
  }
2881
2892
  if (needsAutoSize.current ||
2882
2893
  (!hasSetContentSize.current && (sizeColumns === 'auto' || sizeColumns === 'auto-skip-headers'))) {
2883
- needsAutoSize.current = false;
2884
- setInitialContentSize();
2894
+ void setInitialContentSize();
2885
2895
  }
2886
2896
  }, 200);
2887
2897
  /**
@@ -3010,11 +3020,13 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3010
3020
  if (previousRowDataLength.current !== length) {
3011
3021
  // We need to autosize all cells again
3012
3022
  autoSizeResultRef.current = null;
3013
- setInitialContentSize();
3014
3023
  previousRowDataLength.current = length;
3024
+ void setInitialContentSize();
3025
+ return;
3015
3026
  }
3016
- if (lastUpdatedDep.current === updatedDep || lodashEs.isEmpty(colIdsEdited.current))
3027
+ if (lastUpdatedDep.current === updatedDep || lodashEs.isEmpty(colIdsEdited.current)) {
3017
3028
  return;
3029
+ }
3018
3030
  lastUpdatedDep.current = updatedDep;
3019
3031
  // Don't update while there are spinners
3020
3032
  if (anyUpdating()) {
@@ -3022,7 +3034,7 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3022
3034
  }
3023
3035
  const skipHeader = sizeColumns === 'auto-skip-headers';
3024
3036
  if ((sizeColumns === 'auto' || sizeColumns === 'auto-skip-headers') && hasSetContentSize.current) {
3025
- autoSizeColumns({
3037
+ void autoSizeColumns({
3026
3038
  skipHeader,
3027
3039
  userSizedColIds: new Set(userSizedColIds.current.keys()),
3028
3040
  colIds: colIdsEdited.current,
@@ -3112,11 +3124,21 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3112
3124
  ...colDef,
3113
3125
  children: colDef.children.map((colDef) => adjustColDefOrGroup(colDef)),
3114
3126
  });
3115
- const adjustColDef = (colDef) => ({
3116
- ...colDef,
3117
- suppressSizeToFit: (sizeColumns === 'auto' || sizeColumns === 'auto-skip-headers') && !colDef.flex,
3118
- sortable: colDef.sortable && params.defaultColDef?.sortable !== false,
3119
- });
3127
+ const adjustColDef = (colDef) => {
3128
+ const flex = colDef.flex ?? (sizeColumns === 'fit' ? 1 : undefined);
3129
+ return {
3130
+ ...colDef,
3131
+ // You cannot pass a width to a flex
3132
+ width: !!colDef.flex ? undefined : colDef.width,
3133
+ flexAutoSizeWidth: colDef.width,
3134
+ // If this is allowed flex columns don't size based on flex
3135
+ suppressSizeToFit: true,
3136
+ // Auto-sizing flex columns breaks everything
3137
+ flex,
3138
+ suppressAutoSize: !!flex,
3139
+ sortable: colDef.sortable && params.defaultColDef?.sortable !== false,
3140
+ };
3141
+ };
3120
3142
  return columnDefs.map((colDef) => adjustColDefOrGroup(colDef));
3121
3143
  }, [columnDefs, params.defaultColDef?.sortable, sizeColumns]);
3122
3144
  /**
@@ -3141,7 +3163,7 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3141
3163
  if (sizeColumns === 'auto' || skipHeader) {
3142
3164
  lodashEs.defer(() => {
3143
3165
  if (hasSetContentSize.current) {
3144
- autoSizeColumns({
3166
+ void autoSizeColumns({
3145
3167
  skipHeader,
3146
3168
  userSizedColIds: new Set(userSizedColIds.current.keys()),
3147
3169
  colIds: colIdsEdited.current,
@@ -3296,7 +3318,7 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3296
3318
  }, [columnDefs, params.loading, params.noRowsMatchingOverlayText, params.noRowsOverlayText, rowData, rowHeight]);
3297
3319
  const selectionColumnDef = React.useMemo(() => {
3298
3320
  // Note this has to be 1 for hidden otherwise ag-grid does crazy things whilst resizing
3299
- const selectWidth = params.hideSelectColumn ? 0 : selectable && params.onRowDragEnd ? 76 : 48;
3321
+ const selectWidth = params.onRowDragEnd ? 76 : 48;
3300
3322
  return {
3301
3323
  suppressNavigable: params.hideSelectColumn,
3302
3324
  rowDrag: !!params.onRowDragEnd,
@@ -3306,6 +3328,8 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3306
3328
  headerComponentParams: {
3307
3329
  exportable: false,
3308
3330
  },
3331
+ suppressAutoSize: true,
3332
+ suppressSizeToFit: true,
3309
3333
  headerClass: clsx('ag-header-hide-default-select', params.onRowDragEnd && 'ag-header-select-draggable'),
3310
3334
  headerComponent: rowSelection == 'multiple' ? GridHeaderSelect : undefined,
3311
3335
  suppressHeaderKeyboardEvent: (e) => {
@@ -3336,7 +3360,7 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3336
3360
  selectable,
3337
3361
  ]);
3338
3362
  const onGridSizeChanged = React.useCallback((event) => {
3339
- if (sizeColumns === 'fit') {
3363
+ if (sizeColumns === 'fit' || (['auto', 'auto-skip-headers'].includes(sizeColumns) && hasSetContentSize.current)) {
3340
3364
  event.api.sizeColumnsToFit();
3341
3365
  }
3342
3366
  }, [sizeColumns]);
@@ -3345,8 +3369,12 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3345
3369
  enableSelectionWithoutKeys: params.enableSelectionWithoutKeys ?? false,
3346
3370
  enableClickSelection: params.enableClickSelection ?? false,
3347
3371
  mode: rowSelection === 'single' ? 'singleRow' : 'multiRow',
3372
+ ...(params.hideSelectColumn && {
3373
+ checkboxes: false,
3374
+ headerCheckbox: false,
3375
+ }),
3348
3376
  }
3349
- : undefined, rowHeight: rowHeight, animateRows: params.animateRows ?? false, rowClassRules: params.rowClassRules, getRowId: getRowId, suppressColumnVirtualisation: suppressColumnVirtualization, suppressClickEdit: true, onGridSizeChanged: onGridSizeChanged, onColumnVisible: setInitialContentSize, onRowDataUpdated: onRowDataUpdated, onCellFocused: onCellFocused, onCellKeyDown: onCellKeyPress, onCellClicked: onCellClicked, onCellDoubleClicked: onCellDoubleClick, onCellEditingStopped: onCellEditingStopped, domLayout: params.domLayout, onColumnResized: onColumnResized, defaultColDef: defaultColDef, columnDefs: columnDefsAdjusted, selectionColumnDef: selectionColumnDef, rowData: rowData, postSortRows: params.onRowDragEnd || !defaultPostSort ? undefined : postSortRows, onModelUpdated: onModelUpdated, onGridReady: onGridReady, onSortChanged: ensureSelectedRowIsVisible, quickFilterParser: quickFilterParser, onSelectionChanged: synchroniseExternalStateToGridSelection, onColumnMoved: params.onColumnMoved, noRowsOverlayComponent: noRowsOverlayComponent, alwaysShowVerticalScroll: params.alwaysShowVerticalScroll, isExternalFilterPresent: isExternalFilterPresent, doesExternalFilterPass: doesExternalFilterPass, maintainColumnOrder: true, preventDefaultOnContextMenu: true, onCellContextMenu: gridContextMenu.cellContextMenu, rowDragText: params.rowDragText, onRowDragCancel: clearHighlightRowClasses, onRowDragMove: onRowDragMove, onRowDragEnd: onRowDragEnd, suppressCellFocus: params.suppressCellFocus, pinnedTopRowData: params.pinnedTopRowData, pinnedBottomRowData: params.pinnedBottomRowData, onRowClicked: params.onRowClicked, onRowDoubleClicked: params.onRowDoubleClicked, suppressStartEditOnTab: true }) })] }));
3377
+ : undefined, selectionColumnDef: selectionColumnDef, rowHeight: rowHeight, animateRows: params.animateRows ?? false, rowClassRules: params.rowClassRules, getRowId: getRowId, suppressColumnVirtualisation: suppressColumnVirtualization, suppressClickEdit: true, onGridSizeChanged: onGridSizeChanged, onColumnVisible: () => void setInitialContentSize(), onRowDataUpdated: onRowDataUpdated, onCellFocused: onCellFocused, onCellKeyDown: onCellKeyPress, onCellClicked: onCellClicked, onCellDoubleClicked: onCellDoubleClick, onCellEditingStopped: onCellEditingStopped, domLayout: params.domLayout, onColumnResized: onColumnResized, defaultColDef: defaultColDef, columnDefs: columnDefsAdjusted, rowData: rowData, postSortRows: params.onRowDragEnd || !defaultPostSort ? undefined : postSortRows, onModelUpdated: onModelUpdated, onGridReady: onGridReady, onSortChanged: ensureSelectedRowIsVisible, quickFilterParser: quickFilterParser, onSelectionChanged: synchroniseExternalStateToGridSelection, onColumnMoved: params.onColumnMoved, noRowsOverlayComponent: noRowsOverlayComponent, alwaysShowVerticalScroll: params.alwaysShowVerticalScroll, isExternalFilterPresent: isExternalFilterPresent, doesExternalFilterPass: doesExternalFilterPass, maintainColumnOrder: true, preventDefaultOnContextMenu: true, onCellContextMenu: gridContextMenu.cellContextMenu, rowDragText: params.rowDragText, onRowDragCancel: clearHighlightRowClasses, onRowDragMove: onRowDragMove, onRowDragEnd: onRowDragEnd, suppressCellFocus: params.suppressCellFocus, pinnedTopRowData: params.pinnedTopRowData, pinnedBottomRowData: params.pinnedBottomRowData, onRowClicked: params.onRowClicked, onRowDoubleClicked: params.onRowDoubleClicked, suppressStartEditOnTab: true }) })] }));
3350
3378
  };
3351
3379
  const quickFilterParser = (filterStr) => {
3352
3380
  // filter is exact matches exactly groups separated by commas
@@ -5610,7 +5638,7 @@ const GridContextProvider = (props) => {
5610
5638
  * If you don't clear flex column widths ag-grid gets confused and does random sizing's.
5611
5639
  * Then we size the flexed columns.
5612
5640
  */
5613
- const autoSizeColumns = React.useCallback(({ skipHeader, colIds, userSizedColIds } = {}) => {
5641
+ const autoSizeColumns = React.useCallback(async ({ skipHeader, colIds, userSizedColIds } = {}) => {
5614
5642
  if (!gridApi || !gridApi.getColumnState()) {
5615
5643
  return null;
5616
5644
  }
@@ -5624,23 +5652,34 @@ const GridContextProvider = (props) => {
5624
5652
  };
5625
5653
  const getFlexColStates = () => getVisibleColStates().filter(colStateFlexed);
5626
5654
  const getNonFlexColStates = () => getVisibleColStates().filter(colStateNotFlexed);
5627
- // If we don't reset the flex columns auto size it causes issues with random resizing of flex columns
5628
- let flexColumns = getFlexColStates();
5655
+ // You cannot autosize flex columns it will break layout randomly
5656
+ // So, a flex column is assumed to be 150 wide, unless width is provided
5657
+ const flexColumns = getFlexColStates().map(colStateId);
5629
5658
  let width = 0;
5630
- if (!lodashEs.isEmpty(flexColumns)) {
5631
- gridApi.autoSizeColumns(flexColumns.map(colStateId), skipHeader);
5632
- const flexColumnIds = flexColumns.map(colStateId);
5633
- flexColumns = getVisibleColStates().filter((colState) => flexColumnIds.includes(colState.colId));
5634
- width += lodashEs.sumBy(flexColumns.filter((col) => !col.hide), 'width');
5635
- gridApi.resetColumnState();
5636
- }
5637
- let nonFlexColumns = getNonFlexColStates();
5638
- if (!lodashEs.isEmpty(nonFlexColumns)) {
5639
- gridApi.autoSizeColumns(nonFlexColumns.map(colStateId), skipHeader);
5640
- const nonFlexColumnIds = nonFlexColumns.map(colStateId);
5641
- nonFlexColumns = getVisibleColStates().filter((colState) => nonFlexColumnIds.includes(colState.colId));
5642
- width += lodashEs.sumBy(nonFlexColumns.filter((col) => !col.hide), 'width');
5659
+ flexColumns.forEach((colId) => {
5660
+ const colDef = gridApi.getColumnDef(colId);
5661
+ width += colDef?.flexAutoSizeWidth ?? 200;
5662
+ });
5663
+ const nonFlexColumns = getNonFlexColStates();
5664
+ const nonFlexColumnIds = nonFlexColumns.map(colStateId);
5665
+ gridApi.autoSizeColumns({ colIds: nonFlexColumnIds, skipHeader });
5666
+ const calcSubWidth = () => {
5667
+ const updatedFlexColumns = getVisibleColStates().filter((colState) => nonFlexColumnIds.includes(colState.colId));
5668
+ return lodashEs.sumBy(updatedFlexColumns.filter((col) => !col.hide), 'width');
5669
+ };
5670
+ // ag-grid updates widths asynchronously, so we wait here for the default calculated width to change
5671
+ let lastSubWidth = calcSubWidth();
5672
+ const endTime = Date.now() + 1000;
5673
+ while (Date.now() < endTime) {
5674
+ await wait(40);
5675
+ const newSubWidth = calcSubWidth();
5676
+ if (lastSubWidth !== newSubWidth) {
5677
+ lastSubWidth = newSubWidth;
5678
+ break;
5679
+ }
5643
5680
  }
5681
+ width += lastSubWidth;
5682
+ gridApi.sizeColumnsToFit();
5644
5683
  return {
5645
5684
  width,
5646
5685
  };