@homebound/beam 2.116.1 → 2.117.2

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.
@@ -188,8 +188,10 @@ export declare type GridTableApi<R extends Kinded> = {
188
188
  scrollToIndex: (index: number) => void;
189
189
  /** Returns the ids of currently-selected rows. */
190
190
  getSelectedRowIds(): string[];
191
+ getSelectedRowIds<K extends R["kind"]>(kind: K): string[];
191
192
  /** Returns the currently-selected rows. */
192
193
  getSelectedRows(): GridDataRow<R>[];
194
+ getSelectedRows<K extends R["kind"]>(kind: K): GridDataRow<DiscriminateUnion<R, "kind", K>>[];
193
195
  /** Sets the internal state of 'activeRowId' */
194
196
  setActiveRowId: (id: string | undefined) => void;
195
197
  };
@@ -95,12 +95,20 @@ function GridTable(props) {
95
95
  const tableRef = (0, react_1.useRef)(null);
96
96
  const api = (0, react_1.useRef)({
97
97
  scrollToIndex: (index) => virtuosoRef.current && virtuosoRef.current.scrollToIndex(index),
98
- getSelectedRowIds: () => rowState.selectedIds,
99
- getSelectedRows() {
98
+ getSelectedRowIds(kind) {
99
+ if (kind === undefined) {
100
+ return rowState.selectedIds;
101
+ }
102
+ else {
103
+ return this.getSelectedRows(kind).map((row) => row.id);
104
+ }
105
+ },
106
+ // The any is not great, but getting the overload to handle the optional kind is annoying
107
+ getSelectedRows(kind) {
100
108
  const ids = rowState.selectedIds;
101
109
  const selected = [];
102
110
  (0, visitor_1.visit)(rows, (row) => {
103
- if (ids.includes(row.id)) {
111
+ if (ids.includes(row.id) && (!kind || row.kind === kind)) {
104
112
  selected.push(row);
105
113
  }
106
114
  });
@@ -113,7 +121,7 @@ function GridTable(props) {
113
121
  }
114
122
  (0, react_1.useEffect)(() => {
115
123
  rowState.activeRowId = activeRowId;
116
- }, [activeRowId]);
124
+ }, [rowState, activeRowId]);
117
125
  // We track render count at the table level, which seems odd (we should be able to track this
118
126
  // internally within each GridRow using a useRef), but we have suspicions that react-virtuoso
119
127
  // (or us) is resetting component state more than necessary, so we track render counts from
@@ -209,12 +217,14 @@ function GridTable(props) {
209
217
  observeRows,
210
218
  columnSizes,
211
219
  collapsedIds,
220
+ getCount,
212
221
  ]);
213
222
  let tooManyClientSideRows = false;
214
223
  if (filterMaxRows && filteredRows.length > filterMaxRows) {
215
224
  tooManyClientSideRows = true;
216
225
  filteredRows = filteredRows.slice(0, filterMaxRows);
217
226
  }
227
+ rowState.visibleRows.replace(filteredRows.map(([row]) => { var _a; return (_a = row === null || row === void 0 ? void 0 : row.id) !== null && _a !== void 0 ? _a : ""; }));
218
228
  // Push back to the caller a way to ask us where a row is.
219
229
  const { rowLookup } = props;
220
230
  if (rowLookup) {
@@ -1,3 +1,4 @@
1
+ import { ObservableSet } from "mobx";
1
2
  import React, { MutableRefObject } from "react";
2
3
  import { GridDataRow } from "./GridTable";
3
4
  export declare type SelectedState = "checked" | "unchecked" | "partial";
@@ -21,6 +22,7 @@ export declare class RowState {
21
22
  private persistCollapse;
22
23
  private readonly collapsedRows;
23
24
  private readonly selectedRows;
25
+ visibleRows: ObservableSet<string>;
24
26
  activeRowId: string | undefined;
25
27
  /**
26
28
  * Creates the `RowState` for a given `GridTable`.
@@ -30,6 +30,8 @@ class RowState {
30
30
  this.rows = rows;
31
31
  this.persistCollapse = persistCollapse;
32
32
  this.selectedRows = new mobx_1.ObservableMap();
33
+ // Set of just row ids. Keeps track of which rows are visible. Used to filter out non-visible rows from `selectedIds`
34
+ this.visibleRows = new mobx_1.ObservableSet();
33
35
  this.collapsedRows = new mobx_1.ObservableSet(persistCollapse ? readLocalCollapseState(persistCollapse) : []);
34
36
  this.activeRowId = activeRowId;
35
37
  // Make ourselves an observable so that mobx will do caching of .collapseIds so
@@ -38,7 +40,9 @@ class RowState {
38
40
  }
39
41
  get selectedIds() {
40
42
  // Return only ids that are fully checked, i.e. not partial
41
- const ids = [...this.selectedRows.entries()].filter(([, v]) => v === "checked").map(([k]) => k);
43
+ const ids = [...this.selectedRows.entries()]
44
+ .filter(([id, v]) => this.visibleRows.has(id) && v === "checked")
45
+ .map(([k]) => k);
42
46
  // Hide our header marker
43
47
  const headerIndex = ids.indexOf("header");
44
48
  if (headerIndex > -1) {
@@ -6,7 +6,7 @@ const react_1 = require("react");
6
6
  /** Evaluates a computed function `fn` to a regular value and triggers a re-render whenever it changes. */
7
7
  function useComputed(fn, deps) {
8
8
  // We always return the useRef value, and use this just to trigger re-renders
9
- const [, setValue] = (0, react_1.useState)(0);
9
+ const [, setTick] = (0, react_1.useState)(0);
10
10
  const ref = (0, react_1.useRef)({
11
11
  runner: undefined,
12
12
  value: undefined,
@@ -17,25 +17,25 @@ function useComputed(fn, deps) {
17
17
  // with `useEffect`, which would only get calc'd after the 1st render has
18
18
  // already been done.
19
19
  (0, react_1.useMemo)(() => {
20
- let tick = 0;
21
20
  const { current } = ref;
22
21
  // If deps has changed, unhook the previous observer
23
22
  if (current.runner) {
24
23
  current.runner();
25
24
  }
25
+ // If deps has changed, we're already re-running, so don't trigger a 2nd one
26
+ current.hasRan = false;
26
27
  current.runner = (0, mobx_1.autorun)(() => {
27
28
  // Always eval fn() (even on 1st render) to register our observable.
28
29
  const newValue = fn();
29
- const oldValue = current.value;
30
+ const { value: oldValue, hasRan: oldHasRun } = current;
30
31
  current.value = newValue;
31
32
  current.hasRan = true;
32
33
  // Only trigger a re-render if this is not the 1st autorun. Note
33
34
  // that if deps has changed, we're inherently in a re-render so also
34
35
  // don't need to trigger an additional re-render.
35
- if (tick > 0 && newValue !== oldValue) {
36
- setValue(tick);
36
+ if (oldHasRun && newValue !== oldValue) {
37
+ setTick((tick) => tick + 1);
37
38
  }
38
- tick++;
39
39
  });
40
40
  // eslint-disable-next-line react-hooks/exhaustive-deps
41
41
  }, deps);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/beam",
3
- "version": "2.116.1",
3
+ "version": "2.117.2",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",