@dxos/react-ui-table 0.4.10-main.4c7b3fa → 0.4.10-main.4d48ccf

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.
@@ -92,7 +92,7 @@ var CellCombobox = ({ model, value, onValueChange }) => {
92
92
 
93
93
  // packages/ui/react-ui-table/src/components/ColumnMenu/ColumnMenu.tsx
94
94
  import { X as X2, GearSix, CaretDown as CaretDown2, ArrowDown, ArrowUp } from "@phosphor-icons/react";
95
- import React3, { useRef as useRef2, useState as useState5, useCallback as useCallback3 } from "react";
95
+ import React3, { useRef as useRef2, useState as useState6, useCallback as useCallback5 } from "react";
96
96
  import { Button as Button2, DensityProvider, Popover as Popover2, DropdownMenu } from "@dxos/react-ui";
97
97
  import { getSize as getSize3, mx as mx3 } from "@dxos/react-ui-theme";
98
98
 
@@ -252,11 +252,44 @@ var ColumnSettingsForm = ({ column, tableDef, tableDefs, onUpdate, onDelete, onC
252
252
  }))));
253
253
  };
254
254
 
255
+ // packages/ui/react-ui-table/src/hooks/useColumnSizing.ts
256
+ import { useCallback as useCallback2, useEffect, useState as useState3 } from "react";
257
+ import { useOnTransition } from "@dxos/react-ui";
258
+ var useColumnResizing = ({ columns, onColumnResize }) => {
259
+ const [columnsInitialised, setColumnsInitialised] = useState3(false);
260
+ const [columnSizing, setColumnSizing] = useState3({});
261
+ useEffect(() => {
262
+ if (columnsInitialised) {
263
+ return;
264
+ }
265
+ setColumnSizing(columns.filter((column) => !!column.size && column.prop !== void 0).reduce((state, column) => {
266
+ state[column.prop] = column.size;
267
+ return state;
268
+ }, {}));
269
+ setColumnsInitialised(true);
270
+ }, [
271
+ columns,
272
+ setColumnSizing
273
+ ]);
274
+ const [columnSizingInfo, setColumnSizingInfo] = useState3({});
275
+ const notifyColumnResize = useCallback2(() => onColumnResize?.(columnSizing), [
276
+ onColumnResize,
277
+ columnSizing
278
+ ]);
279
+ useOnTransition(columnSizingInfo.isResizingColumn, (v) => typeof v === "string", false, notifyColumnResize);
280
+ return {
281
+ columnSizing,
282
+ columnSizingInfo,
283
+ setColumnSizing,
284
+ setColumnSizingInfo
285
+ };
286
+ };
287
+
255
288
  // packages/ui/react-ui-table/src/hooks/useColumnSorting.ts
