@dxos/react-ui-table 0.5.1-next.2ad6c47 → 0.5.1-next.5a770e3

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.
@@ -91,8 +91,8 @@ var CellCombobox = ({ model, value, onValueChange }) => {
91
91
  };
92
92
 
93
93
  // packages/ui/react-ui-table/src/components/ColumnMenu/ColumnMenu.tsx
94
- import { X as X2, GearSix, CaretDown as CaretDown2, ArrowDown, ArrowUp } from "@phosphor-icons/react";
95
- import React3, { useRef as useRef2, useState as useState6, useCallback as useCallback5 } from "react";
94
+ import { X as X2, GearSix, CaretDown as CaretDown2, ArrowDown, ArrowUp, ArrowRight, ArrowLeft } from "@phosphor-icons/react";
95
+ import React4, { 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
 
@@ -106,7 +106,7 @@ var Section = ({ children, className }) => /* @__PURE__ */ React2.createElement(
106
106
  role: "none",
107
107
  className: mx2("p-2", className)
108
108
  }, children);
109
- var ColumnSettingsForm = ({ column, tableDef, tableDefs, onUpdate, onDelete, onClose }) => {
109
+ var ColumnSettingsForm = ({ column, tableDef, tablesToReference, onUpdate, onDelete, onClose }) => {
110
110
  const [formState, setFormState] = useState2({
111
111
  prop: column.id,
112
112
  refTable: column.refTable,
@@ -144,6 +144,10 @@ var ColumnSettingsForm = ({ column, tableDef, tableDefs, onUpdate, onDelete, onC
144
144
  });
145
145
  onClose?.();
146
146
  };
147
+ const handleDelete = () => {
148
+ onDelete?.(column.id);
149
+ onClose?.();
150
+ };
147
151
  return /* @__PURE__ */ React2.createElement("div", {
148
152
  className: "flex flex-col gap-1"
149
153
  }, /* @__PURE__ */ React2.createElement(Section, null, /* @__PURE__ */ React2.createElement(Input.Root, null, /* @__PURE__ */ React2.createElement(Input.Label, {
@@ -204,7 +208,7 @@ var ColumnSettingsForm = ({ column, tableDef, tableDefs, onUpdate, onDelete, onC
204
208
  }, /* @__PURE__ */ React2.createElement(Select.TriggerButton, {
205
209
  placeholder: "Table",
206
210
  classNames: "is-full"
207
- }), /* @__PURE__ */ React2.createElement(Select.Portal, null, /* @__PURE__ */ React2.createElement(Select.Content, null, /* @__PURE__ */ React2.createElement(Select.Viewport, null, tableDefs.filter((t2) => t2.id !== tableDef.id).map(({ id, name }) => /* @__PURE__ */ React2.createElement(Select.Option, {
211
+ }), /* @__PURE__ */ React2.createElement(Select.Portal, null, /* @__PURE__ */ React2.createElement(Select.Content, null, /* @__PURE__ */ React2.createElement(Select.Viewport, null, tablesToReference.filter((t2) => t2.id !== tableDef.id).map(({ id, name }) => /* @__PURE__ */ React2.createElement(Select.Option, {
208
212
  key: id,
209
213
  value: id
210
214
  }, name ?? id))))))), formState.refTable && /* @__PURE__ */ React2.createElement(Input.Root, null, /* @__PURE__ */ React2.createElement(Input.Label, {
@@ -218,7 +222,7 @@ var ColumnSettingsForm = ({ column, tableDef, tableDefs, onUpdate, onDelete, onC
218
222
  }, /* @__PURE__ */ React2.createElement(Select.TriggerButton, {
219
223
  placeholder: "Property",
220
224
  classNames: "is-full"
221
- }), /* @__PURE__ */ React2.createElement(Select.Portal, null, /* @__PURE__ */ React2.createElement(Select.Content, null, /* @__PURE__ */ React2.createElement(Select.Viewport, null, tableDefs.find((tableDef2) => tableDef2.id === formState.refTable)?.columns.map(({ id, label }) => /* @__PURE__ */ React2.createElement(Select.Option, {
225
+ }), /* @__PURE__ */ React2.createElement(Select.Portal, null, /* @__PURE__ */ React2.createElement(Select.Content, null, /* @__PURE__ */ React2.createElement(Select.Viewport, null, tablesToReference.find((tableDef2) => tableDef2.id === formState.refTable)?.columns.map(({ id, label }) => /* @__PURE__ */ React2.createElement(Select.Option, {
222
226
  key: id,
223
227
  value: id
224
228
  }, label ?? id)))))))), /* @__PURE__ */ React2.createElement(Separator, {
@@ -244,7 +248,7 @@ var ColumnSettingsForm = ({ column, tableDef, tableDefs, onUpdate, onDelete, onC
244
248
  className: getSize2(5)
245
249
  })), /* @__PURE__ */ React2.createElement(Button, {
246
250
  classNames: "is-full flex gap-2",
247
- onClick: () => onDelete?.(column.id)
251
+ onClick: handleDelete
248
252
  }, /* @__PURE__ */ React2.createElement("span", null, t("delete label")), /* @__PURE__ */ React2.createElement("div", {
249
253
  className: "grow"
250
254
  }), /* @__PURE__ */ React2.createElement(Trash, {
@@ -287,40 +291,66 @@ var useColumnResizing = ({ columns, onColumnResize }) => {
287
291
 
288
292
  // packages/ui/react-ui-table/src/hooks/useColumnSorting.ts
289
293
  import { useState as useState4 } from "react";
290
- var useColumnSorting = (column) => {
291
- const [sortDirection, setSortDirection] = useState4(column.getIsSorted());
292
- const [firstSort, setFirstSort] = useState4();
294
+
295
+ // packages/ui/react-ui-table/src/components/Table/TableContext.tsx
296
+ import React3, { useContext, createContext } from "react";
297
+ var TableContext = /* @__PURE__ */ createContext({});
298
+ var TableProvider = ({ children, ...value }) => {
299
+ return /* @__PURE__ */ React3.createElement(TableContext.Provider, {
300
+ value
301
+ }, children);
302
+ };
303
+ var useTableContext = () => {
304
+ const context = useContext(TableContext);
305
+ return context;
306
+ };
307
+
308
+ // packages/ui/react-ui-table/src/hooks/useColumnSorting.ts
309
+ var useColumnSorting = () => {
310
+ const [columnSorting, setColumnSorting] = useState4([]);
311
+ const sortColumn = (columnId, sortDirection) => {
312
+ setColumnSorting([
313
+ {
314
+ id: columnId,
315
+ desc: sortDirection === "desc"
316
+ }
317
+ ]);
318
+ };
319
+ const clearColumnSorting = () => {
320
+ setColumnSorting([]);
321
+ };
322
+ return {
323
+ sorting: columnSorting,
324
+ sortColumn,
325
+ clearColumnSorting
326
+ };
327
+ };
328
+ var useSortColumn = (column) => {
329
+ const { sorting, sortColumn, clearColumnSorting } = useTableContext();
330
+ const [initialSortDirection, setInitialSortDirection] = useState4();
331
+ const columnSort = sorting.find((s) => s.id === column.id);
332
+ const sortDirection = columnSort ? columnSort.desc ? "desc" : "asc" : void 0;
293
333
  const canSort = column.getCanSort();
294
334
  const updateColumnSort = (direction) => {
295
- setSortDirection(direction);
296
- switch (direction) {
297
- case false:
298
- column.clearSorting();
299
- break;
300
- case "asc":
301
- column.toggleSorting(false);
302
- break;
303
- case "desc":
304
- column.toggleSorting(true);
305
- break;
306
- }
335
+ sortColumn(column.id, direction);
307
336
  };
308
337
  const onToggleSort = () => {
309
- if (sortDirection === false) {
338
+ if (sortDirection === void 0) {
310
339
  return;
311
340
  }
312
- if (sortDirection === firstSort) {
313
- updateColumnSort(firstSort === "asc" ? "desc" : "asc");
341
+ if (sortDirection === initialSortDirection) {
342
+ updateColumnSort(initialSortDirection === "asc" ? "desc" : "asc");
314
343
  } else {
315
- updateColumnSort(false);
344
+ clearColumnSorting();
316
345
  }
317
346
  };
318
347
  const onSelectSort = (direction) => {
319
- setFirstSort(direction);
348
+ clearColumnSorting();
349
+ setInitialSortDirection(direction);
320
350
  updateColumnSort(direction);
321
351
  };
322
352
  const onClearSort = () => {
323
- updateColumnSort(false);
353
+ clearColumnSorting();
324
354
  };
325
355
  return {
326
356
  canSort,
@@ -420,10 +450,10 @@ var useRowSelection = (props) => {
420
450
  };
421
451
 
422
452
  // packages/ui/react-ui-table/src/components/ColumnMenu/ColumnMenu.tsx
423
- var ColumnMenu = ({ column, ...props }) => {
453
+ var ColumnMenu = ({ column, columnOrderable, columnPosition, onColumnReorder, ...props }) => {
424
454
  const title = column.label?.length ? column.label : column.id;
425
455
  const header = props.context.header;
426
- const { canSort, sortDirection, onSelectSort, onToggleSort, onClearSort } = useColumnSorting(header.column);
456
+ const { canSort, sortDirection, onSelectSort, onToggleSort, onClearSort } = useSortColumn(header.column);
427
457
  const columnSettingsAnchorRef = useRef2(null);
428
458
  const [isColumnSettingsOpen, setIsColumnSettingsOpen] = useState6(false);
429
459
  const shouldPreventFocusOnClose = useRef2(false);
@@ -442,103 +472,111 @@ var ColumnMenu = ({ column, ...props }) => {
442
472
  }, [
443
473
  shouldPreventFocusOnClose
444
474
  ]);
445
- return /* @__PURE__ */ React3.createElement("div", {
475
+ return /* @__PURE__ */ React4.createElement("div", {
446
476
  className: "flex items-center justify-center justify-between"
447
- }, /* @__PURE__ */ React3.createElement("div", {
477
+ }, /* @__PURE__ */ React4.createElement("div", {
448
478
  className: "flex items-center gap-1 truncate",
449
479
  title
450
- }, /* @__PURE__ */ React3.createElement(SortIndicator, {
480
+ }, /* @__PURE__ */ React4.createElement(SortIndicator, {
451
481
  direction: sortDirection,
452
482
  onClick: onToggleSort
453
- }), /* @__PURE__ */ React3.createElement("div", {
483
+ }), /* @__PURE__ */ React4.createElement("div", {
454
484
  className: "truncate"
455
- }, title)), /* @__PURE__ */ React3.createElement(DropdownMenu.Root, {
485
+ }, title)), /* @__PURE__ */ React4.createElement(DropdownMenu.Root, {
456
486
  modal: false
457
- }, /* @__PURE__ */ React3.createElement(Popover2.Root, {
487
+ }, /* @__PURE__ */ React4.createElement(Popover2.Root, {
458
488
  open: isColumnSettingsOpen,
459
489
  onOpenChange: setIsColumnSettingsOpen,
460
490
  modal: false
461
- }, /* @__PURE__ */ React3.createElement(DropdownMenu.Trigger, {
491
+ }, /* @__PURE__ */ React4.createElement(DropdownMenu.Trigger, {
462
492
  ref: columnSettingsAnchorRef,
463
493
  asChild: true
464
- }, /* @__PURE__ */ React3.createElement(Popover2.Anchor, {
494
+ }, /* @__PURE__ */ React4.createElement(Popover2.Anchor, {
465
495
  asChild: true
466
- }, /* @__PURE__ */ React3.createElement(Button2, {
496
+ }, /* @__PURE__ */ React4.createElement(Button2, {
467
497
  variant: "ghost"
468
- }, /* @__PURE__ */ React3.createElement(CaretDown2, {
498
+ }, /* @__PURE__ */ React4.createElement(CaretDown2, {
469
499
  className: getSize3(4)
470
- })))), /* @__PURE__ */ React3.createElement(DropdownMenu.Portal, null, /* @__PURE__ */ React3.createElement(DropdownMenu.Content, {
500
+ })))), /* @__PURE__ */ React4.createElement(DropdownMenu.Portal, null, /* @__PURE__ */ React4.createElement(DropdownMenu.Content, {
471
501
  onCloseAutoFocus: onDropdownCloseAutoFocus,
472
502
  sideOffset: 4,
473
503
  collisionPadding: 8
474
- }, /* @__PURE__ */ React3.createElement(DropdownMenu.Viewport, null, canSort && /* @__PURE__ */ React3.createElement(React3.Fragment, null, /* @__PURE__ */ React3.createElement(DropdownMenu.Item, {
504
+ }, /* @__PURE__ */ React4.createElement(DropdownMenu.Viewport, null, canSort && /* @__PURE__ */ React4.createElement(React4.Fragment, null, /* @__PURE__ */ React4.createElement(DropdownMenu.Item, {
475
505
  onClick: () => onSelectSort("asc")
476
- }, /* @__PURE__ */ React3.createElement("span", {
506
+ }, /* @__PURE__ */ React4.createElement("span", {
477
507
  className: "grow"
478
- }, "Sort ascending"), /* @__PURE__ */ React3.createElement("span", {
508
+ }, "Sort ascending"), /* @__PURE__ */ React4.createElement("span", {
479
509
  className: "opacity-50"
480
- }, /* @__PURE__ */ React3.createElement(ArrowUp, {
510
+ }, /* @__PURE__ */ React4.createElement(ArrowUp, {
481
511
  className: getSize3(4)
482
- }))), /* @__PURE__ */ React3.createElement(DropdownMenu.Item, {
512
+ }))), /* @__PURE__ */ React4.createElement(DropdownMenu.Item, {
483
513
  onClick: () => onSelectSort("desc")
484
- }, /* @__PURE__ */ React3.createElement("span", {
514
+ }, /* @__PURE__ */ React4.createElement("span", {
485
515
  className: "grow"
486
- }, "Sort descending"), /* @__PURE__ */ React3.createElement("span", {
516
+ }, "Sort descending"), /* @__PURE__ */ React4.createElement("span", {
487
517
  className: "opacity-50"
488
- }, /* @__PURE__ */ React3.createElement(ArrowDown, {
518
+ }, /* @__PURE__ */ React4.createElement(ArrowDown, {
489
519
  className: getSize3(4)
490
- }))), sortDirection !== false && /* @__PURE__ */ React3.createElement(DropdownMenu.Item, {
520
+ }))), sortDirection && /* @__PURE__ */ React4.createElement(DropdownMenu.Item, {
491
521
  onClick: onClearSort
492
- }, /* @__PURE__ */ React3.createElement("span", {
522
+ }, /* @__PURE__ */ React4.createElement("span", {
493
523
  className: "grow"
494
- }, "Clear sort"), /* @__PURE__ */ React3.createElement("span", {
524
+ }, "Clear sort"), /* @__PURE__ */ React4.createElement("span", {
495
525
  className: "opacity-50"
496
- }, /* @__PURE__ */ React3.createElement(X2, {
526
+ }, /* @__PURE__ */ React4.createElement(X2, {
497
527
  className: getSize3(4)
498
- })))), /* @__PURE__ */ React3.createElement(DropdownMenu.Item, {
528
+ })))), columnOrderable && /* @__PURE__ */ React4.createElement(React4.Fragment, null, columnPosition !== "start" && /* @__PURE__ */ React4.createElement(DropdownMenu.Item, {
529
+ onClick: () => onColumnReorder(header.column.id, "left")
530
+ }, /* @__PURE__ */ React4.createElement("span", {
531
+ className: "grow"
532
+ }, "Move left"), /* @__PURE__ */ React4.createElement("span", {
533
+ className: "opacity-50"
534
+ }, /* @__PURE__ */ React4.createElement(ArrowLeft, {
535
+ className: mx3(getSize3(4))
536
+ }))), columnPosition !== "end" && /* @__PURE__ */ React4.createElement(DropdownMenu.Item, {
537
+ onClick: () => onColumnReorder(header.column.id, "right")
538
+ }, /* @__PURE__ */ React4.createElement("span", {
539
+ className: "grow"
540
+ }, "Move right"), /* @__PURE__ */ React4.createElement("span", {
541
+ className: "opacity-50"
542
+ }, /* @__PURE__ */ React4.createElement(ArrowRight, {
543
+ className: mx3(getSize3(4))
544
+ })))), /* @__PURE__ */ React4.createElement(DropdownMenu.Item, {
499
545
  onClick: onOpenColumnSettings
500
- }, /* @__PURE__ */ React3.createElement("span", {
546
+ }, /* @__PURE__ */ React4.createElement("span", {
501
547
  className: "grow"
502
- }, "Column settings"), /* @__PURE__ */ React3.createElement("span", {
548
+ }, "Column settings"), /* @__PURE__ */ React4.createElement("span", {
503
549
  className: "opacity-50"
504
- }, /* @__PURE__ */ React3.createElement(GearSix, {
550
+ }, /* @__PURE__ */ React4.createElement(GearSix, {
505
551
  className: mx3(getSize3(4), "rotate-90")
506
- })))), /* @__PURE__ */ React3.createElement(DropdownMenu.Arrow, null))), /* @__PURE__ */ React3.createElement(ColumnSettingsPanel, {
552
+ })))), /* @__PURE__ */ React4.createElement(DropdownMenu.Arrow, null))), /* @__PURE__ */ React4.createElement(Popover2.Portal, null, /* @__PURE__ */ React4.createElement(Popover2.Content, null, /* @__PURE__ */ React4.createElement(Popover2.Viewport, {
553
+ classNames: "w-60"
554
+ }, /* @__PURE__ */ React4.createElement(DensityProvider, {
555
+ density: "fine"
556
+ }, /* @__PURE__ */ React4.createElement(ColumnSettingsForm, {
507
557
  ...props,
508
558
  column,
509
559
  onClose: () => setIsColumnSettingsOpen(false)
510
- }))));
560
+ }))), /* @__PURE__ */ React4.createElement(Popover2.Arrow, null))))));
511
561
  };
512
- var ColumnSettingsPanel = ({ tableDefs, tableDef, column, onUpdate, onDelete, onClose }) => /* @__PURE__ */ React3.createElement(Popover2.Portal, null, /* @__PURE__ */ React3.createElement(Popover2.Content, null, /* @__PURE__ */ React3.createElement(Popover2.Viewport, {
513
- classNames: "w-60"
514
- }, /* @__PURE__ */ React3.createElement(DensityProvider, {
515
- density: "fine"
516
- }, /* @__PURE__ */ React3.createElement(ColumnSettingsForm, {
517
- column,
518
- tableDefs,
519
- tableDef,
520
- onUpdate,
521
- onDelete,
522
- onClose
523
- }))), /* @__PURE__ */ React3.createElement(Popover2.Arrow, null)));
524
562
  var SortIndicator = ({ direction, onClick }) => {
525
- if (direction === false) {
563
+ if (direction === void 0) {
526
564
  return null;
527
565
  }
528
566
  let icon;
529
567
  switch (direction) {
530
568
  case "asc":
531
- icon = /* @__PURE__ */ React3.createElement(ArrowUp, {
569
+ icon = /* @__PURE__ */ React4.createElement(ArrowUp, {
532
570
  className: getSize3(3)
533
571
  });
534
572
  break;
535
573
  case "desc":
536
- icon = /* @__PURE__ */ React3.createElement(ArrowDown, {
574
+ icon = /* @__PURE__ */ React4.createElement(ArrowDown, {
537
575
  className: getSize3(3)
538
576
  });
539
577
  break;
540
578
  }
541
- return /* @__PURE__ */ React3.createElement("div", {
579
+ return /* @__PURE__ */ React4.createElement("div", {
542
580
  onClick,
543
581
  className: "flex items-center cursor-pointer"
544
582
  }, icon);
@@ -556,19 +594,6 @@ import { useArrowNavigationGroup } from "@fluentui/react-tabster";
556
594
  import { flexRender } from "@tanstack/react-table";
557
595
  import React6, { useContext as useContext2 } from "react";
558
596
 
559
- // packages/ui/react-ui-table/src/components/Table/TableContext.tsx
560
- import React4, { useContext, createContext } from "react";
561
- var TableContext = /* @__PURE__ */ createContext({});
562
- var TableProvider = ({ children, ...value }) => {
563
- return /* @__PURE__ */ React4.createElement(TableContext.Provider, {
564
- value
565
- }, children);
566
- };
567
- var useTableContext = () => {
568
- const context = useContext(TableContext);
569
- return context;
570
- };
571
-
572
597
  // packages/ui/react-ui-table/src/components/Table/TableRootContext.ts
573
598
  import { useVirtualizer } from "@tanstack/react-virtual";
574
599
  import { createContext as createContext2, useRef as useRef3, useState as useState7 } from "react";
@@ -819,6 +844,7 @@ var TablePrimitive = (props) => {
819
844
  handleGroupingChange,
820
845
  props.grouping
821
846
  ]);
847
+ const columnSorting = useColumnSorting();
822
848
  const table = useReactTable({
823
849
  // Data
824
850
  meta: {},
@@ -834,6 +860,7 @@ var TablePrimitive = (props) => {
834
860
  columnVisibility,
835
861
  columnSizing,
836
862
  columnSizingInfo,
863
+ sorting: columnSorting.sorting,
837
864
  rowSelection,
838
865
  grouping
839
866
  },
@@ -868,6 +895,7 @@ var TablePrimitive = (props) => {
868
895
  const expand = false;
869
896
  return /* @__PURE__ */ React9.createElement(TableProvider2, {
870
897
  ...props,
898
+ ...columnSorting,
871
899
  table,
872
900
  header,
873
901
  expand,
@@ -962,8 +990,8 @@ var GroupedTableContent = () => {
962
990
  };
963
991
  var Table = {
964
992
  Root: TableRoot,
965
- Table: TablePrimitive,
966
- Viewport: TableViewport
993
+ Viewport: TableViewport,
994
+ Main: TablePrimitive
967
995
  };
968
996
 
969
997
  // packages/ui/react-ui-table/src/helpers.tsx
@@ -1522,7 +1550,6 @@ export {
1522
1550
  ColumnBuilder,
1523
1551
  ColumnMenu,
1524
1552
  ColumnSettingsForm,
1525
- ColumnSettingsPanel,
1526
1553
  MemoizedVirtualisedTableContent,
1527
1554
  SortIndicator,
1528
1555
  Table,
@@ -1554,6 +1581,7 @@ export {
1554
1581
  useColumnSorting,
1555
1582
  usePinLastRow,
1556
1583
  useRowSelection,
1584
+ useSortColumn,
1557
1585
  useTableRootContext
1558
1586
  };
1559
1587
  //# sourceMappingURL=index.mjs.map