@homebound/beam 2.98.2 → 2.101.0

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.
@@ -9,7 +9,7 @@ export interface IconProps extends AriaAttributes, DOMProps {
9
9
  /** The size of the icon in increments, i.e. 1 == 8px, default is 3 == 24px. */
10
10
  inc?: number;
11
11
  /** Styles overrides */
12
- xss?: Xss<Margin>;
12
+ xss?: Xss<Margin | "visibility">;
13
13
  }
14
14
  export declare const Icon: React.MemoExoticComponent<(props: IconProps) => import("@emotion/react/jsx-runtime").JSX.Element>;
15
15
  /**
@@ -1,4 +1,4 @@
1
- import { ReactNode } from "react";
1
+ import { MutableRefObject, ReactNode } from "react";
2
2
  import { Only, Xss } from "../../Css";
3
3
  import { Callback } from "../../types";
4
4
  export declare type ModalSize = "sm" | "md" | "lg" | "xl";
@@ -19,7 +19,12 @@ export interface ModalProps {
19
19
  forceScrolling?: boolean;
20
20
  /** Adds a callback that is called _after_ close has definitely happened. */
21
21
  onClose?: Callback;
22
+ /** Imperative API for interacting with the Modal */
23
+ api?: MutableRefObject<ModalApi | undefined>;
22
24
  }
25
+ export declare type ModalApi = {
26
+ setSize: (size: ModalProps["size"]) => void;
27
+ };
23
28
  /**
24
29
  * Internal component for displaying a Modal; see `useModal` for the public API.
25
30
  *
@@ -20,7 +20,7 @@ const utils_1 = require("../../utils");
20
20
  * Provides underlay, modal container, and header. Will disable scrolling of page under the modal.
21
21
  */
22
22
  function Modal(props) {
23
- const { size = "md", content, forceScrolling } = props;
23
+ const { size = "md", content, forceScrolling, api } = props;
24
24
  const isFixedHeight = typeof size !== "string";
25
25
  const ref = (0, react_1.useRef)(null);
26
26
  const { modalBodyDiv, modalFooterDiv, modalHeaderDiv, drawerContentStack } = (0, BeamContext_1.useBeamContext)();
@@ -37,13 +37,16 @@ function Modal(props) {
37
37
  }, ref);
38
38
  const { modalProps } = (0, react_aria_1.useModal)();
39
39
  const { dialogProps, titleProps } = (0, react_aria_1.useDialog)({ role: "dialog" }, ref);
40
- const [width, height] = getSize(size);
40
+ const [[width, height], setSize] = (0, react_1.useState)(getSize(size));
41
41
  const contentRef = (0, react_1.useRef)(null);
42
42
  const modalBodyRef = (0, react_1.useRef)(null);
43
43
  const modalFooterRef = (0, react_1.useRef)(null);
44
44
  const modalHeaderRef = (0, react_1.useRef)(null);
45
45
  const testId = (0, utils_1.useTestIds)({}, testIdPrefix);
46
46
  (0, react_aria_1.usePreventScroll)();
47
+ if (api) {
48
+ api.current = { setSize: (size = "md") => setSize(getSize(size)) };
49
+ }
47
50
  const [hasScroll, setHasScroll] = (0, react_1.useState)(forceScrolling !== null && forceScrolling !== void 0 ? forceScrolling : false);
