@linzjs/step-ag-grid 17.5.3 → 17.6.1

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.
@@ -20,6 +20,7 @@ function r(e){var t,f,n="";if("string"==typeof e||"number"==typeof e)n+=e;else i
20
20
 
21
21
  const GridContext = createContext({
22
22
  gridReady: false,
23
+ gridRenderState: () => null,
23
24
  getColDef: () => {
24
25
  console.error("no context provider for getColDef");
25
26
  return undefined;
@@ -2323,8 +2324,8 @@ const useGridContextMenu = ({ contextMenuSelectRow, contextMenu: ContextMenu, })
2323
2324
  /**
2324
2325
  * Wrapper for AgGrid to add commonly used functionality.
2325
2326
  */
2326
- const Grid = ({ "data-testid": dataTestId, rowSelection = "multiple", suppressColumnVirtualization = true, theme = "ag-theme-step-default", sizeColumns = "auto", selectColumnPinned = null, contextMenuSelectRow = false, singleClickEdit = false, rowHeight = theme === "ag-theme-step-default" ? 40 : theme === "ag-theme-step-compact" ? 36 : undefined, ...params }) => {
2327
- const { gridReady, setApis, ensureRowVisible, getFirstRowId, selectRowsById, focusByRowById, ensureSelectedRowIsVisible, autoSizeColumns, sizeColumnsToFit, externallySelectedItemsAreInSync, setExternallySelectedItemsAreInSync, isExternalFilterPresent, doesExternalFilterPass, setOnCellEditingComplete, getColDef, } = useContext(GridContext);
2327
+ const Grid = ({ "data-testid": dataTestId, rowSelection = "multiple", suppressColumnVirtualization = true, theme = "ag-theme-step-default", sizeColumns = "auto", selectColumnPinned = null, contextMenuSelectRow = false, singleClickEdit = false, rowData, rowHeight = theme === "ag-theme-step-default" ? 40 : theme === "ag-theme-step-compact" ? 36 : undefined, ...params }) => {
2328
+ const { gridReady, gridRenderState, setApis, ensureRowVisible, getFirstRowId, selectRowsById, focusByRowById, ensureSelectedRowIsVisible, autoSizeColumns, sizeColumnsToFit, externallySelectedItemsAreInSync, setExternallySelectedItemsAreInSync, isExternalFilterPresent, doesExternalFilterPass, setOnCellEditingComplete, getColDef, } = useContext(GridContext);
2328
2329
  const { checkUpdating, updatedDep, updatingCols } = useContext(GridUpdatingContext);
2329
2330
  const { prePopupOps } = useContext(GridContext);
2330
2331
  const gridDivRef = useRef(null);
@@ -2339,42 +2340,45 @@ const Grid = ({ "data-testid": dataTestId, rowSelection = "multiple", suppressCo
2339
2340
  */
2340
2341
  const hasSetContentSize = useRef(false);
2341
2342
  const hasSetContentSizeEmpty = useRef(false);
2342
- const needsAutoSize = useRef(false);
2343
+ const needsAutoSize = useRef(true);
2343
2344
  const setInitialContentSize = useCallback(() => {
2344
- if (!gridDivRef.current?.clientWidth) {
2345
+ if (!gridDivRef.current?.clientWidth || rowData == null) {
2345
2346
  // Don't resize grids if they are offscreen as it doesn't work.
2346
2347
  needsAutoSize.current = true;
2347
2348
  return;
2348
2349
  }
2349
- const headerCellCount = gridDivRef.current?.getElementsByClassName("ag-header-cell-label")?.length;
2350
- if (headerCellCount < 2) {
2351
- // Don't resize grids until all the columns are visible
2352
- // as `autoSizeColumns` will fail silently in this case
2350
+ const gridRendered = gridRenderState();
2351
+ if (gridRendered === null) {
2352
+ // Don't resize until grid has rendered, or it has 0 rows.
2353
2353
  needsAutoSize.current = true;
2354
2354
  return;
2355
2355
  }
2356
- const skipHeader = sizeColumns === "auto-skip-headers" && !isEmpty(params.rowData);
2356
+ const skipHeader = sizeColumns === "auto-skip-headers" && gridRendered === "rows-visible";
2357
2357
  if (sizeColumns === "auto" || skipHeader) {
2358
2358
  const result = autoSizeColumns({ skipHeader, userSizedColIds: userSizedColIds.current, includeFlex: true });
2359
- if (isEmpty(params.rowData)) {
2359
+ if (gridRendered === "empty") {
2360
2360
  if (!hasSetContentSizeEmpty.current && result && !hasSetContentSize.current) {
2361
2361
  hasSetContentSizeEmpty.current = true;
2362
2362
  params.onContentSize && params.onContentSize(result);
2363
2363
  }
2364
2364
  }
2365
- else {
2365
+ else if (gridRendered === "rows-visible") {
2366
2366
  if (result && !hasSetContentSize.current) {
2367
2367
  hasSetContentSize.current = true;
2368
2368
  params.onContentSize && params.onContentSize(result);
2369
2369
  }
2370
2370
  }
2371
+ else {
2372
+ // It should be impossible to get here
2373
+ console.error("Unknown value returned from hasGridRendered");
2374
+ }
2371
2375
  }
2372
2376
  if (sizeColumns !== "none") {
2373
2377
  sizeColumnsToFit();
2374
2378
  }
2375
2379
  setAutoSized(true);
2376
2380
  needsAutoSize.current = false;
2377
- }, [autoSizeColumns, params, sizeColumns, sizeColumnsToFit]);
2381
+ }, [autoSizeColumns, gridRenderState, params, rowData, sizeColumns, sizeColumnsToFit]);
2378
2382
  const lastOwnerDocumentRef = useRef();
2379
2383
  /**
2380
2384
  * Auto-size windows that had deferred auto-size
@@ -2394,24 +2398,17 @@ const Grid = ({ "data-testid": dataTestId, rowSelection = "multiple", suppressCo
2394
2398
  setInitialContentSize();
2395
2399
  }
2396
2400
  },
2397
- timeoutMs: 1000,
2401
+ timeoutMs: 200,
2398
2402
  });
2399
- const previousGridReady = useRef(gridReady);
2400
- useEffect(() => {
2401
- if (!previousGridReady.current && gridReady) {
2402
- previousGridReady.current = true;
2403
- setInitialContentSize();
2404
- }
2405
- }, [gridReady, setInitialContentSize]);
2406
2403
  /**
2407
2404
  * On data load select the first row of the grid if required.
2408
2405
  */
2409
2406
  const hasSelectedFirstItem = useRef(false);
2410
2407
  useEffect(() => {
2411
- if (!gridReady || hasSelectedFirstItem.current || !params.rowData || !externallySelectedItemsAreInSync)
2408
+ if (!gridReady || hasSelectedFirstItem.current || !rowData || !externallySelectedItemsAreInSync)
2412
2409
  return;
2413
2410
  hasSelectedFirstItem.current = true;
2414
- if (isNotEmpty(params.rowData) && isEmpty(params.externalSelectedItems)) {
2411
+ if (isNotEmpty(rowData) && isEmpty(params.externalSelectedItems)) {
2415
2412
  const firstRowId = getFirstRowId();
2416
2413
  if (params.autoSelectFirstRow) {
2417
2414
  selectRowsById([firstRowId]);
@@ -2426,7 +2423,7 @@ const Grid = ({ "data-testid": dataTestId, rowSelection = "multiple", suppressCo
2426
2423
  gridReady,
2427
2424
  params.externalSelectedItems,
2428
2425
  params.autoSelectFirstRow,
2429
- params.rowData,
2426
+ rowData,
2430
2427
  selectRowsById,
2431
2428
  getFirstRowId,
2432
2429
  ]);
@@ -2567,7 +2564,7 @@ const Grid = ({ "data-testid": dataTestId, rowSelection = "multiple", suppressCo
2567
2564
  */
2568
2565
  const previousRowDataLength = useRef(0);
2569
2566
  const onRowDataChanged = useCallback(() => {
2570
- const length = params.rowData?.length ?? 0;
2567
+ const length = rowData?.length ?? 0;
2571
2568
  if (previousRowDataLength.current !== length) {
2572
2569
  setInitialContentSize();
2573
2570
  previousRowDataLength.current = length;
@@ -2581,7 +2578,7 @@ const Grid = ({ "data-testid": dataTestId, rowSelection = "multiple", suppressCo
2581
2578
  const skipHeader = sizeColumns === "auto-skip-headers";
2582
2579
  autoSizeColumns({ skipHeader, userSizedColIds: userSizedColIds.current, colIds: colIdsEdited.current });
2583
2580
  colIdsEdited.current.clear();
2584
- }, [autoSizeColumns, params.rowData?.length, setInitialContentSize, sizeColumns, updatedDep, updatingCols]);
2581
+ }, [autoSizeColumns, rowData?.length, setInitialContentSize, sizeColumns, updatedDep, updatingCols]);
2585
2582
  /**
2586
2583
  * Show/hide no rows overlay when model changes.
2587
2584
  */
@@ -2750,7 +2747,6 @@ const Grid = ({ "data-testid": dataTestId, rowSelection = "multiple", suppressCo
2750
2747
  const position = clientSideRowModel.getHighlightPosition(event.y, event.overNode);
2751
2748
  //we don't want to show the row highlight if it wouldn't result in the row moving
2752
2749
  const targetIndex = event.overIndex + position - (event.node.rowIndex < event.overIndex ? 1 : 0);
2753
- //console.log(targetIndex)
2754
2750
  if (event.node.rowIndex != targetIndex) {
2755
2751
  clientSideRowModel.highlightRowAtPixel(event.node, event.y);
2756
2752
  }
@@ -2779,9 +2775,9 @@ const Grid = ({ "data-testid": dataTestId, rowSelection = "multiple", suppressCo
2779
2775
  }, [params]);
2780
2776
  // This is setting a ref in the GridContext so won't be triggering an update loop
2781
2777
  setOnCellEditingComplete(params.onCellEditingComplete);
2782
- return (jsxs("div", { "data-testid": dataTestId, className: clsx("Grid-container", theme, "theme-specific", staleGrid && "Grid-sortIsStale", gridReady && params.rowData && autoSized && "Grid-ready"), children: [gridContextMenu.component, jsx("div", { style: { flex: 1 }, ref: gridDivRef, children: jsx(AgGridReact, { rowHeight: rowHeight, animateRows: params.animateRows, rowClassRules: params.rowClassRules, getRowId: (params) => `${params.data.id}`, suppressRowClickSelection: true, rowSelection: rowSelection, suppressBrowserResizeObserver: true, onGridSizeChanged: onGridSizeChanged, suppressColumnVirtualisation: suppressColumnVirtualization, suppressClickEdit: true, onColumnVisible: () => {
2778
+ return (jsxs("div", { "data-testid": dataTestId, className: clsx("Grid-container", theme, "theme-specific", staleGrid && "Grid-sortIsStale", gridReady && rowData && autoSized && "Grid-ready"), children: [gridContextMenu.component, jsx("div", { style: { flex: 1 }, ref: gridDivRef, children: jsx(AgGridReact, { rowHeight: rowHeight, animateRows: params.animateRows, rowClassRules: params.rowClassRules, getRowId: (params) => `${params.data.id}`, suppressRowClickSelection: true, rowSelection: rowSelection, suppressBrowserResizeObserver: true, onGridSizeChanged: onGridSizeChanged, suppressColumnVirtualisation: suppressColumnVirtualization, suppressClickEdit: true, onColumnVisible: () => {
2783
2779
  setInitialContentSize();
2784
- }, onRowDataUpdated: onRowDataChanged, onCellKeyDown: onCellKeyPress, onCellClicked: onCellClicked, onCellDoubleClicked: onCellDoubleClick, onCellEditingStarted: refreshSelectedRows, domLayout: params.domLayout, onColumnResized: onColumnResized, defaultColDef: { minWidth: 48, ...omit(params.defaultColDef, ["editable"]) }, columnDefs: columnDefsAdjusted, rowData: params.rowData, noRowsOverlayComponent: (event) => {
2780
+ }, onRowDataUpdated: onRowDataChanged, onCellKeyDown: onCellKeyPress, onCellClicked: onCellClicked, onCellDoubleClicked: onCellDoubleClick, onCellEditingStarted: refreshSelectedRows, domLayout: params.domLayout, onColumnResized: onColumnResized, defaultColDef: { minWidth: 48, ...omit(params.defaultColDef, ["editable"]) }, columnDefs: columnDefsAdjusted, rowData: rowData, noRowsOverlayComponent: (event) => {
2785
2781
  let rowCount = 0;
2786
2782
  event.api.forEachNode(() => rowCount++);
2787
2783
  return (jsx(GridNoRowsOverlay, { rowCount: rowCount, filteredRowCount: event.api.getDisplayedRowCount(), noRowsOverlayText: params.noRowsOverlayText }));
@@ -4593,6 +4589,20 @@ const GridContextProvider = (props) => {
4593
4589
  gridApi?.setQuickFilter(quickFilter);
4594
4590
  setGridReady(!!gridApi);
4595
4591
  }, [quickFilter]);
4592
+ /**
4593
+ * Used to check if it's OK to autosize.
4594
+ */
4595
+ const gridRenderState = useCallback(() => {
4596
+ // Even though getModel can't be null, sometimes it is
4597
+ if (!gridApi || !gridApi.getModel())
4598
+ return null;
4599
+ if (!gridApi.getModel().isRowsToRender())
4600
+ return "empty";
4601
+ if (!isEmpty(gridApi.getRenderedNodes()))
4602
+ return "rows-visible";
4603
+ // If there are rows to render, but there are no rendered nodes then we should wait
4604
+ return null;
4605
+ }, [gridApi]);
4596
4606
  /**
4597
4607
  * Expose scrollRowIntoView for playwright tests.
4598
4608
  */
@@ -5011,6 +5021,7 @@ const GridContextProvider = (props) => {
5011
5021
  });
5012
5022
  }, [columnApi, gridApi]);
5013
5023
  return (jsx(GridContext.Provider, { value: {
5024
+ gridRenderState,
5014
5025
  getColDef,
5015
5026
  getColumns,
5016
5027
  getColumnIds,