@linzjs/step-ag-grid 2.4.3 → 2.4.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.
package/dist/index.js CHANGED
@@ -1868,7 +1868,10 @@ var GridContext = react.createContext({
1868
1868
  console.error("no context provider for modifyUpdating");
1869
1869
  return [2 /*return*/, false];
1870
1870
  });
1871
- }); }
1871
+ }); },
1872
+ redrawRows: function () {
1873
+ console.error("no context provider for redrawRows");
1874
+ }
1872
1875
  });
1873
1876
 
1874
1877
  /**
@@ -2122,6 +2125,11 @@ var GridContextProvider = function (props) {
2122
2125
  }
2123
2126
  });
2124
2127
  }); };
2128
+ var redrawRows = function (rowNodes) {
2129
+ gridApiOp(function (gridApi) {
2130
+ gridApi.redrawRows(rowNodes ? { rowNodes: rowNodes } : undefined);
2131
+ });
2132
+ };
2125
2133
  return (jsxRuntime.jsx(GridContext.Provider, __assign({ value: {
2126
2134
  gridReady: gridReady,
2127
2135
  setGridApi: setGridApi,
@@ -2139,7 +2147,8 @@ var GridContextProvider = function (props) {
2139
2147
  ensureSelectedRowIsVisible: ensureSelectedRowIsVisible,
2140
2148
  sizeColumnsToFit: sizeColumnsToFit,
2141
2149
  stopEditing: stopEditing,
2142
- updatingCells: updatingCells
2150
+ updatingCells: updatingCells,
2151
+ redrawRows: redrawRows
2143
2152
  } }, { children: props.children })));
2144
2153
  };
2145
2154
 
@@ -2188,6 +2197,7 @@ function clsx () {
2188
2197
  */