256
- import { useState as useState3 } from "react";
289
+ import { useState as useState4 } from "react";
257
290
  var useColumnSorting = (column) => {
258
- const [sortDirection, setSortDirection] = useState3(column.getIsSorted());
259
- const [firstSort, setFirstSort] = useState3();
291
+ const [sortDirection, setSortDirection] = useState4(column.getIsSorted());
292
+ const [firstSort, setFirstSort] = useState4();
260
293
  const canSort = column.getCanSort();
261
294
  const updateColumnSort = (direction) => {
262
295
  setSortDirection(direction);
@@ -299,10 +332,10 @@ var useColumnSorting = (column) => {
299
332
  };
300
333
 
301
334
  // packages/ui/react-ui-table/src/hooks/usePinLastRow.ts
302
- import { useCallback as useCallback2, useEffect, useState as useState4 } from "react";
335
+ import { useCallback as useCallback3, useEffect as useEffect2, useState as useState5 } from "react";
303
336
  var usePinLastRow = (pinLastRow, table, data, getScrollElement) => {
304
- const [pinnedRowKey, setPinnedRowKey] = useState4();
305
- const scrollToBottom = useCallback2(() => {
337
+ const [pinnedRowKey, setPinnedRowKey] = useState5();
338
+ const scrollToBottom = useCallback3(() => {
306
339
  const scrollElement = getScrollElement?.();
307
340
  if (scrollElement) {
308
341
  requestAnimationFrame(() => scrollElement.scrollTo({
@@ -312,20 +345,20 @@ var usePinLastRow = (pinLastRow, table, data, getScrollElement) => {
312
345
  }, [
313
346
  getScrollElement
314
347
  ]);
315
- const pinRow = useCallback2((rowToPin) => {
348
+ const pinRow = useCallback3((rowToPin) => {
316
349
  table.resetRowPinning();
317
350
  rowToPin.pin("bottom");
318
351
  setPinnedRowKey(rowToPin.id);
319
352
  }, [
320
353
  table
321
354
  ]);
322
- const clearRowPinning = useCallback2(() => {
355
+ const clearRowPinning = useCallback3(() => {
323
356
  table.resetRowPinning();
324
357
  setPinnedRowKey(void 0);
325
358
  }, [
326
359
  table
327
360
  ]);
328
- const pinLastRowCallback = useCallback2(() => {
361
+ const pinLastRowCallback = useCallback3(() => {
329
362
  const rows = table.getRowModel().rows;
330
363
  if (rows.length === 0) {
331
364
  return clearRowPinning();
@@ -343,7 +376,7 @@ var usePinLastRow = (pinLastRow, table, data, getScrollElement) => {
343
376
  scrollToBottom,
344
377
  pinnedRowKey
345
378
  ]);
346
- useEffect(() => {
379
+ useEffect2(() => {
347
380
  if (pinLastRow) {
348
381
  pinLastRowCallback();
349
382
  }
@@ -354,22 +387,54 @@ var usePinLastRow = (pinLastRow, table, data, getScrollElement) => {
354
387
  ]);
355
388
  };
356
389
 
390
+ // packages/ui/react-ui-table/src/hooks/useRowSelection.ts
391
+ import { useControllableState } from "@radix-ui/react-use-controllable-state";
392
+ import { useCallback as useCallback4 } from "react";
393
+ var useRowSelection = (props) => {
394
+ const [rowSelection = {}, setRowSelection] = useControllableState({
395
+ prop: props.rowSelection,
396
+ onChange: props.onRowSelectionChange,
397
+ defaultProp: props.defaultRowSelection
398
+ });
399
+ const handleRowSelectionChange = useCallback4((updaterOrValue) => {
400
+ const nextRowSelection = typeof updaterOrValue === "function" ? updaterOrValue(rowSelection) : updaterOrValue;
401
+ if (props.rowsSelectable === "multi") {
402
+ setRowSelection(nextRowSelection);
403
+ } else if (props.rowsSelectable) {
404
+ const nextRowSelectionKey = Object.keys(nextRowSelection).filter((id) => !rowSelection[id])[0];
405
+ setRowSelection(nextRowSelectionKey ? {
406
+ [nextRowSelectionKey]: true
407
+ } : {});
408
+ } else {
409
+ setRowSelection({});
410
+ }
411
+ }, [
412
+ props.rowsSelectable,
413
+ setRowSelection,
414
+ rowSelection
415
+ ]);
416
+ return {
417
+ rowSelection,
418
+ handleRowSelectionChange
419
+ };
420
+ };
421
+
357
422
  // packages/ui/react-ui-table/src/components/ColumnMenu/ColumnMenu.tsx
358
423
  var ColumnMenu = ({ column, ...props }) => {
359
424
  const title = column.label?.length ? column.label : column.id;
360
425
  const header = props.context.header;
361
426
  const { canSort, sortDirection, onSelectSort, onToggleSort, onClearSort } = useColumnSorting(header.column);
362
427
  const columnSettingsAnchorRef = useRef2(null);
363
- const [isColumnSettingsOpen, setIsColumnSettingsOpen] = useState5(false);
428
+ const [isColumnSettingsOpen, setIsColumnSettingsOpen] = useState6(false);
364
429
  const shouldPreventFocusOnClose = useRef2(false);
365
- const onOpenColumnSettings = useCallback3(() => {
430
+ const onOpenColumnSettings = useCallback5(() => {
366
431
  shouldPreventFocusOnClose.current = true;
367
432
  setIsColumnSettingsOpen(true);
368
433
  }, [
369
434
  shouldPreventFocusOnClose,
370
435
  setIsColumnSettingsOpen
371
436
  ]);
372
- const onDropdownCloseAutoFocus = useCallback3((event) => {
437
+ const onDropdownCloseAutoFocus = useCallback5((event) => {
373
438
  if (shouldPreventFocusOnClose.current) {
374
439
  event.preventDefault();
375
440
  shouldPreventFocusOnClose.current = false;
@@ -402,7 +467,7 @@ var ColumnMenu = ({ column, ...props }) => {
402
467
  variant: "ghost"
403
468
  }, /* @__PURE__ */ React3.createElement(CaretDown2, {
404
469
  className: getSize3(4)
405
- })))), /* @__PURE__ */ React3.createElement(DropdownMenu.Content, {
470
+ })))), /* @__PURE__ */ React3.createElement(DropdownMenu.Portal, null, /* @__PURE__ */ React3.createElement(DropdownMenu.Content, {
406
471
  onCloseAutoFocus: onDropdownCloseAutoFocus,
407
472
  sideOffset: 4,
408
473
  collisionPadding: 8
@@ -438,7 +503,7 @@ var ColumnMenu = ({ column, ...props }) => {
438
503
  className: "opacity-50"
439
504
  }, /* @__PURE__ */ React3.createElement(GearSix, {
440
505
  className: mx3(getSize3(4), "rotate-90")
441
- })))), /* @__PURE__ */ React3.createElement(DropdownMenu.Arrow, null)), /* @__PURE__ */ React3.createElement(ColumnSettingsPanel, {
506
+ })))), /* @__PURE__ */ React3.createElement(DropdownMenu.Arrow, null))), /* @__PURE__ */ React3.createElement(ColumnSettingsPanel, {
442
507
  ...props,
443
508
  column,
444
509
  onClose: () => setIsColumnSettingsOpen(false)
@@ -480,12 +545,11 @@ var SortIndicator = ({ direction, onClick }) => {
480
545
  };
481
546
 
482
547
  // packages/ui/react-ui-table/src/components/Table/Table.tsx
483
- import { useControllableState } from "@radix-ui/react-use-controllable-state";
484
548
  import { getCoreRowModel, getSortedRowModel, useReactTable, getGroupedRowModel, getExpandedRowModel } from "@tanstack/react-table";
485
549
  import { useVirtualizer } from "@tanstack/react-virtual";
486
- import React9, { Fragment, useCallback as useCallback4, useEffect as useEffect2, useState as useState6 } from "react";
550
+ import React9, { Fragment, useCallback as useCallback6, useEffect as useEffect3, useState as useState7 } from "react";
487
551
  import { log } from "@dxos/log";
488
- import { useDefaultValue, useOnTransition } from "@dxos/react-ui";
552
+ import { useDefaultValue } from "@dxos/react-ui";
489
553
 
490
554
  // packages/ui/react-ui-table/src/components/Table/TableBody.tsx
491
555
  import { useArrowNavigationGroup } from "@fluentui/react-tabster";
@@ -681,53 +745,27 @@ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/react-ui-table/src/c
681
745
  var Table = (props) => {
682
746
  const { role, onColumnResize, columnVisibility, header = true, rowsSelectable, debug, onDataSelectionChange, getScrollElement, pinLastRow } = props;
683
747
  const columns = useDefaultValue(props.columns, []);
684
- const data = useDefaultValue(props.data, []);
685
- const TableProvider2 = TableProvider;
686
- const [columnsInitialised, setColumnsInitialised] = useState6(false);
687
- const [columnSizing, setColumnSizing] = useState6({});
688
- useEffect2(() => {
689
- if (columnsInitialised) {
690
- return;
691
- }
692
- setColumnSizing(columns.filter((column) => !!column.size && column.prop !== void 0).reduce((state, column) => {
693
- state[column.prop] = column.size;
694
- return state;
695
- }, {}));
696
- setColumnsInitialised(true);
697
- }, [
698
- columns,
699
- setColumnSizing
748
+ const incomingData = useDefaultValue(props.data, []);
749
+ const [data, setData] = useState7([
750
+ ...incomingData
700
751
  ]);
701
- const [columnSizingInfo, setColumnSizingInfo] = useState6({});
702
- const notifyColumnResize = useCallback4(() => onColumnResize?.(columnSizing), [
703
- onColumnResize,
704
- columnSizing
752
+ const forceUpdate = useCallback6(() => setData([
753
+ ...incomingData
754
+ ]), [
755
+ incomingData
705
756
  ]);
706
- useOnTransition(columnSizingInfo.isResizingColumn, (v) => typeof v === "string", false, notifyColumnResize);
707
- const [rowSelection = {}, setRowSelection] = useControllableState({
708
- prop: props.rowSelection,
709
- onChange: props.onRowSelectionChange,
710
- defaultProp: props.defaultRowSelection
711
- });
712
- const handleRowSelectionChange = useCallback4((updaterOrValue) => {
713
- const nextRowSelection = typeof updaterOrValue === "function" ? updaterOrValue(rowSelection) : updaterOrValue;
714
- if (rowsSelectable === "multi") {
715
- setRowSelection(nextRowSelection);
716
- } else if (rowsSelectable) {
717
- const nextRowSelectionKey = Object.keys(nextRowSelection).filter((id) => !rowSelection[id])[0];
718
- setRowSelection(nextRowSelectionKey ? {
719
- [nextRowSelectionKey]: true
720
- } : {});
721
- } else {
722
- setRowSelection({});
723
- }
724
- }, [
725
- rowsSelectable,
726
- setRowSelection,
727
- rowSelection
757
+ useEffect3(() => forceUpdate(), [
758
+ JSON.stringify(incomingData),
759
+ forceUpdate
728
760
  ]);
729
- const [grouping, handleGroupingChange] = useState6(props.grouping ?? []);
730
- useEffect2(() => handleGroupingChange(props.grouping ?? []), [
761
+ const TableProvider2 = TableProvider;
762
+ const { columnSizing, setColumnSizing, columnSizingInfo, setColumnSizingInfo } = useColumnResizing({
763
+ columns,
764
+ onColumnResize
765
+ });
766
+ const { rowSelection, handleRowSelectionChange } = useRowSelection(props);
767
+ const [grouping, handleGroupingChange] = useState7(props.grouping ?? []);
768
+ useEffect3(() => handleGroupingChange(props.grouping ?? []), [
731
769
  handleGroupingChange,
732
770
  props.grouping
733
771
  ]);
@@ -769,7 +807,7 @@ var Table = (props) => {
769
807
  // Debug
770
808
  debugTable: debug
771
809
  });
772
- useEffect2(() => {
810
+ useEffect3(() => {
773
811
  onDataSelectionChange?.(Object.keys(rowSelection).map((id) => table.getRowModel().rowsById[id].original));
774
812
  }, [
775
813
  onDataSelectionChange,
@@ -781,7 +819,7 @@ var Table = (props) => {
781
819
  if (!getScrollElement) {
782
820
  log.warn("Table: getScrollElement is not set. This is required for virtualized tables.", void 0, {
783
821
  F: __dxlog_file,
784
- L: 160,
822
+ L: 120,
785
823
  S: void 0,
786
824
  C: (f, a) => f(...a)
787
825
  });
@@ -900,7 +938,7 @@ import { Check as Check2, ClipboardText, X as X3 } from "@phosphor-icons/react";
900
938
  import { createColumnHelper } from "@tanstack/react-table";
901
939
  import { format } from "date-fns/format";
902
940
  import { formatDistanceToNow } from "date-fns/formatDistanceToNow";
903
- import React10, { useEffect as useEffect3, useRef as useRef3, useState as useState7 } from "react";
941
+ import React10, { useCallback as useCallback7, useEffect as useEffect4, useRef as useRef3, useState as useState8 } from "react";
904
942
  import { log as log2 } from "@dxos/log";
905
943
  import { Input as Input2, Tooltip } from "@dxos/react-ui";
906
944
  import { getSize as getSize4, mx as mx5 } from "@dxos/react-ui-theme";
@@ -922,12 +960,15 @@ var StringBuilderCell = (cellContext) => {
922
960
  const { findPrevFocusable } = useFocusFinders();
923
961
  const { onUpdate, cell } = cellContext.column.columnDef.meta;
924
962
  const initialValue = cellContext.getValue();
925
- const [value, setValue] = useState7(() => {
926
- if (initialValue?.content) {
927
- return initialValue?.content;
928
- }
929
- return initialValue;
930
- });
963
+ const getInitialValue = useCallback7(() => {
964
+ return initialValue?.content ?? initialValue;
965
+ }, [
966
+ initialValue
967
+ ]);
968
+ const [value, setValue] = useState8(getInitialValue);
969
+ useEffect4(() => setValue(getInitialValue()), [
970
+ getInitialValue
971
+ ]);
931
972
  const handleSave = () => {
932
973
  ref.current && document.activeElement === ref.current && findPrevFocusable(ref.current)?.focus();
933
974
  if (value !== initialValue) {
@@ -964,10 +1005,10 @@ var displayNumber = (value, digits) => value || value === 0 ? value.toLocaleStri
964
1005
  var NumberBuilderCell = (cellContext) => {
965
1006
  const { onUpdate, digits } = cellContext.column.columnDef.meta;
966
1007
  const value = cellContext.getValue();
967
- const [text, setText] = useState7(displayNumber(value, digits));
1008
+ const [text, setText] = useState8(displayNumber(value, digits));
968
1009
  const { findPrevFocusable } = useFocusFinders();
969
1010
  const ref = useRef3(null);
970
- useEffect3(() => {
1011
+ useEffect4(() => {
971
1012
  (!document.activeElement || document.activeElement !== ref.current) && setText(displayNumber(value, digits));
972
1013
  }, [
973
1014
  digits,
@@ -1146,7 +1187,7 @@ var ColumnBuilder = class {
1146
1187
  value
1147
1188
  }, {
1148
1189
  F: __dxlog_file2,
1149
- L: 374,
1190
+ L: 376,
1150
1191
  S: this,
1151
1192
  C: (f, a) => f(...a)
1152
1193
  });
@@ -1473,7 +1514,9 @@ export {
1473
1514
  theadTh,
1474
1515
  theadTr,
1475
1516
  translations_default as translations,
1517
+ useColumnResizing,
1476
1518
  useColumnSorting,
1477
- usePinLastRow
1519
+ usePinLastRow,
1520
+ useRowSelection
1478
1521
  };
1479
1522
  //# sourceMappingURL=index.mjs.map