@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.
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { GridApi } from "ag-grid-community";
2
+ import { GridApi, RowNode } from "ag-grid-community";
3
3
  import { GridBaseRow } from "../components/Grid";
4
4
  export interface GridContextType {
5
5
  gridReady: () => boolean;
@@ -22,5 +22,6 @@ export interface GridContextType {
22
22
  selectedRows: GridBaseRow[];
23
23
  field?: string;
24
24
  }, fnUpdate: (selectedRows: any[]) => Promise<boolean>, setSaving?: (saving: boolean) => void) => Promise<boolean>;
25
+ redrawRows: (rowNodes?: RowNode[]) => void;
25
26
  }
26
27
  export declare const GridContext: import("react").Context<GridContextType>;
@@ -1,15 +1,18 @@
1
1
  /// <reference types="react" />
2
2
  import "./ActionButton.scss";
3
3
  import { IconName } from "@linzjs/lui/dist/components/LuiIcon/LuiIcon";
4
+ import { LuiButtonProps } from "@linzjs/lui/dist/components/LuiButton/LuiButton";
4
5
  export interface ActionButtonProps {
5
6
  icon: IconName;
6
- name: string;
7
+ name?: string;
8
+ "aria-label"?: string;
7
9
  inProgressName?: string;
8
10
  title?: string;
9
11
  dataTestId?: string;
10
12
  size?: "xs" | "sm" | "md" | "lg" | "xl" | "ns";
11
13
  className?: string;
12
- onAction: () => Promise<void>;
14
+ onAction: () => Promise<void> | void;
13
15
  externalSetInProgress?: () => void;
16
+ level?: LuiButtonProps["level"];
14
17
  }
15
- export declare const ActionButton: ({ icon, name, inProgressName, dataTestId, className, title, onAction, externalSetInProgress, size, }: ActionButtonProps) => JSX.Element;
18
+ export declare const ActionButton: ({ icon, name, inProgressName, dataTestId, className, title, onAction, externalSetInProgress, size, level, "aria-label": ariaLabel, }: ActionButtonProps) => JSX.Element;
@@ -1866,7 +1866,10 @@ var GridContext = createContext({
1866
1866
  console.error("no context provider for modifyUpdating");
1867
1867
  return [2 /*return*/, false];
1868
1868
  });
1869
- }); }
1869
+ }); },
1870
+ redrawRows: function () {
1871
+ console.error("no context provider for redrawRows");
1872
+ }
1870
1873
  });
1871
1874
 
1872
1875
  /**
@@ -2120,6 +2123,11 @@ var GridContextProvider = function (props) {
2120
2123
  }
2121
2124
  });
2122
2125
  }); };
2126
+ var redrawRows = function (rowNodes) {
2127
+ gridApiOp(function (gridApi) {
2128
+ gridApi.redrawRows(rowNodes ? { rowNodes: rowNodes } : undefined);
2129
+ });
2130
+ };
2123
2131
  return (jsx(GridContext.Provider, __assign({ value: {
2124
2132
  gridReady: gridReady,
2125
2133
  setGridApi: setGridApi,
@@ -2137,7 +2145,8 @@ var GridContextProvider = function (props) {
2137
2145
  ensureSelectedRowIsVisible: ensureSelectedRowIsVisible,
2138
2146
  sizeColumnsToFit: sizeColumnsToFit,
2139
2147
  stopEditing: stopEditing,
2140
- updatingCells: updatingCells
2148
+ updatingCells: updatingCells,
2149
+ redrawRows: redrawRows
2141
2150
  } }, { children: props.children })));
2142
2151
  };
2143
2152
 
@@ -2186,6 +2195,7 @@ function clsx () {
2186
2195
  */