2189
2198
  var usePostSortRowsHook = function (_a) {
2190
2199
  var setStaleGrid = _a.setStaleGrid;
2200
+ var redrawRows = react.useContext(GridContext).redrawRows;
2191
2201
  // On first run we need to init the first backed up sort order
2192
2202
  var initialised = react.useRef(false);
2193
2203
  // Used to detect sort order has changed since last run
@@ -2203,14 +2213,20 @@ var usePostSortRowsHook = function (_a) {
2203
2213
  var previousQuickFilter = react.useRef("");
2204
2214
  return react.useCallback(function (_a) {
2205
2215
  var api = _a.api, columnApi = _a.columnApi, nodes = _a.nodes;
2216
+ // Grid is destroyed
2217
+ if (!api || !columnApi)
2218
+ return;
2206
2219
  var previousRowSortIndex = previousRowSortIndexRef.current;
2220
+ var hashNode = function (node) {
2221
+ return node ? JSON.stringify(node.data) : "";
2222
+ };
2207
2223
  var copyCurrentSortSettings = function () {
2208
2224
  return columnApi.getColumnState().map(function (row) { return ({ colId: row.colId, sort: row.sort, sortIndex: row.sortIndex }); });
2209
2225
  };
2210
2226
  var backupSortOrder = function () {
2211
2227
  for (var x in previousRowSortIndex)
2212
2228
  delete previousRowSortIndex[x];
2213
- nodes.forEach(function (row, index) { return (previousRowSortIndex[row.data.id] = index); });
2229
+ nodes.forEach(function (node, index) { return (previousRowSortIndex["".concat(node.data.id)] = { index: index, hash: hashNode(node) }); });
2214
2230
  };
2215
2231
  // Check if column is the first sorted column. Note: column is preconfigured to sort its sortIndex is null not 1
2216
2232
  var isFirstSortColumn = function (row) { var _a; return row.sort && [0, null].includes((_a = row.sortIndex) !== null && _a !== void 0 ? _a : null); };
@@ -2218,19 +2234,11 @@ var usePostSortRowsHook = function (_a) {
2218
2234
  return col1 && col2 && col1.colId === col2.colId && col1.sort != col2.sort;
2219
2235
  };
2220
2236
  var restorePreviousSortColumnState = function () { return columnApi.applyColumnState({ state: previousColSort.current }); };
2221
- var hasNewRows = function () { return nodes.some(function (row) { return previousRowSortIndex[row.data.id] == null; }); };
2222
- var sortIsStale = function () {
2223
- // If there are new rows we want to them to be at the bottom of the grid, so we treat it as sort not stale
2224
- if (hasNewRows())
2225
- return false;
2226
- // Otherwise check if the stored sort index matches the new sort index
2227
- return nodes.some(function (node, index) { return previousRowSortIndex[node.data.id] != index; });
2228
- };
2229
2237
  var sortNodesByPreviousSort = function () {
2230
- return nodes.sort(function (a, b) {
2231
- var _a, _b;
2232
- return ((_a = previousRowSortIndex[a.data.id]) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER) -
2233
- ((_b = previousRowSortIndex[b.data.id]) !== null && _b !== void 0 ? _b : Number.MAX_SAFE_INTEGER);
2238
+ nodes.sort(function (a, b) {
2239
+ var _a, _b, _c, _d;
2240
+ return ((_b = (_a = previousRowSortIndex["".concat(a.data.id)]) === null || _a === void 0 ? void 0 : _a.index) !== null && _b !== void 0 ? _b : Number.MAX_SAFE_INTEGER) -
2241
+ ((_d = (_c = previousRowSortIndex["".concat(b.data.id)]) === null || _c === void 0 ? void 0 : _c.index) !== null && _d !== void 0 ? _d : Number.MAX_SAFE_INTEGER);
2234
2242
  });
2235
2243
  };
2236
2244
  // On first load copy the current sort
@@ -2245,6 +2253,9 @@ var usePostSortRowsHook = function (_a) {
2245
2253
  previousQuickFilter.current = quickFilter;
2246
2254
  sortOrderChanged = true;
2247
2255
  }
2256
+ if (lodashEs.isEmpty(previousRowSortIndex)) {
2257
+ backupSortOrder();
2258
+ }
2248
2259
  if (sortOrderChanged) {
2249
2260
  var thisFirstCol = copyCurrentSortSettings().find(isFirstSortColumn);
2250
2261
  var previousFirstCol = previousColSort.current.find(isFirstSortColumn);
@@ -2275,22 +2286,72 @@ var usePostSortRowsHook = function (_a) {
2275
2286
  }
2276
2287
  }
2277
2288
  else {
2278
- if (sortIsStale()) {
2289
+ var firstChangedNodeIndex = -1;
2290
+ var lastNewNode = undefined;
2291
+ var changedRowCount = 0;
2292
+ var newRowCount = 0;
2293
+ var index = 0;
2294
+ for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
2295
+ var node = nodes_1[_i];
2296
+ var psr = previousRowSortIndex["".concat(node.data.id)];
2297
+ if (psr) {
2298
+ if (psr.hash != hashNode(node)) {
2299
+ if (firstChangedNodeIndex === -1)
2300
+ firstChangedNodeIndex = index;
2301
+ changedRowCount++;
2302
+ }
2303
+ }
2304
+ else {
2305
+ lastNewNode = node;
2306
+ newRowCount++;
2307
+ }
2308
+ index++;
2309
+ }
2310
+ var wasStale = false;
2311
+ if (changedRowCount === 0 && newRowCount === 1) {
2312
+ // insert new row at end
2313
+ var newIndex = index - 1;
2314
+ previousRowSortIndex["".concat(lastNewNode === null || lastNewNode === void 0 ? void 0 : lastNewNode.data.id)] = { index: newIndex, hash: hashNode(lastNewNode) };
2315
+ wasStale = true;
2316
+ }
2317
+ else if (changedRowCount === 2 && newRowCount === 0) {
2318
+ // This must be a swap rows
2319
+ backupSortOrder();
2320
+ wasStale = false;
2321
+ }
2322
+ else if (changedRowCount > 1 && newRowCount === 1) {
2323
+ // This must be a insert so, insert new row near the row that changed
2324
+ previousRowSortIndex["".concat(lastNewNode === null || lastNewNode === void 0 ? void 0 : lastNewNode.data.id)] = {
2325
+ index: firstChangedNodeIndex + 0.5,
2326
+ hash: hashNode(lastNewNode)
2327
+ };
2328
+ wasStale = true;
2329
+ // For some reason AgGrid mis-positions the inserted row.
2330
+ lastNewNode && redrawRows();
2331
+ }
2332
+ else if (changedRowCount == 1 && newRowCount === 0) {
2333
+ // User edited one row so, do nothing, retain sort
2334
+ wasStale = true;
2335
+ }
2336
+ else if (changedRowCount !== 0 || newRowCount != 0) {
2337
+ // too many rows changed, resort
2338
+ backupSortOrder();
2339
+ }
2340
+ if (wasStale) {
2341
+ // Check if the sort order the aggrid passed matches our stale sort order
2342
+ var stillStale = Object.keys(previousRowSortIndex).length != nodes.length ||
2343
+ nodes.some(function (node, index) { var _a; return ((_a = previousRowSortIndex["".concat(node.data.id)]) === null || _a === void 0 ? void 0 : _a.index) !== index; });
2279
2344
  // If we haven't already processed a stale sort then...
2280
- if (!sortWasStale.current) {
2345
+ if (stillStale && !sortWasStale.current) {
2281
2346
  // backup sort state, so we can restore it when sort is clicked on a stale column
2282
2347
  previousColSort.current = copyCurrentSortSettings();
2283
- backupSortOrder();
2284
2348
  sortWasStale.current = true;
2285
2349
  setStaleGrid(true);
2286
2350
  }
2287
- sortNodesByPreviousSort();
2288
2351
  }
2289
- // secondary sort backup as there may be new nodes that didn't have their sort registered
2290
- // which would cause two new rows to sort out of sequence
2291
- backupSortOrder();
2352
+ sortNodesByPreviousSort();
2292
2353
  }
2293
- }, [setStaleGrid]);
2354
+ }, [redrawRows, setStaleGrid]);
2294
2355
  };
2295
2356
 
2296
2357
  /**
@@ -3797,38 +3858,44 @@ var useStateDeferred = function (initialValue) {
3797
3858
  // Kept this less than one second, so I don't have issues with waitFor as it defaults to 1s
3798
3859
  var minimumInProgressTimeMs = 950;
3799
3860
  var ActionButton = function (_a) {
3800
- var _b;
3801
- var _c;
3802
- var icon = _a.icon, name = _a.name, inProgressName = _a.inProgressName, dataTestId = _a.dataTestId, className = _a.className, title = _a.title, onAction = _a.onAction, externalSetInProgress = _a.externalSetInProgress, _d = _a.size, size = _d === void 0 ? "sm" : _d;
3803
- var _e = react.useState(false), inProgress = _e[0], setInProgress = _e[1];
3861
+ var _b, _c, _d;
3862
+ var icon = _a.icon, name = _a.name, inProgressName = _a.inProgressName, dataTestId = _a.dataTestId, className = _a.className, title = _a.title, onAction = _a.onAction, externalSetInProgress = _a.externalSetInProgress, _e = _a.size, size = _e === void 0 ? "sm" : _e, _f = _a.level, level = _f === void 0 ? "tertiary" : _f, ariaLabel = _a["aria-label"];
3863
+ var _g = react.useState(false), inProgress = _g[0], setInProgress = _g[1];
3804
3864
  var lastInProgress = usePrevious(inProgress !== null && inProgress !== void 0 ? inProgress : false);
3805
- var _f = useStateDeferred(inProgress), localInProgress = _f[0], setLocalInProgress = _f[1], setLocalInProgressDeferred = _f[2];
3865
+ var _h = useStateDeferred(inProgress), localInProgress = _h[0], setLocalInProgress = _h[1], setLocalInProgressDeferred = _h[2];
3806
3866
  react.useEffect(function () {
3807
3867
  if (inProgress == lastInProgress)
3808
3868
  return;
3809
3869
  inProgress ? setLocalInProgress(true) : setLocalInProgressDeferred(false, minimumInProgressTimeMs);
3810
3870
  }, [inProgress, lastInProgress, setLocalInProgress, setLocalInProgressDeferred]);
3811
- return (jsxRuntime.jsxs(lui.LuiButton, __assign({ "data-testid": dataTestId, type: "button", level: "tertiary", title: title !== null && title !== void 0 ? title : name, "aria-label": name, className: clsx("lui-button-icon", "ActionButton", className, localInProgress && "ActionButton-inProgress"), size: "lg", onClick: function () { return __awaiter(void 0, void 0, void 0, function () {
3871
+ return (jsxRuntime.jsxs(lui.LuiButton, __assign({ "data-testid": dataTestId, type: "button", level: level, title: (_b = title !== null && title !== void 0 ? title : ariaLabel) !== null && _b !== void 0 ? _b : name, "aria-label": ariaLabel !== null && ariaLabel !== void 0 ? ariaLabel : name, className: clsx("lui-button-icon", "ActionButton", className, localInProgress && "ActionButton-inProgress"), size: "lg", style: name == null ? { padding: "8px 5px" } : {}, onClick: function () { return __awaiter(void 0, void 0, void 0, function () {
3872
+ var promise, isPromise;
3812
3873
  return __generator(this, function (_a) {
3813
3874
  switch (_a.label) {
3814
3875
  case 0:
3876
+ promise = onAction();
3877
+ isPromise = typeof promise !== "undefined";
3878
+ if (!isPromise) return [3 /*break*/, 3];
3815
3879
  setInProgress(true);
3816
3880
  externalSetInProgress && setInProgress(true);
3817
- return [4 /*yield*/, onAction()];
3881
+ if (!isPromise) return [3 /*break*/, 2];
3882
+ return [4 /*yield*/, promise];
3818
3883
  case 1:
3819
3884
  _a.sent();
3885
+ _a.label = 2;
3886
+ case 2:
3820
3887
  externalSetInProgress && setInProgress(false);
3821
3888
  setInProgress(false);
3822
- return [2 /*return*/];
3889
+ _a.label = 3;
3890
+ case 3: return [2 /*return*/];
3823
3891
  }