48
51
  (0, resize_observer_1.default)(contentRef, ({ target }) => {
49
52
  if (forceScrolling === undefined && !isFixedHeight) {
@@ -4,5 +4,6 @@ export interface UseModalHook {
4
4
  openModal: (props: ModalProps) => void;
5
5
  closeModal: Callback;
6
6
  addCanClose: (canClose: CheckFn) => void;
7
+ setSize: (size: ModalProps["size"]) => void;
7
8
  }
8
9
  export declare function useModal(): UseModalHook;
@@ -7,6 +7,7 @@ const utils_1 = require("../../utils");
7
7
  function useModal() {
8
8
  const { modalState, modalCanCloseChecks } = (0, BeamContext_1.useBeamContext)();
9
9
  const lastCanClose = (0, react_1.useRef)();
10
+ const api = (0, react_1.useRef)();
10
11
  (0, react_1.useEffect)(() => {
11
12
  return () => {
12
13
  modalCanCloseChecks.current = modalCanCloseChecks.current.filter((c) => c !== lastCanClose.current);
@@ -16,7 +17,7 @@ function useModal() {
16
17
  openModal(props) {
17
18
  // TODO Check already open?
18
19
  // TODO Check can leave?
19
- modalState.current = props;
20
+ modalState.current = { ...props, api };
20
21
  },
21
22
  closeModal() {
22
23
  var _a;
@@ -38,6 +39,12 @@ function useModal() {
38
39
  ];
39
40
  lastCanClose.current = canClose;
40
41
  },
42
+ setSize(size) {
43
+ var _a;
44
+ if (modalState.current && ((_a = modalState.current.api) === null || _a === void 0 ? void 0 : _a.current)) {
45
+ modalState.current.api.current.setSize(size);
46
+ }
47
+ },
41
48
  }), [modalState, modalCanCloseChecks]);
42
49
  }
43
50
  exports.useModal = useModal;
@@ -21,7 +21,14 @@ export interface GridStyle {
21
21
  firstNonHeaderRowCss?: Properties;
22
22
  /** Applied to all cell divs (via a selector off the base div). */
23
23
  cellCss?: Properties;
24
- /** Applied to the header (really first) row div. */
24
+ /**
25
+ * Applied to the header row divs.
26
+ *
27
+ * NOTE `as=virtual`: When using a virtual table with the goal of adding space
28
+ * between the header and the first row use `firstNonHeaderRowCss` with a
29
+ * margin-top instead. Using `headerCellCss` will not work since the header
30
+ * rows are wrapper with Chrome rows.
31
+ */
25
32
  headerCellCss?: Properties;
26
33
  /** Applied to the first cell of all rows, i.e. for table-wide padding or left-side borders. */
27
34
  firstCellCss?: Properties;
@@ -96,13 +103,13 @@ export declare type RowTuple<R extends Kinded> = [GridDataRow<R> | undefined, Re
96
103
  export declare type GridSortConfig<S> = {
97
104
  on: "client";
98
105
  /** The optional initial column (index in columns) and direction to sort. */
99
- initial?: [S | GridColumn<any>, Direction];
106
+ initial?: [S | GridColumn<any>, Direction] | undefined;
100
107
  } | {
101
108
  on: "server";
102
109
  /** The current sort by value + direction (if server-side sorting). */
103
110
  value?: [S, Direction];
104
- /** Callback for when the column is sorted (if server-side sorting). */
105
- onSort: (orderBy: S, direction: Direction) => void;
111
+ /** Callback for when the column is sorted (if server-side sorting). Parameters set to `undefined` is a signal to return to the initial sort state */
112
+ onSort: (orderBy: S | undefined, direction: Direction | undefined) => void;
106
113
  };
107
114
  export interface GridTableProps<R extends Kinded, S, X> {
108
115
  id?: string;
@@ -178,18 +185,10 @@ export declare type GridTableApi = {
178
185
  */
179
186
  export declare function GridTable<R extends Kinded, S = {}, X extends Only<GridTableXss, X> = {}>(props: GridTableProps<R, S, X>): import("@emotion/react/jsx-runtime").JSX.Element;
180
187
  /**
181
- * Creates a `grid-template-column` specific to our virtual output.
182
- *
183
- * Because of two things:
184
- *
185
- * a) react-virtuoso puts the header in a different div than the normal rows, and
186
- *
187
- * b) content-aware sizing just in general look janky/constantly resize while scrolling
188
- *
189
- * When we're as=virtual, we change our default + enforce only fixed-sized units (% and px)
188
+ * Calculates column widths using a flexible `calc()` definition that allows for consistent column alignment without the use of `<table />`, CSS Grid, etc layouts.
189
+ * Enforces only fixed-sized units (% and px)
190
190
  */
191
- export declare function calcVirtualGridColumns(columns: GridColumn<any>[], firstLastColumnWidth: number | undefined): string[];
192
- export declare function calcDivGridColumns(columns: GridColumn<any>[], firstLastColumnWidth: number | undefined): string;
191
+ export declare function calcColumnSizes(columns: GridColumn<any>[], firstLastColumnWidth: number | undefined): string[];
193
192
  /**
194
193
  * Given an ADT of type T, performs a look up and returns the type of kind K.
195
194
  *
@@ -218,21 +217,8 @@ export declare type GridColumn<R extends Kinded, S = {}> = {
218
217
  } ? (data: D, row: GridRowKind<R, K>) => ReactNode | GridCellContent : (row: GridRowKind<R, K>) => ReactNode | GridCellContent);
219
218
  } & {
220
219
  /**
221
- * The column's grid column width.
222
- *
223
- * For `as=div` output:
224
- *
225
- * - Any CSS grid units are supported
226
- * - Numbers are treated as `fr` units
227
- * - The default value is `auto`, which in CSS grid will do content-aware/responsive layout.
228
- *
229
- * For `as=virtual` output:
230
- *
231
- * - Only px, percentage, or fr units are supported, due to a) react-virtuoso puts the sticky header
232
- * rows in a separate `div` and so we end up with two `grid-template-columns`, so cannot rely on
233
- * any content-aware sizing, and b) content-aware sizing in a scrolling/virtual table results in
234
- * a ~janky experience as the columns will constantly resize as new/different content is put in/out
235
- * of the DOM.
220
+ * The column's width.
221
+ * - Only px, percentage, or fr units are supported, due to each GridRow acting as an independent table. This ensures consistent alignment between rows.
236
222
  * - Numbers are treated as `fr` units
237
223
  * - The default value is `1fr`
238
224
  */
@@ -270,7 +256,7 @@ export declare type GridCellAlignment = "left" | "right" | "center";
270
256
  * primitive value for filtering and sorting.
271
257
  */
272
258
  export declare type GridCellContent = {
273
- /** The JSX content of the cell. Virtual tables that client-side sort should use a function to avaid perf overhead. */
259
+ /** The JSX content of the cell. Virtual tables that client-side sort should use a function to avoid perf overhead. */
274
260
  content: ReactNode | (() => ReactNode);
275
261
  alignment?: GridCellAlignment;
276
262
  /** Allow value to be a function in case it's a dynamic value i.e. reading from an inline-edited proxy. */
@@ -22,7 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.matchesFilter = exports.GridCollapseContext = exports.applyRowFn = exports.calcDivGridColumns = exports.calcVirtualGridColumns = exports.GridTable = exports.setGridTableDefaults = exports.setDefaultStyle = exports.setRunningInJest = exports.emptyCell = exports.DESC = exports.ASC = void 0;
25
+ exports.matchesFilter = exports.GridCollapseContext = exports.applyRowFn = exports.calcColumnSizes = exports.GridTable = exports.setGridTableDefaults = exports.setDefaultStyle = exports.setRunningInJest = exports.emptyCell = exports.DESC = exports.ASC = void 0;
26
26
  const jsx_runtime_1 = require("@emotion/react/jsx-runtime");
27
27
  const memoize_one_1 = __importDefault(require("memoize-one"));
28
28
  const mobx_react_1 = require("mobx-react");
@@ -92,12 +92,10 @@ function GridTable(props) {
92
92
  };
93
93
  }
94
94
  const [sortState, setSortKey] = (0, useSortState_1.useSortState)(columns, sorting);
95
- // Disclaimer that technically even though this is a useMemo, sortRows is mutating `rows` directly
96
95
  const maybeSorted = (0, react_1.useMemo)(() => {
97
96
  if ((sorting === null || sorting === void 0 ? void 0 : sorting.on) === "client" && sortState) {
98
97
  // If using client-side sort, the sortState use S = number
99
- (0, sortRows_1.sortRows)(columns, rows, sortState);
100
- return rows;
98
+ return (0, sortRows_1.sortRows)(columns, rows, sortState);
101
99
  }
102
100
  return rows;
103
101
  }, [columns, rows, sorting, sortState]);
@@ -106,12 +104,13 @@ function GridTable(props) {
106
104
  var _a;
107
105
  // Break up "foo bar" into `[foo, bar]` and a row must match both `foo` and `bar`
108
106
  const filters = (filter && filter.split(/ +/)) || [];
109
- const columnSizes = as === "virtual" ? calcVirtualGridColumns(columns, (_a = style.nestedCards) === null || _a === void 0 ? void 0 : _a.firstLastColumnWidth) : undefined;
107
+ const columnSizes = calcColumnSizes(columns, (_a = style.nestedCards) === null || _a === void 0 ? void 0 : _a.firstLastColumnWidth);
110
108
  function makeRowComponent(row) {
111
109
  // We only pass sortState to header rows, b/c non-headers rows shouldn't have to re-render on sorting
112
110
  // changes, and so by not passing the sortProps, it means the data rows' React.memo will still cache them.
113
111
  const sortProps = row.kind === "header" ? { sorting, sortState, setSortKey } : { sorting };
114
112
  const RowComponent = observeRows ? ObservedGridRow : MemoizedGridRow;
113
+ const openCards = row.kind === "header" ? headerCards : rowCards;
115
114
  return ((0, jsx_runtime_1.jsx)(exports.GridCollapseContext.Provider, Object.assign({ value: row.kind === "header" ? collapseAllContext : collapseRowContext }, { children: (0, jsx_runtime_1.jsx)(RowComponent, Object.assign({}, {
116
115
  as,
117
116
  columns,
@@ -120,16 +119,18 @@ function GridTable(props) {
120
119
  rowStyles,
121
120
  stickyHeader,
122
121
  stickyOffset,
123
- openCards: nestedCards ? nestedCards.currentOpenCards() : undefined,
122
+ openCards: openCards ? openCards.currentOpenCards() : undefined,
124
123
  columnSizes,
125
124
  ...sortProps,
126
- }), `${row.kind}-${row.id}`) }), `${row.kind}-${row.id}`));
125
+ }), void 0) }), `${row.kind}-${row.id}`));
127
126
  }
128
127
  // Split out the header rows from the data rows so that we can put an `infoMessage` in between them (if needed).
129
128
  const headerRows = [];
130
129
  const filteredRows = [];
131
130
  // Misc state to track our nested card-ification, i.e. interleaved actual rows + chrome rows
132
- const nestedCards = !!style.nestedCards && new nestedCards_1.NestedCards(columns, filteredRows, style);
131
+ // for the header and row (non-header rows) cards.
132
+ const headerCards = !!style.nestedCards && new nestedCards_1.NestedCards(columns, headerRows, style.nestedCards);
133
+ const rowCards = !!style.nestedCards && new nestedCards_1.NestedCards(columns, filteredRows, style.nestedCards);
133
134
  // Depth-first to filter
134
135
  function visit(row) {
135
136
  var _a;
@@ -139,32 +140,38 @@ function GridTable(props) {
139
140
  // Even if we don't pass the filter, one of our children might, so we continue on after this check
140
141
  let isCard = false;
141
142
  if (matches) {
142
- isCard = nestedCards && nestedCards.maybeOpenCard(row);
143
+ isCard = rowCards && rowCards.maybeOpenCard(row);
143
144
  filteredRows.push([row, makeRowComponent(row)]);
144
145
  }
145
146
  const isCollapsed = collapsedIds.includes(row.id);
146
147
  if (!isCollapsed && !!((_a = row.children) === null || _a === void 0 ? void 0 : _a.length)) {
147
- nestedCards && matches && nestedCards.addSpacer();
148
+ rowCards && matches && rowCards.addSpacer();
148
149
  visitRows(row.children, isCard);
149
150
  }
150
- isCard && nestedCards && nestedCards.closeCard();
151
+ isCard && rowCards && rowCards.closeCard();
151
152
  }
152
153
  function visitRows(rows, addSpacer) {
153
154
  const length = rows.length;
154
155
  rows.forEach((row, i) => {
156
+ var _a;
155
157
  if (row.kind === "header") {
156
- nestedCards && nestedCards.maybeOpenCard(row);
158
+ // Flag to determine if the header has nested card styles.
159
+ // This will determine if we should include opening and closing
160
+ // Chrome rows.
161
+ const isHeaderNested = !!((_a = style.nestedCards) === null || _a === void 0 ? void 0 : _a.kinds["header"]);
162
+ isHeaderNested && headerCards && headerCards.maybeOpenCard(row);
157
163
  headerRows.push([row, makeRowComponent(row)]);
158
- nestedCards && nestedCards.closeCard();
164
+ isHeaderNested && headerCards && headerCards.closeCard();
165
+ isHeaderNested && headerCards && headerCards.done();
159
166
  return;
160
167
  }
161
168
  visit(row);
162
- addSpacer && nestedCards && i !== length - 1 && nestedCards.addSpacer();
169
+ addSpacer && rowCards && i !== length - 1 && rowCards.addSpacer();
163
170
  });
164
171
  }
165
172
  // If nestedCards is set, we assume the top-level kind is a card, and so should add spacers between them
166
- visitRows(maybeSorted, !!nestedCards);
167
- nestedCards && nestedCards.done();
173
+ visitRows(maybeSorted, !!rowCards);
174
+ rowCards && rowCards.done();
168
175
  return [headerRows, filteredRows];
169
176
  }, [
170
177
  as,
@@ -210,22 +217,52 @@ exports.GridTable = GridTable;
210
217
  // Determine which HTML element to use to build the GridTable
211
218
  const renders = {
212
219
  table: renderTable,
213
- div: renderCssGrid,
220
+ div: renderDiv,
214
221
  virtual: renderVirtual,
215
222
  };
216
- /** Renders as a CSS Grid, which is the default / most well-supported rendered. */
217
- function renderCssGrid(style, id, columns, headerRows, filteredRows, firstRowMessage, stickyHeader, firstLastColumnWidth, xss, virtuosoRef) {
223
+ /** Renders table using divs with flexbox rows, which is the default render */
224
+ function renderDiv(style, id, columns, headerRows, filteredRows, firstRowMessage, _stickyHeader, firstLastColumnWidth, xss, _virtuosoRef) {
225
+ var _a;
226
+ // We must determine if the header is using nested card styles to account for
227
+ // the opening and closing Chrome rows.
228
+ const isNestedCardStyleHeader = !!((_a = style.nestedCards) === null || _a === void 0 ? void 0 : _a.kinds["header"]);
229
+ /*
230
+ Determine at what CSS selector index is the first non header row. Note that
231
+ CSS selectors are not zero-based, unlike JavaScript, so we must add 1 to
232
+ what we'd normally expect.
233
+
234
+ Ex: When we don't have nestedCard styled headers, then we don't have opening
235
+ and closing Chrome rows. Therefore, the first non-header row is the second
236
+ row and since CSS selectors are not zero-based, we are aiming for a value
237
+ of 2 (1 + 1).
238
+
239
+ Ex: When we have nestedCard styled headers, then we have opening and closing
240
+ Chrome rows. Therefore, the first non-header row is the fourth row because:
241
+ - Row 1 is the opening Chrome Row
242
+ - Row 2 is the header
243
+ - Row 3 is the closing Chrome Row
244
+ - Row 4 is the first non-header row
245
+ And since CSS selectors are not zero-based, we are aiming for a value of 4
246
+ (3 + 1).
247
+ */
248
+ const firstNonHeaderRowIndex = (!isNestedCardStyleHeader ? 1 : 3) + 1;
218
249
  return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ css: {
219
- ...Css_1.Css.dg.gtc(calcDivGridColumns(columns, firstLastColumnWidth)).$,
220
- // Apply the between-row styling with `div + div > *` so that we don't have to have conditional
221
- // `if !lastRow add border` CSS applied via JS that would mean the row can't be React.memo'd.
222
- // The `div + div` is also the "owl operator", i.e. don't apply to the 1st row.
223
- ...(style.betweenRowsCss ? Css_1.Css.addIn("& > div + div > *", style.betweenRowsCss).$ : {}),
224
- // removes border between header and second row
225
- ...(style.firstNonHeaderRowCss ? Css_1.Css.addIn("& > div:nth-of-type(2) > *", style.firstNonHeaderRowCss).$ : {}),
250
+ /*
251
+ Using n + (firstNonHeaderRowIndex + 1) here to target all rows that
252
+ are after the first non-header row. Since n starts at 0, we can use
253
+ the + operator as an offset.
254
+
255
+ Inspired by: https://stackoverflow.com/a/25005740/2551333
256
+ */
257
+ ...(style.betweenRowsCss
258
+ ? Css_1.Css.addIn(`& > div:nth-of-type(n + ${firstNonHeaderRowIndex + 1}) > *`, style.betweenRowsCss).$
259
+ : {}),
260
+ ...(style.firstNonHeaderRowCss
261
+ ? Css_1.Css.addIn(`& > div:nth-of-type(${firstNonHeaderRowIndex}) > *`, style.firstNonHeaderRowCss).$
262
+ : {}),
226
263
  ...style.rootCss,
227
264
  ...xss,
228
- }, "data-testid": id }, { children: [headerRows.map(([, node]) => node), firstRowMessage && ((0, jsx_runtime_1.jsx)("div", Object.assign({ css: Css_1.Css.add("gridColumn", `${columns.length} span`).$ }, { children: (0, jsx_runtime_1.jsx)("div", Object.assign({ css: { ...style.firstRowMessageCss } }, { children: firstRowMessage }), void 0) }), void 0)), filteredRows.map(([, node]) => node)] }), void 0));
265
+ }, "data-testid": id }, { children: [headerRows.map(([, node]) => node), firstRowMessage && (0, jsx_runtime_1.jsx)("div", Object.assign({ css: { ...style.firstRowMessageCss } }, { children: firstRowMessage }), void 0), filteredRows.map(([, node]) => node)] }), void 0));
229
266
  }