2187
2196
  var usePostSortRowsHook = function (_a) {
2188
2197
  var setStaleGrid = _a.setStaleGrid;
2198
+ var redrawRows = useContext(GridContext).redrawRows;
2189
2199
  // On first run we need to init the first backed up sort order
2190
2200
  var initialised = useRef(false);
2191
2201
  // Used to detect sort order has changed since last run
@@ -2201,14 +2211,20 @@ var usePostSortRowsHook = function (_a) {
2201
2211
  var previousQuickFilter = useRef("");
2202
2212
  return useCallback(function (_a) {
2203
2213
  var api = _a.api, columnApi = _a.columnApi, nodes = _a.nodes;
2214
+ // Grid is destroyed
2215
+ if (!api || !columnApi)
2216
+ return;
2204
2217
  var previousRowSortIndex = previousRowSortIndexRef.current;
2218
+ var hashNode = function (node) {
2219
+ return node ? JSON.stringify(node.data) : "";
2220
+ };
2205
2221
  var copyCurrentSortSettings = function () {
2206
2222
  return columnApi.getColumnState().map(function (row) { return ({ colId: row.colId, sort: row.sort, sortIndex: row.sortIndex }); });
2207
2223
  };
2208
2224
  var backupSortOrder = function () {
2209
2225
  for (var x in previousRowSortIndex)
2210
2226
  delete previousRowSortIndex[x];
2211
- nodes.forEach(function (row, index) { return (previousRowSortIndex[row.data.id] = index); });
2227
+ nodes.forEach(function (node, index) { return (previousRowSortIndex["".concat(node.data.id)] = { index: index, hash: hashNode(node) }); });
2212
2228
  };
2213
2229
  // Check if column is the first sorted column. Note: column is preconfigured to sort its sortIndex is null not 1
2214
2230
  var isFirstSortColumn = function (row) { var _a; return row.sort && [0, null].includes((_a = row.sortIndex) !== null && _a !== void 0 ? _a : null); };
@@ -2216,19 +2232,11 @@ var usePostSortRowsHook = function (_a) {
2216
2232
  return col1 && col2 && col1.colId === col2.colId && col1.sort != col2.sort;
2217
2233
  };
2218
2234
  var restorePreviousSortColumnState = function () { return columnApi.applyColumnState({ state: previousColSort.current }); };
2219
- var hasNewRows = function () { return nodes.some(function (row) { return previousRowSortIndex[row.data.id] == null; }); };
2220
- var sortIsStale = function () {
2221
- // 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
2222
- if (hasNewRows())
2223
- return false;
2224
- // Otherwise check if the stored sort index matches the new sort index
2225
- return nodes.some(function (node, index) { return previousRowSortIndex[node.data.id] != index; });
2226
- };
2227
2235
  var sortNodesByPreviousSort = function () {
2228
- return nodes.sort(function (a, b) {
2229
- var _a, _b;
2230
- return ((_a = previousRowSortIndex[a.data.id]) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER) -
2231
- ((_b = previousRowSortIndex[b.data.id]) !== null && _b !== void 0 ? _b : Number.MAX_SAFE_INTEGER);
2236
+ nodes.sort(function (a, b) {
2237
+ var _a, _b, _c, _d;
2238
+ 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) -
2239
+ ((_d = (_c = previousRowSortIndex["".concat(b.data.id)]) === null || _c === void 0 ? void 0 : _c.index) !== null && _d !== void 0 ? _d : Number.MAX_SAFE_INTEGER);
2232
2240
  });
2233
2241
  };
2234
2242
  // On first load copy the current sort
@@ -2243,6 +2251,9 @@ var usePostSortRowsHook = function (_a) {
2243
2251
  previousQuickFilter.current = quickFilter;
2244
2252
  sortOrderChanged = true;
2245
2253
  }
2254
+ if (isEmpty(previousRowSortIndex)) {
2255
+ backupSortOrder();
2256
+ }
2246
2257
  if (sortOrderChanged) {
2247
2258
  var thisFirstCol = copyCurrentSortSettings().find(isFirstSortColumn);
2248
2259
  var previousFirstCol = previousColSort.current.find(isFirstSortColumn);
@@ -2273,22 +2284,72 @@ var usePostSortRowsHook = function (_a) {
2273
2284
  }
2274
2285
  }
2275
2286
  else {
2276
- if (sortIsStale()) {
2287
+ var firstChangedNodeIndex = -1;
2288
+ var lastNewNode = undefined;
2289
+ var changedRowCount = 0;
2290
+ var newRowCount = 0;
2291
+ var index = 0;
2292
+ for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
2293
+ var node = nodes_1[_i];
2294
+ var psr = previousRowSortIndex["".concat(node.data.id)];
2295
+ if (psr) {
2296
+ if (psr.hash != hashNode(node)) {
2297
+ if (firstChangedNodeIndex === -1)
2298
+ firstChangedNodeIndex = index;
2299
+ changedRowCount++;
2300
+ }
2301
+ }
2302
+ else {
2303
+ lastNewNode = node;
2304
+ newRowCount++;
2305
+ }
2306
+ index++;
2307
+ }
2308
+ var wasStale = false;
2309
+ if (changedRowCount === 0 && newRowCount === 1) {
2310
+ // insert new row at end
2311
+ var newIndex = index - 1;
2312
+ previousRowSortIndex["".concat(lastNewNode === null || lastNewNode === void 0 ? void 0 : lastNewNode.data.id)] = { index: newIndex, hash: hashNode(lastNewNode) };
2313
+ wasStale = true;
2314
+ }
2315
+ else if (changedRowCount === 2 && newRowCount === 0) {
2316
+ // This must be a swap rows
2317
+ backupSortOrder();
2318
+ wasStale = false;
2319
+ }
2320
+ else if (changedRowCount > 1 && newRowCount === 1) {
2321
+ // This must be a insert so, insert new row near the row that changed
2322
+ previousRowSortIndex["".concat(lastNewNode === null || lastNewNode === void 0 ? void 0 : lastNewNode.data.id)] = {
2323
+ index: firstChangedNodeIndex + 0.5,
2324
+ hash: hashNode(lastNewNode)
2325
+ };
2326
+ wasStale = true;
2327
+ // For some reason AgGrid mis-positions the inserted row.
2328
+ lastNewNode && redrawRows();
2329
+ }
2330
+ else if (changedRowCount == 1 && newRowCount === 0) {
2331
+ // User edited one row so, do nothing, retain sort
2332
+ wasStale = true;
2333
+ }
2334
+ else if (changedRowCount !== 0 || newRowCount != 0) {
2335
+ // too many rows changed, resort
2336
+ backupSortOrder();
2337
+ }
2338
+ if (wasStale) {
2339
+ // Check if the sort order the aggrid passed matches our stale sort order
2340
+ var stillStale = Object.keys(previousRowSortIndex).length != nodes.length ||
2341
+ nodes.some(function (node, index) { var _a; return ((_a = previousRowSortIndex["".concat(node.data.id)]) === null || _a === void 0 ? void 0 : _a.index) !== index; });
2277
2342
  // If we haven't already processed a stale sort then...
2278
- if (!sortWasStale.current) {
2343
+ if (stillStale && !sortWasStale.current) {
2279
2344
  // backup sort state, so we can restore it when sort is clicked on a stale column
2280
2345
  previousColSort.current = copyCurrentSortSettings();
2281
- backupSortOrder();
2282
2346
  sortWasStale.current = true;
2283
2347
  setStaleGrid(true);
2284
2348
  }
2285
- sortNodesByPreviousSort();
2286
2349
  }
2287
- // secondary sort backup as there may be new nodes that didn't have their sort registered
2288
- // which would cause two new rows to sort out of sequence
2289
- backupSortOrder();
2350
+ sortNodesByPreviousSort();
2290
2351
  }
2291
- }, [setStaleGrid]);
2352
+ }, [redrawRows, setStaleGrid]);
2292
2353
  };
2293
2354
 
2294
2355
  /**
@@ -3795,38 +3856,44 @@ var useStateDeferred = function (initialValue) {
3795
3856
  // Kept this less than one second, so I don't have issues with waitFor as it defaults to 1s
3796
3857
  var minimumInProgressTimeMs = 950;
3797
3858
  var ActionButton = function (_a) {
3798
- var _b;
3799
- var _c;
3800
- 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;
3801
- var _e = useState(false), inProgress = _e[0], setInProgress = _e[1];
3859
+ var _b, _c, _d;
3860
+ 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"];
3861
+ var _g = useState(false), inProgress = _g[0], setInProgress = _g[1];
3802
3862
  var lastInProgress = usePrevious(inProgress !== null && inProgress !== void 0 ? inProgress : false);
3803
- var _f = useStateDeferred(inProgress), localInProgress = _f[0], setLocalInProgress = _f[1], setLocalInProgressDeferred = _f[2];
3863
+ var _h = useStateDeferred(inProgress), localInProgress = _h[0], setLocalInProgress = _h[1], setLocalInProgressDeferred = _h[2];
3804
3864
  useEffect(function () {
3805
3865
  if (inProgress == lastInProgress)
3806
3866
  return;
3807
3867
  inProgress ? setLocalInProgress(true) : setLocalInProgressDeferred(false, minimumInProgressTimeMs);
3808
3868
  }, [inProgress, lastInProgress, setLocalInProgress, setLocalInProgressDeferred]);
3809
- return (jsxs(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 () {
3869
+ return (jsxs(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 () {
3870
+ var promise, isPromise;
3810
3871
  return __generator(this, function (_a) {
3811
3872
  switch (_a.label) {
3812
3873
  case 0:
3874
+ promise = onAction();
3875
+ isPromise = typeof promise !== "undefined";
3876
+ if (!isPromise) return [3 /*break*/, 3];
3813
3877
  setInProgress(true);