3824
3892
  });
3825
- }); }, disabled: localInProgress }, { children: [localInProgress ? (jsxRuntime.jsx(lui.LuiMiniSpinner, { size: 16, divProps: (_b = {
3826
- "data-testid": "loading-spinner",
3827
- style: { padding: 0, margin: 0, paddingRight: 8 },
3828
- role: "status"
3829
- },
3830
- _b["aria-label"] = "Loading",
3831
- _b) })) : (jsxRuntime.jsx(lui.LuiIcon, { name: icon, alt: name, size: size })), jsxRuntime.jsxs("span", __assign({ className: "ActionButton-minimalArea" }, { children: [jsxRuntime.jsx("span", __assign({ className: "ActionButton-minimalAreaDisplay" }, { children: (_c = (localInProgress ? inProgressName : name)) !== null && _c !== void 0 ? _c : name })), jsxRuntime.jsx("span", __assign({ className: "ActionButton-minimalAreaExpand" }, { children: name }))] }))] })));
3893
+ }); }, disabled: localInProgress }, { children: [localInProgress ? (jsxRuntime.jsx(lui.LuiMiniSpinner, { size: 16, divProps: {
3894
+ "data-testid": "loading-spinner",
3895
+ style: { margin: 0, paddingRight: 5, paddingLeft: 3, paddingBottom: 0, paddingTop: 0 },
3896
+ role: "status",
3897
+ "aria-label": "Loading"
3898
+ } })) : (jsxRuntime.jsx(lui.LuiIcon, { name: icon, alt: (_c = ariaLabel !== null && ariaLabel !== void 0 ? ariaLabel : name) !== null && _c !== void 0 ? _c : "", size: size })), jsxRuntime.jsxs("span", __assign({ className: "ActionButton-minimalArea" }, { children: [jsxRuntime.jsx("span", __assign({ className: "ActionButton-minimalAreaDisplay" }, { children: (_d = (localInProgress ? inProgressName : name)) !== null && _d !== void 0 ? _d : name })), jsxRuntime.jsx("span", __assign({ className: "ActionButton-minimalAreaExpand" }, { children: name }))] }))] })));
3832
3899
  };
3833
3900
 
3834
3901
  exports.ActionButton = ActionButton;