230
267
  /** Renders as a table, primarily/solely for good print support. */
231
268
  function renderTable(style, id, columns, headerRows, filteredRows, firstRowMessage, _stickyHeader, _firstLastColumnWidth, xss, _virtuosoRef) {
@@ -259,6 +296,7 @@ function renderTable(style, id, columns, headerRows, filteredRows, firstRowMessa
259
296
  * [3]: https://github.com/tannerlinsley/react-virtual/issues/108
260
297
  */
261
298
  function renderVirtual(style, id, columns, headerRows, filteredRows, firstRowMessage, stickyHeader, firstLastColumnWidth, xss, virtuosoRef) {
299
+ // eslint-disable-next-line react-hooks/rules-of-hooks
262
300
  const { footerStyle, listStyle } = (0, react_1.useMemo)(() => {
263
301
  var _a;
264
302
  const { paddingBottom, ...otherRootStyles } = (_a = style.rootCss) !== null && _a !== void 0 ? _a : {};
@@ -279,52 +317,66 @@ function renderVirtual(style, id, columns, headerRows, filteredRows, firstRowMes
279
317
  // so instead drill into the 1st real content cell.
280
318
  return maybeContentsDiv.firstElementChild.getBoundingClientRect().height;
281
319
  }, itemContent: (index) => {
282
- // We keep header and filter rows separate, but react-virtuoso is a flat list,
283
- // so we pick the right header / first row message / actual row.
284
- let i = index;
285
- if (i < headerRows.length) {
286
- return headerRows[i][1];
320
+ // Since we have two arrays of rows: `headerRows` and `filteredRow` we
321
+ // must determine which one to render.
322
+ // Determine if we need to render a header row
323
+ if (index < headerRows.length) {
324
+ return headerRows[index][1];
287
325
  }
288
- i -= headerRows.length;
326
+ // Reset index
327
+ index -= headerRows.length;
328
+ // Show firstRowMessage as the first `filteredRow`
289
329
  if (firstRowMessage) {
290
- if (i === 0) {
330
+ if (index === 0) {
291
331
  return ((0, jsx_runtime_1.jsx)("div", Object.assign({ css: Css_1.Css.add("gridColumn", `${columns.length} span`).$ }, { children: (0, jsx_runtime_1.jsx)("div", Object.assign({ css: { ...style.firstRowMessageCss } }, { children: firstRowMessage }), void 0) }), void 0));
292
332
  }
293
- i -= 1;
333
+ // Shift index -1 when there is a firstRowMessage to not skip the
334
+ // first `filteredRow`
335
+ index--;
294
336
  }
295
- return filteredRows[i][1];
337
+ // Lastly render `filteredRow`
338
+ return filteredRows[index][1];
296
339
  }, totalCount: (headerRows.length || 0) + (firstRowMessage ? 1 : 0) + (filteredRows.length || 0) }, void 0));
297
340
  }
298
341
  /**
299
- * Customizes the `List` element that react-virtuoso renders, to have our css grid logic.
342
+ * A table might render two of these components to represent two virtual lists.
343
+ * This generally happens when `topItemCount` prop is used and React-Virtuoso
344
+ * creates to Virtual lists where the first represents, generally, the header
345
+ * rows and the second represents the non-header rows (list rows).
346
+ *
347
+ * The main goal of this custom component is to:
348
+ * - Customize the list wrapper to our styles
300
349
  *
301
- * We wrap this in memoizeOne so that React.createElement sees a consistent/stable component
302
- * identity, even though technically we have a different "component" per the given set of props
303
- * (solely to capture as params that we can't pass through react-virtuoso's API as props).
350
+ * We wrap this in memoizeOne so that React.createElement sees a
351
+ * consistent/stable component identity, even though technically we have a
352
+ * different "component" per the given set of props (solely to capture as
353
+ * params that we can't pass through react-virtuoso's API as props).
304
354
  */
305
- const VirtualRoot = (0, memoize_one_1.default)((gs, columns, id, firstLastColumnWidth, xss) => {
355
+ const VirtualRoot = (0, memoize_one_1.default)((gs, _columns, id, _firstLastColumnWidth, xss) => {
306
356
  return react_1.default.forwardRef(function VirtualRoot({ style, children }, ref) {
307
- // This re-renders each time we have new children in the view port
357
+ // This VirtualRoot list represent the header when no styles are given. The
358
+ // table list generally has styles to scroll the page for windowing.
359
+ const isHeader = Object.keys(style || {}).length === 0;
360
+ // This re-renders each time we have new children in the viewport
308
361
  return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: { ...style, ...(gs.nestedCards ? { minWidth: "fit-content" } : {}) }, css: {
309
362
  // Add an extra `> div` due to Item + itemContent both having divs
310
363
  ...Css_1.Css.addIn("& > div + div > div > *", gs.betweenRowsCss || {}).$,
364
+ // Table list styles only
365
+ ...(isHeader
366
+ ? {}
367
+ : {
368
+ ...Css_1.Css.addIn("& > div:first-of-type > *", gs.firstNonHeaderRowCss).$,
369
+ }),
311
370
  ...gs.rootCss,
312
371
  ...xss,
313
372
  }, "data-testid": id }, { children: children }), void 0));
314
373
  });
315
374
  });
316
375
  /**
317
- * Creates a `grid-template-column` specific to our virtual output.
318
- *
319
- * Because of two things:
320
- *
321
- * a) react-virtuoso puts the header in a different div than the normal rows, and
322
- *
323
- * b) content-aware sizing just in general look janky/constantly resize while scrolling
324
- *
325
- * When we're as=virtual, we change our default + enforce only fixed-sized units (% and px)
376
+ * Calculates column widths using a flexible `calc()` definition that allows for consistent column alignment without the use of `<table />`, CSS Grid, etc layouts.
377
+ * Enforces only fixed-sized units (% and px)
326
378
  */
327
- function calcVirtualGridColumns(columns, firstLastColumnWidth) {
379
+ function calcColumnSizes(columns, firstLastColumnWidth) {
328
380
  // For both default columns (1fr) as well as `w: 4fr` columns, we translate the width into an expression that looks like:
329
381
  // calc((100% - allOtherPercent - allOtherPx) * ((myFr / totalFr))`
330
382
  //
@@ -350,7 +402,7 @@ function calcVirtualGridColumns(columns, firstLastColumnWidth) {
350
402
  return { ...acc, claimedPercentages: acc.claimedPercentages + Number(w.replace("%", "")) };
351
403
  }
352
404
  else {
353
- throw new Error("as=virtual only supports px, percentage, or fr units");
405
+ throw new Error("Beam Table column width definition only supports px, percentage, or fr units");
354
406
  }
355
407
  }, { claimedPercentages: 0, claimedPixels: firstLastColumnWidth ? firstLastColumnWidth * 2 : 0, totalFr: 0 });
356
408
  // This is our "fake but for some reason it lines up better" fr calc
@@ -369,7 +421,7 @@ function calcVirtualGridColumns(columns, firstLastColumnWidth) {
369
421
  return fr(Number(w.replace("fr", "")));
370
422
  }
371
423
  else {
372
- throw new Error("as=virtual only supports px, percentage, or fr units");
424
+ throw new Error("Beam Table column width definition only supports px, percentage, or fr units");
373
425
  }
374
426
  }