3814
3878
  externalSetInProgress && setInProgress(true);
3815
- return [4 /*yield*/, onAction()];
3879
+ if (!isPromise) return [3 /*break*/, 2];
3880
+ return [4 /*yield*/, promise];
3816
3881
  case 1:
3817
3882
  _a.sent();
3883
+ _a.label = 2;
3884
+ case 2:
3818
3885
  externalSetInProgress && setInProgress(false);
3819
3886
  setInProgress(false);
3820
- return [2 /*return*/];
3887
+ _a.label = 3;
3888
+ case 3: return [2 /*return*/];
3821
3889
  }
3822
3890
  });
3823
- }); }, disabled: localInProgress }, { children: [localInProgress ? (jsx(LuiMiniSpinner, { size: 16, divProps: (_b = {
3824
- "data-testid": "loading-spinner",
3825
- style: { padding: 0, margin: 0, paddingRight: 8 },
3826
- role: "status"
3827
- },
3828
- _b["aria-label"] = "Loading",
3829
- _b) })) : (jsx(LuiIcon, { name: icon, alt: name, size: size })), jsxs("span", __assign({ className: "ActionButton-minimalArea" }, { children: [jsx("span", __assign({ className: "ActionButton-minimalAreaDisplay" }, { children: (_c = (localInProgress ? inProgressName : name)) !== null && _c !== void 0 ? _c : name })), jsx("span", __assign({ className: "ActionButton-minimalAreaExpand" }, { children: name }))] }))] })));
3891
+ }); }, disabled: localInProgress }, { children: [localInProgress ? (jsx(LuiMiniSpinner, { size: 16, divProps: {
3892
+ "data-testid": "loading-spinner",
3893
+ style: { margin: 0, paddingRight: 5, paddingLeft: 3, paddingBottom: 0, paddingTop: 0 },
3894
+ role: "status",
3895
+ "aria-label": "Loading"
3896
+ } })) : (jsx(LuiIcon, { name: icon, alt: (_c = ariaLabel !== null && ariaLabel !== void 0 ? ariaLabel : name) !== null && _c !== void 0 ? _c : "", size: size })), jsxs("span", __assign({ className: "ActionButton-minimalArea" }, { children: [jsx("span", __assign({ className: "ActionButton-minimalAreaDisplay" }, { children: (_d = (localInProgress ? inProgressName : name)) !== null && _d !== void 0 ? _d : name })), jsx("span", __assign({ className: "ActionButton-minimalAreaExpand" }, { children: name }))] }))] })));
3830
3897
  };
