@homebound/beam 2.98.1 → 2.100.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;
@@ -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]);
@@ -112,6 +110,7 @@ function GridTable(props) {
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,
@@ -214,15 +221,46 @@ const renders = {
214
221
  virtual: renderVirtual,
215
222
  };
216
223
  /** 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) {
224
+ function renderCssGrid(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
250
  ...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).$ : {}),
251
+ /*
252
+ Using n + (firstNonHeaderRowIndex + 1) here to target all rows that
253
+ are after the first non-header row. Since n starts at 0, we can use
254
+ the + operator as an offset.
255
+
256
+ Inspired by: https://stackoverflow.com/a/25005740/2551333
257
+ */
258
+ ...(style.betweenRowsCss
259
+ ? Css_1.Css.addIn(`& > div:nth-of-type(n + ${firstNonHeaderRowIndex + 1}) > *`, style.betweenRowsCss).$
260
+ : {}),
261
+ ...(style.firstNonHeaderRowCss
262
+ ? Css_1.Css.addIn(`& > div:nth-of-type(${firstNonHeaderRowIndex}) > *`, style.firstNonHeaderRowCss).$
263
+ : {}),
226
264
  ...style.rootCss,
227
265
  ...xss,
228
266
  }, "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));
@@ -259,6 +297,7 @@ function renderTable(style, id, columns, headerRows, filteredRows, firstRowMessa
259
297
  * [3]: https://github.com/tannerlinsley/react-virtual/issues/108
260
298
  */
261
299
  function renderVirtual(style, id, columns, headerRows, filteredRows, firstRowMessage, stickyHeader, firstLastColumnWidth, xss, virtuosoRef) {
300
+ // eslint-disable-next-line react-hooks/rules-of-hooks
262
301
  const { footerStyle, listStyle } = (0, react_1.useMemo)(() => {
263
302
  var _a;
264
303
  const { paddingBottom, ...otherRootStyles } = (_a = style.rootCss) !== null && _a !== void 0 ? _a : {};
@@ -279,35 +318,56 @@ function renderVirtual(style, id, columns, headerRows, filteredRows, firstRowMes
279
318
  // so instead drill into the 1st real content cell.
280
319
  return maybeContentsDiv.firstElementChild.getBoundingClientRect().height;
281
320
  }, 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];
321
+ // Since we have two arrays of rows: `headerRows` and `filteredRow` we
322
+ // must determine which one to render.
323
+ // Determine if we need to render a header row
324
+ if (index < headerRows.length) {
325
+ return headerRows[index][1];
287
326
  }
288
- i -= headerRows.length;
327
+ // Reset index
328
+ index -= headerRows.length;
329
+ // Show firstRowMessage as the first `filteredRow`
289
330
  if (firstRowMessage) {
290
- if (i === 0) {
331
+ if (index === 0) {
291
332
  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
333
  }
293
- i -= 1;
334
+ // Shift index -1 when there is a firstRowMessage to not skip the
335
+ // first `filteredRow`
336
+ index--;
294
337
  }
295
- return filteredRows[i][1];
338
+ // Lastly render `filteredRow`
339
+ return filteredRows[index][1];
296
340
  }, totalCount: (headerRows.length || 0) + (firstRowMessage ? 1 : 0) + (filteredRows.length || 0) }, void 0));
297
341
  }
298
342
  /**
299
- * Customizes the `List` element that react-virtuoso renders, to have our css grid logic.
343
+ * A table might render two of these components to represent two virtual lists.
344
+ * This generally happens when `topItemCount` prop is used and React-Virtuoso
345
+ * creates to Virtual lists where the first represents, generally, the header
346
+ * rows and the second represents the non-header rows (list rows).
300
347
  *
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).
348
+ * The main goal of this custom component is to:
349
+ * - Customize the list wrapper to our css grid logic styles
350
+ *
351
+ * We wrap this in memoizeOne so that React.createElement sees a
352
+ * consistent/stable component identity, even though technically we have a
353
+ * different "component" per the given set of props (solely to capture as
354
+ * params that we can't pass through react-virtuoso's API as props).
304
355
  */
305
- const VirtualRoot = (0, memoize_one_1.default)((gs, columns, id, firstLastColumnWidth, xss) => {
356
+ const VirtualRoot = (0, memoize_one_1.default)((gs, _columns, id, _firstLastColumnWidth, xss) => {
306
357
  return react_1.default.forwardRef(function VirtualRoot({ style, children }, ref) {
307
- // This re-renders each time we have new children in the view port
308
- return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: style, css: {
358
+ // This VirtualRoot list represent the header when no styles are given. The
359
+ // table list generally has styles to scroll the page for windowing.
360
+ const isHeader = Object.keys(style || {}).length === 0;
361
+ // This re-renders each time we have new children in the viewport
362
+ return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: { ...style, ...(gs.nestedCards ? { minWidth: "fit-content" } : {}) }, css: {
309
363
  // Add an extra `> div` due to Item + itemContent both having divs
310
364
  ...Css_1.Css.addIn("& > div + div > div > *", gs.betweenRowsCss || {}).$,
365
+ // Table list styles only
366
+ ...(isHeader
367
+ ? {}
368
+ : {
369
+ ...Css_1.Css.addIn("& > div:first-of-type > *", gs.firstNonHeaderRowCss).$,
370
+ }),
311
371
  ...gs.rootCss,
312
372
  ...xss,
313
373
  }, "data-testid": id }, { children: children }), void 0));
@@ -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.1",
3
+ "version": "2.100.0",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -48,7 +48,7 @@
48
48
  "react-router": "^5.2.0",
49
49
  "react-router-dom": "^5.2.0",
50
50
  "react-stately": "^3.9.0",
51
- "react-virtuoso": "^2.3.1",
51
+ "react-virtuoso": "^2.4.0",
52
52
  "tinycolor2": "^1.4.2",
53
53
  "tributejs": "^5.1.3",
54
54
  "trix": "^1.3.1",