375
427
  else {
@@ -378,25 +430,7 @@ function calcVirtualGridColumns(columns, firstLastColumnWidth) {
378
430
  });
379
431
  return maybeAddCardColumns(sizes, firstLastColumnWidth);
380
432
  }
381
- exports.calcVirtualGridColumns = calcVirtualGridColumns;
382
- function calcDivGridColumns(columns, firstLastColumnWidth) {
383
- const sizes = columns.map(({ w }) => {
384
- if (typeof w === "undefined") {
385
- // Hrm, I waffle between 'auto' or '1fr' being the better default here...
386
- return "auto";
387
- }
388
- else if (typeof w === "string") {
389
- // Use whatever the user passed in
390
- return w;
391
- }
392
- else {
393
- // Otherwise assume fr units
394
- return `${w}fr`;
395
- }
396
- });
397
- return maybeAddCardColumns(sizes, firstLastColumnWidth).join(" ");
398
- }
399
- exports.calcDivGridColumns = calcDivGridColumns;
433
+ exports.calcColumnSizes = calcColumnSizes;
400
434
  // If we're doing nested cards, we add extra 1st/last cells...
401
435
  function maybeAddCardColumns(sizes, firstLastColumnWidth) {
402
436
  return !firstLastColumnWidth ? sizes : [`${firstLastColumnWidth}px`, ...sizes, `${firstLastColumnWidth}px`];
@@ -420,19 +454,15 @@ function GridRow(props) {
420
454
  const rowStyle = rowStyles === null || rowStyles === void 0 ? void 0 : rowStyles[row.kind];
421
455
  const rowStyleCellCss = maybeApplyFunction(row, rowStyle === null || rowStyle === void 0 ? void 0 : rowStyle.cellCss);
422
456
  const rowCss = {
423
- // We add a display-contents so that we can have "row-level" div elements in our
424
- // DOM structure. I.e. grid is normally root-element > cell-elements, but we want
425
- // root-element > row-element > cell-elements, so that we can have a hook for
426
- // hovers and styling. In theory this would change with subgrids.
427
- // Only enable when using div as elements
428
457
  // For virtual tables use `display: flex` to keep all cells on the same row, but use `flexNone` to ensure the cells stay their defined widths
429
- ...(as === "table" ? {} : as === "virtual" ? Css_1.Css.df.addIn("&>*", Css_1.Css.flexNone.$).$ : Css_1.Css.display("contents").$),
458
+ ...(as === "table" ? {} : Css_1.Css.df.addIn("&>*", Css_1.Css.flexNone.$).$),
430
459
  ...(((rowStyle === null || rowStyle === void 0 ? void 0 : rowStyle.rowLink) || (rowStyle === null || rowStyle === void 0 ? void 0 : rowStyle.onClick)) &&
431
460
  style.rowHoverColor && {
432
461
  // Even though backgroundColor is set on the cellCss (due to display: content), the hover target is the row.
433
462
  "&:hover > *": Css_1.Css.cursorPointer.bgColor(maybeDarken(rowStyleCellCss === null || rowStyleCellCss === void 0 ? void 0 : rowStyleCellCss.backgroundColor, style.rowHoverColor)).$,
434
463
  }),
435
464
  ...maybeApplyFunction(row, rowStyle === null || rowStyle === void 0 ? void 0 : rowStyle.rowCss),
465
+ ...(isHeader && stickyHeader ? Css_1.Css.sticky.top(stickyOffset).z1.$ : undefined),
436
466
  };
437
467
  const Row = as === "table" ? "tr" : "div";
438
468
  const openCardStyles = typeof openCards === "string"
@@ -483,18 +513,11 @@ function GridRow(props) {
483
513
  ...getIndentationCss(style, rowStyle, columnIndex, maybeContent),
484
514
  // Then apply any header-specific override
485
515
  ...(isHeader && style.headerCellCss),
486
- // Non-virtualized tables use h100 so that all cells are the same height across the row.
487
- // Virtualized table rows use `display: flex;`, so the flex children are set to `align-self: stretch` by default, which achieves the same goal.
488
- // Though, we need to omit setting `h100` on the flex children, as a flex container needs a defined height set for `h100` to work on flex children
489
- ...(isHeader && as !== "virtual" ? Css_1.Css.h100.$ : undefined),
490
- ...maybeStickyHeaderStyles,
491
516
  // If we're within a card, use its background color
492
517
  ...(currentCard && Css_1.Css.bgColor(currentCard.bgColor).$),
493
- // Add in colspan css if needed
494
- ...(currentColspan > 1 ? Css_1.Css.gc(`span ${currentColspan}`).$ : {}),
495
518
  // And finally the specific cell's css (if any from GridCellContent)
496
519
  ...rowStyleCellCss,
497
- // For virtual tables we define the width of the column on each cell. Supports col spans.
520
+ // Define the width of the column on each cell. Supports col spans.
498
521
  ...(columnSizes && {
499
522
  width: `calc(${columnSizes
500
523
  .slice(maybeNestedCardColumnIndex, maybeNestedCardColumnIndex + currentColspan)
@@ -581,8 +604,8 @@ function applyRowFn(column, row) {
581
604
  exports.applyRowFn = applyRowFn;
582
605
  /** Renders our default cell element, i.e. if no row links and no custom renderCell are used. */
583
606
  const defaultRenderFn = (as) => (key, css, content) => {
584
- const Row = as === "table" ? "td" : "div";
585
- return ((0, jsx_runtime_1.jsx)(Row, Object.assign({ css: { ...css, ...tableRowStyles(as) } }, { children: content }), key));
607
+ const Cell = as === "table" ? "td" : "div";
608
+ return ((0, jsx_runtime_1.jsx)(Cell, Object.assign({ css: { ...css, ...tableRowStyles(as) } }, { children: content }), key));
586
609
  };
587
610
  exports.GridCollapseContext = react_1.default.createContext({
588
611
  headerCollapsed: false,
@@ -6,6 +6,7 @@ const react_1 = require("react");
6
6
  const Icon_1 = require("../Icon");
7
7
  const GridSortContext_1 = require("./GridSortContext");
8
8
  const Css_1 = require("../../Css");
9
+ const hooks_1 = require("../../hooks");
9
10
  const useTestIds_1 = require("../../utils/useTestIds");
10
11
  /**
11
12
  * Wraps column header names with up/down sorting icons.
@@ -20,8 +21,12 @@ const useTestIds_1 = require("../../utils/useTestIds");
20
21
  */
21
22
  function SortHeader(props) {
22
23
  const { content, xss } = props;
24
+ const { isHovered, hoverProps } = (0, hooks_1.useHover)({});
23
25
  const { sorted, toggleSort } = (0, react_1.useContext)(GridSortContext_1.GridSortContext);
24
26
  const tid = (0, useTestIds_1.useTestIds)(props, "sortHeader");
25
- return ((0, jsx_runtime_1.jsxs)("div", Object.assign({}, tid, { css: { ...Css_1.Css.df.aic.cursorPointer.selectNone.$, ...xss }, onClick: toggleSort }, { children: [content, sorted === "ASC" && (0, jsx_runtime_1.jsx)(Icon_1.Icon, Object.assign({ icon: "sortUp", inc: 2 }, tid.icon, { xss: Css_1.Css.mlPx(4).$ }), void 0), sorted === "DESC" && (0, jsx_runtime_1.jsx)(Icon_1.Icon, Object.assign({ icon: "sortDown", inc: 2 }, tid.icon, { xss: Css_1.Css.mlPx(4).$ }), void 0)] }), void 0));
27
+ return ((0, jsx_runtime_1.jsxs)("div", Object.assign({}, tid, { css: { ...Css_1.Css.df.aic.h100.cursorPointer.selectNone.$, ...xss } }, hoverProps, { onClick: toggleSort }, { children: [content, (0, jsx_runtime_1.jsx)("span", Object.assign({ css: Css_1.Css.fs0.$ }, { children: (0, jsx_runtime_1.jsx)(Icon_1.Icon, Object.assign({ icon: sorted === "DESC" ? "sortDown" : "sortUp", color: sorted !== undefined ? Css_1.Palette.LightBlue700 : Css_1.Palette.Gray400, xss: Css_1.Css.ml1
28
+ .visibility("hidden")
29
+ .if(isHovered || sorted !== undefined)
30
+ .visibility("visible").$, inc: 2 }, tid.icon), void 0) }), void 0)] }), void 0));
26
31
  }
27
32
  exports.SortHeader = SortHeader;
@@ -1,5 +1,7 @@
1
1
  import { ReactElement } from "react";
2
- import { GridColumn, GridDataRow, GridStyle, Kinded, NestedCardsStyle, NestedCardStyle, NestedCardStyleByKind, RowTuple } from "./GridTable";
2
+ import { GridColumn, GridDataRow, Kinded, NestedCardsStyle, NestedCardStyle, NestedCardStyleByKind, RowTuple } from "./GridTable";
3
+ declare type Chrome = () => JSX.Element;
4
+ declare type ChromeBuffer = Chrome[];
3
5
  /**
4
6
  * A helper class to create our nested card DOM shenanigans.
5
7
  *
@@ -12,22 +14,47 @@ import { GridColumn, GridDataRow, GridStyle, Kinded, NestedCardsStyle, NestedCar
12
14
  * a content row itself, the nested padding is handled separately by the
13
15
  * GridRow component.
14
16
  */
15
- declare type Chrome = () => JSX.Element;
16
- declare type ChromeBuffer = Chrome[];
17
17
  export declare class NestedCards {
18
- private columns;
19
- private filteredRows;
20
- private style;
18
+ private readonly columns;
19
+ private readonly rows;
20
+ private readonly styles;
21
21
  private readonly openCards;
22
22
  private readonly chromeBuffer;
23
- private readonly styles;
24
- constructor(columns: GridColumn<any>[], filteredRows: RowTuple<any>[], style: GridStyle);
23
+ private chromeRowIndex;
24
+ constructor(columns: GridColumn<any>[], rows: RowTuple<any>[], styles: NestedCardsStyle);
25
+ /**
26
+ * Maybe add an opening Chrome row to the given `row` if its a nested card.
27
+ *
28
+ * @param row The row which will be opened. The open Chrome row will appear
29
+ * above this row.
30
+ */
25
31
  maybeOpenCard(row: GridDataRow<any>): boolean;
26
32
  closeCard(): void;
27
33
  addSpacer(): void;
34
+ /**
35
+ * Close the remaining open rows with a close Chrome row.
36
+ *
37
+ * @param row The row that is completing/closing nested open Chrome rows.
38
+ */
28
39
  done(): void;
29
40
  /** Return a stable copy of the cards, so it won't change as we keep going. */
30
41
  currentOpenCards(): string;
42
+ /**
43
+ * Takes the current Chrome row buffer of close row(s), spacers, and open row,
44
+ * and creates a single chrome DOM row.
45
+ *
46
+ * This allows a minimal amount of DOM overhead, insofar as to the css-grid or
47
+ * react-virtuoso we only add 1 extra DOM node between each row of content to
48
+ * achieve our nested card look & feel.
49
+ *
50
+ * i.e.:
51
+ * - chrome row (open)
52
+ * - card1 content row
53
+ * - chrome row (card2 open)
54
+ * - nested card2 content row
55
+ * - chrome row (card2 close, card1 close)
56
+ */
57
+ maybeCreateChromeRow(): void;
31
58
  }
32
59
  /**
33
60
  * Draws the rounded corners (either open or close) for a new card.
@@ -55,23 +82,10 @@ export declare function maybeAddCardPadding(openCards: NestedCardStyle[], column
55
82
  * have any open cards, but still want a space between top-level cards.
56
83
  */
57
84
  export declare function makeSpacer(height: number, openCards: string[], styles: NestedCardsStyle): Chrome;
58
- /**
59
- * Takes the current buffer of close row(s), spacers, and open row, and creates a single chrome DOM row.
60
- *
61
- * This allows a minimal amount of DOM overhead, insofar as to the css-grid or react-virtuoso we only
62
- * 1 extra DOM node between each row of content to achieve our nested card look & feel, i.e.:
63
- *
64
- * - chrome row (open)
65
- * - card1 content row
66
- * - chrome row (card2 open)
67
- * - nested card2 content row
68
- * - chrome row (card2 close, card1 close)
69
- */
70
- export declare function maybeCreateChromeRow(columns: GridColumn<any>[], filteredRows: RowTuple<any>[], chromeBuffer: ChromeBuffer): void;
71
85
  interface ChromeRowProps {
72
86
  chromeBuffer: ChromeBuffer;
73
87
  columns: number;
74
88
  }
75
89
  export declare function ChromeRow({ chromeBuffer, columns }: ChromeRowProps): import("@emotion/react/jsx-runtime").JSX.Element;
76
- export declare function dropChromeRows<R extends Kinded>(filteredRows: RowTuple<R>[]): [GridDataRow<R>, ReactElement][];
90
+ export declare function dropChromeRows<R extends Kinded>(rows: RowTuple<R>[]): [GridDataRow<R>, ReactElement][];
77
91
  export {};
@@ -1,20 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dropChromeRows = exports.ChromeRow = exports.maybeCreateChromeRow = exports.makeSpacer = exports.maybeAddCardPadding = exports.makeOpenOrCloseCard = exports.NestedCards = void 0;
3
+ exports.dropChromeRows = exports.ChromeRow = exports.makeSpacer = exports.maybeAddCardPadding = exports.makeOpenOrCloseCard = exports.NestedCards = void 0;
4
4
  const jsx_runtime_1 = require("@emotion/react/jsx-runtime");
5
5
  const react_1 = require("react");
6
6
  const Css_1 = require("../../Css");
7
+ /**
8
+ * A helper class to create our nested card DOM shenanigans.
9
+ *
10
+ * This acts as a one-off visitor that accepts "begin row", "between row",
11
+ * "end row" calls from GridTable while its translating the user's nested
12
+ * GridDataRows into a flat list of RowTuples, and interjects fake/chrome
13
+ * rows into `filteredRows` as necessary.
14
+ *
15
+ * Note that this class only handles *between row* chrome and that within
16
+ * a content row itself, the nested padding is handled separately by the
17
+ * GridRow component.
18
+ */
7
19
  class NestedCards {
8
- constructor(columns, filteredRows, style) {
20
+ constructor(columns,
21
+ // An array of rows which we will add open Chrome rows too to create the table.
22
+ rows, styles) {
9
23
  this.columns = columns;
10
- this.filteredRows = filteredRows;
11
- this.style = style;
24
+ this.rows = rows;
25
+ this.styles = styles;
12
26
  // A stack of the current cards we're showing
13
27
  this.openCards = [];
14
28
  // A buffer of the open/close/spacer rows we need between each content row.
15
29
  this.chromeBuffer = [];
16
- this.styles = style.nestedCards;
30
+ this.chromeRowIndex = 0;
17
31
  }
32
+ /**
33
+ * Maybe add an opening Chrome row to the given `row` if its a nested card.
34
+ *
35
+ * @param row The row which will be opened. The open Chrome row will appear
36
+ * above this row.
37
+ */
18
38
  maybeOpenCard(row) {
19
39
  const card = this.styles.kinds[row.kind];
20
40
  // If this kind doesn't have a card defined, don't put it on the card stack
@@ -23,7 +43,7 @@ class NestedCards {
23
43
  this.chromeBuffer.push(makeOpenOrCloseCard(this.openCards, this.styles.kinds, "open"));
24
44
  }
25
45
  // But always close previous cards if needed
26
- maybeCreateChromeRow(this.columns, this.filteredRows, this.chromeBuffer);
46
+ this.maybeCreateChromeRow();
27
47
  return !!card;
28
48
  }
29
49
  closeCard() {
@@ -33,13 +53,43 @@ class NestedCards {
33
53
  addSpacer() {
34
54
  this.chromeBuffer.push(makeSpacer(this.styles.spacerPx, this.openCards, this.styles));
35
55
  }
56
+ /**
57
+ * Close the remaining open rows with a close Chrome row.
58
+ *
59
+ * @param row The row that is completing/closing nested open Chrome rows.
60
+ */
36
61
  done() {
37
- maybeCreateChromeRow(this.columns, this.filteredRows, this.chromeBuffer);
62
+ this.maybeCreateChromeRow();
38
63
  }
39
64
  /** Return a stable copy of the cards, so it won't change as we keep going. */
40
65
  currentOpenCards() {
41
66
  return this.openCards.join(":");
42
67
  }
68
+ /**
69
+ * Takes the current Chrome row buffer of close row(s), spacers, and open row,
70
+ * and creates a single chrome DOM row.
71
+ *
72
+ * This allows a minimal amount of DOM overhead, insofar as to the css-grid or
73
+ * react-virtuoso we only add 1 extra DOM node between each row of content to
74
+ * achieve our nested card look & feel.
75
+ *
76
+ * i.e.:
77
+ * - chrome row (open)
78
+ * - card1 content row
79
+ * - chrome row (card2 open)
80
+ * - nested card2 content row
81
+ * - chrome row (card2 close, card1 close)
82
+ */
83
+ maybeCreateChromeRow() {
84
+ if (this.chromeBuffer.length > 0) {
85
+ this.rows.push([
86
+ undefined,
87
+ (0, jsx_runtime_1.jsx)(ChromeRow, { chromeBuffer: [...this.chromeBuffer], columns: this.columns.length }, this.chromeRowIndex++),
88
+ ]);
89
+ // clear the Chrome buffer
90
+ this.chromeBuffer.splice(0, this.chromeBuffer.length);
91
+ }
92
+ }
43
93
  }
44
94
  exports.NestedCards = NestedCards;
45
95
  /**
@@ -125,33 +175,13 @@ function makeSpacer(height, openCards, styles) {
125
175
  };
126
176
  }
127
177
  exports.makeSpacer = makeSpacer;
128
- /**
129
- * Takes the current buffer of close row(s), spacers, and open row, and creates a single chrome DOM row.
130
- *
131
- * This allows a minimal amount of DOM overhead, insofar as to the css-grid or react-virtuoso we only
132
- * 1 extra DOM node between each row of content to achieve our nested card look & feel, i.e.:
133
- *
134
- * - chrome row (open)
135
- * - card1 content row
136
- * - chrome row (card2 open)
137
- * - nested card2 content row
138
- * - chrome row (card2 close, card1 close)
139
- */
140
- function maybeCreateChromeRow(columns, filteredRows, chromeBuffer) {
141
- if (chromeBuffer.length > 0) {
142
- filteredRows.push([undefined, (0, jsx_runtime_1.jsx)(ChromeRow, { chromeBuffer: [...chromeBuffer], columns: columns.length }, void 0)]);
143
- // clear the buffer
144
- chromeBuffer.splice(0, chromeBuffer.length);
145
- }
146
- }
147
- exports.maybeCreateChromeRow = maybeCreateChromeRow;
148
178
  function ChromeRow({ chromeBuffer, columns }) {
149
179
  return (
150
180
  // We add 2 to account for our dedicated open/close columns
151
181
  (0, jsx_runtime_1.jsx)("div", Object.assign({ css: Css_1.Css.gc(`span ${columns + 2}`).$, "data-chrome": "true" }, { children: chromeBuffer.map((c, i) => ((0, jsx_runtime_1.jsx)(react_1.Fragment, { children: c() }, i))) }), void 0));
152
182
  }
153
183
  exports.ChromeRow = ChromeRow;
154
- function dropChromeRows(filteredRows) {
155
- return filteredRows.filter(([r]) => !!r);
184
+ function dropChromeRows(rows) {
185
+ return rows.filter(([r]) => !!r);
156
186
  }
157
187
  exports.dropChromeRows = dropChromeRows;
@@ -1,5 +1,5 @@
1
1
  import { ReactNode } from "react";
2
2
  import { GridCellContent, GridColumn, GridDataRow, GridSortConfig, Kinded } from "./GridTable";
3
3
  import { SortState } from "./useSortState";
4
- export declare function sortRows<R extends Kinded>(columns: GridColumn<R>[], rows: GridDataRow<R>[], sortState: SortState<number>): void;
4
+ export declare function sortRows<R extends Kinded>(columns: GridColumn<R>[], rows: GridDataRow<R>[], sortState: SortState<number>): GridDataRow<R>[];
5
5
  export declare function ensureClientSideSortValueIsSortable(sorting: GridSortConfig<any> | undefined, isHeader: boolean, column: GridColumn<any>, idx: number, maybeContent: ReactNode | GridCellContent): void;
@@ -2,21 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ensureClientSideSortValueIsSortable = exports.sortRows = void 0;
4
4
  const GridTable_1 = require("./GridTable");
5
- // We currently mutate `rows` while sorting; this would be bad if rows was directly
6
- // read from an immutable store like the apollo cache, but we basically always make
7
- // a copy in the process of adding our `kind` tags.
8
- //
9
- // I suppose that is an interesting idea, would we ever want to render a GQL query/cache
10
- // result directly into the table without first doing a kind-mapping? Like maybe we could
11
- // use __typename as the kind.
5
+ // Returns a shallow copy of the `rows` parameter sorted based on `sortState`
12
6
  function sortRows(columns, rows, sortState) {
13
- sortBatch(columns, rows, sortState);
7
+ const sorted = sortBatch(columns, rows, sortState);
14
8
  // Recursively sort child rows
15
- for (const row of rows) {
9
+ sorted.forEach((row, i) => {
16
10
  if (row.children) {
17
- sortRows(columns, row.children, sortState);
11
+ sorted[i] = { ...sorted[i], children: sortRows(columns, row.children, sortState) };
18
12
  }
19
- }
13
+ });
14
+ return sorted;
20
15
  }
21
16
  exports.sortRows = sortRows;
22
17
  function sortBatch(columns, batch, sortState) {
@@ -24,7 +19,8 @@ function sortBatch(columns, batch, sortState) {
24
19
  const [value, direction] = sortState;
25
20
  const column = columns[value];
26
21
  const invert = direction === "DESC";
27
- batch.sort((a, b) => {
22
+ // Make a shallow copy for sorting to avoid mutating the original list
23
+ return [...batch].sort((a, b) => {
28
24
  const v1 = sortValue((0, GridTable_1.applyRowFn)(column, a));
29
25
  const v2 = sortValue((0, GridTable_1.applyRowFn)(column, b));
30
26
  const v1e = v1 === null || v1 === undefined;
@@ -10,3 +10,4 @@ import { Direction, GridColumn, GridSortConfig, Kinded } from "./GridTable";
10
10
  export declare type SortState<S> = readonly [S, Direction];
11
11
  /** Small custom hook that wraps the "setSortColumn inverts the current sort" logic. */
12
12
  export declare function useSortState<R extends Kinded, S>(columns: GridColumn<R, S>[], sorting?: GridSortConfig<S>): [SortState<S> | undefined, (value: S) => void];
13
+ export declare function deriveSortState<S>(currentSortState: SortState<S> | undefined, clickedKey: S, initialSortState: SortState<S> | undefined): SortState<S> | undefined;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useSortState = void 0;
3
+ exports.deriveSortState = exports.useSortState = void 0;
4
4
  const react_1 = require("react");
5
5
  const GridTable_1 = require("./GridTable");
6
6
  /** Small custom hook that wraps the "setSortColumn inverts the current sort" logic. */
@@ -8,34 +8,34 @@ function useSortState(columns, sorting) {
8
8
  // If we're server-side sorting, use the caller's `sorting.value` prop to initialize our internal
9
9
  // `useState`. After this, we ignore `sorting.value` because we assume it should match what our
10
10
  // `setSortState` just changed anyway (in response to the user sorting a column).
11
- const [sortState, setSortState] = (0, react_1.useState)(() => {
11
+ const initialSortState = (0, react_1.useMemo)(() => {
12
12
  if ((sorting === null || sorting === void 0 ? void 0 : sorting.on) === "client") {
13
13
  const { initial } = sorting;
14
- if (initial) {
14
+ if (initial === undefined && "initial" in sorting) {
15
+ // if explicitly set to `undefined`, then do not sort
16
+ return undefined;
17
+ }
18
+ else if (initial) {
15
19
  const key = typeof initial[0] === "number" ? initial[0] : columns.indexOf(initial[0]);
16
20
  return [key, initial[1]];
17
21
  }
18
22
  else {
19
23
  // If no explicit sorting, assume 1st column ascending
20
24
  const firstSortableColumn = columns.findIndex((c) => c.clientSideSort !== false);
21
- return [firstSortableColumn, "ASC"];
25
+ return [firstSortableColumn, GridTable_1.ASC];
22
26
  }
23
27
  }
24
28
  else {
25
29
  return sorting === null || sorting === void 0 ? void 0 : sorting.value;
26
30
  }
27
- });
28
- // Make a custom setSortKey that is useState-like but contains the invert-if-same-column-clicked-twice logic.
31
+ }, [sorting, columns]);
32
+ const [sortState, setSortState] = (0, react_1.useState)(initialSortState);
33
+ // Make a custom setSortKey that is useState-like but contains the ASC->DESC->RESET logic.
29
34
  const setSortKey = (0, react_1.useCallback)((clickedKey) => {
30
- const [currentKey, currentDirection] = sortState || [];
31
- const [newKey, newDirection] =
32
- // If clickedKey === currentKey, then toggle direction
33
- clickedKey === currentKey
34
- ? [currentKey, currentDirection === GridTable_1.ASC ? GridTable_1.DESC : GridTable_1.ASC]
35
- : // Otherwise, use the new key, and default to ascending
36
- [clickedKey, GridTable_1.ASC];
37
- setSortState([newKey, newDirection]);
35
+ const newState = deriveSortState(sortState, clickedKey, initialSortState);
36
+ setSortState(newState);
38
37
  if ((sorting === null || sorting === void 0 ? void 0 : sorting.on) === "server") {
38
+ const [newKey, newDirection] = newState !== null && newState !== void 0 ? newState : [undefined, undefined];
39
39
  sorting.onSort(newKey, newDirection);
40
40
  }
41
41
  },
@@ -44,3 +44,19 @@ function useSortState(columns, sorting) {
44
44
  return [sortState, setSortKey];
45
45
  }
46
46
  exports.useSortState = useSortState;
47
+ // Exported for testing purposes
48
+ function deriveSortState(currentSortState, clickedKey, initialSortState) {
49
+ const [currentKey, currentDirection] = currentSortState || [];
50
+ // If the current sort state is not defined, or clicking a new column, then sort ASC on the clicked key
51
+ if (!currentSortState || clickedKey !== currentKey) {
52
+ return [clickedKey, GridTable_1.ASC];
53
+ }
54
+ // Otherwise when clicking the current column, toggle through sort states
55
+ if (currentDirection === GridTable_1.ASC) {
56
+ // if ASC -> go to desc
57
+ return [clickedKey, GridTable_1.DESC];
58
+ }
59
+ // Else, direction is already DESC, so revert to original sort value.
60
+ return initialSortState;
61
+ }
62
+ exports.deriveSortState = deriveSortState;
@@ -4,40 +4,27 @@ exports.CompoundField = void 0;
4
4
  const jsx_runtime_1 = require("@emotion/react/jsx-runtime");
5
5
  const react_1 = require("react");
6
6
  const Css_1 = require("../../Css");
7
- const utils_1 = require("../../utils");
8
7
  /** Internal component to help create compound fields */
9
8
  function CompoundField({ children }) {
10
9
  if ((children === null || children === void 0 ? void 0 : children.length) !== 2) {
11
10
  throw global.Error("CompoundField requires two children components");
12
11
  }
13
- const [focusedEl, setFocusedEl] = (0, react_1.useState)();
14
12
  const commonStyles = Css_1.Css.df.aic.fs1.maxwPx(550).bt.bb.bGray300.$;
15
13
  const internalProps = { compound: true };
16
- return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ css: Css_1.Css.df.$ }, { children: [(0, jsx_runtime_1.jsx)("div", Object.assign({ css: {
14
+ return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ css: {
15
+ ...Css_1.Css.df.$,
16
+ "&:focus-within > div:nth-of-type(2)": Css_1.Css.bgLightBlue700.$, // Separation line when inputs are focused
17
+ } }, { children: [(0, jsx_runtime_1.jsx)("div", Object.assign({ css: {
17
18
  ...commonStyles,
18
- ...Css_1.Css.bl.borderRadius("4px 0 0 4px").if(focusedEl === "left").bLightBlue700.$,
19
+ ...Css_1.Css.bl.borderRadius("4px 0 0 4px").$,
20
+ "&:focus-within": Css_1.Css.bLightBlue700.$,
19
21
  } }, { children: (0, react_1.cloneElement)(children[0], {
20
- onFocus: () => {
21
- (0, utils_1.maybeCall)(children[0].props.onFocus);
22
- setFocusedEl("left");
23
- },
24
- onBlur: () => {
25
- (0, utils_1.maybeCall)(children[0].props.onBlur);
26
- setFocusedEl(undefined);
27
- },
28
22
  internalProps,
29
- }) }), void 0), (0, jsx_runtime_1.jsx)("div", { css: Css_1.Css.wPx(1).flexNone.bgGray300.if(focusedEl !== undefined).bgLightBlue700.$ }, void 0), (0, jsx_runtime_1.jsx)("div", Object.assign({ css: {
23
+ }) }), void 0), (0, jsx_runtime_1.jsx)("div", { css: Css_1.Css.wPx(1).flexNone.bgGray300.$ }, void 0), (0, jsx_runtime_1.jsx)("div", Object.assign({ css: {
30
24
  ...commonStyles,
31
- ...Css_1.Css.fg1.br.borderRadius("0 4px 4px 0").if(focusedEl === "right").bLightBlue700.$,
25
+ ...Css_1.Css.fg1.br.borderRadius("0 4px 4px 0").$,
26
+ "&:focus-within": Css_1.Css.bLightBlue700.$,
32
27
  } }, { children: (0, react_1.cloneElement)(children[1], {
33
- onFocus: () => {
34
- (0, utils_1.maybeCall)(children[1].props.onFocus);
35
- setFocusedEl("right");
36
- },
37
- onBlur: () => {
38
- (0, utils_1.maybeCall)(children[1].props.onBlur);
39
- setFocusedEl(undefined);
40
- },
41
28
  internalProps,
42
29
  }) }), void 0)] }), void 0));
43
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/beam",
3
- "version": "2.98.2",
3
+ "version": "2.101.0",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",