3831
3898
 
3832
3899
  export { ActionButton, ComponentLoadingWrapper, ControlledMenu, FocusableItem, GenericCellEditorComponent, GenericMultiEditCellClass, Grid, GridCell, GridCellRenderer, GridContext, GridContextProvider, GridFormDropDown, GridFormMultiSelect, GridFormPopoutMenu, GridFormSubComponentTextInput, GridHeaderSelect, GridIcon, GridLoadableCell, GridPopoutEditMultiSelect, GridPopoverEditBearing, GridPopoverEditBearingCorrection, GridPopoverEditDropDown, GridPopoverMenu, GridPopoverMessage, GridPopoverTextArea, GridPopoverTextInput, GridRenderPopoutMenuCell, GridRendererGenericCell, GridSubComponentTextArea, GridUpdatingContext, GridUpdatingContextProvider, Menu, MenuButton, MenuDivider, MenuGroup, MenuHeader, MenuHeaderItem, MenuHeaderString, MenuItem, MenuRadioGroup, MenuSeparator, MenuSeparatorString, PopoutMenuSeparator, SubMenu, TextAreaInput, TextInputFormatted, bearingCorrectionValueFormatter, bearingNumberParser, bearingStringValidator, bearingValueFormatter, convertDDToDMS, hasParentClass, isFloat, isNotEmpty, useGridPopoverHook, useMenuState, usePostSortRowsHook, wait };