@dxos/react-ui-table 0.5.1-main.ff7d242 → 0.5.1-next.0ef769d

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,
@@ -208,7 +208,7 @@ var ColumnSettingsForm = ({ column, tableDef, tableDefs, onUpdate, onDelete, onC
208
208
  }, /* @__PURE__ */ React2.createElement(Select.TriggerButton, {
209
209
  placeholder: "Table",
210
210
  classNames: "is-full"
211
- }), /* @__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, {
212
212
  key: id,
213
213
  value: id
214
214
  }, name ?? id))))))), formState.refTable && /* @__PURE__ */ React2.createElement(Input.Root, null, /* @__PURE__ */ React2.createElement(Input.Label, {
@@ -222,7 +222,7 @@ var ColumnSettingsForm = ({ column, tableDef, tableDefs, onUpdate, onDelete, onC
222
222
  }, /* @__PURE__ */ React2.createElement(Select.TriggerButton, {
223
223
  placeholder: "Property",
224
224
  classNames: "is-full"
225
- }), /* @__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, {
226
226
  key: id,
227
227
  value: id
228
228
  }, label ?? id)))))))), /* @__PURE__ */ React2.createElement(Separator, {
@@ -291,40 +291,66 @@ var useColumnResizing = ({ columns, onColumnResize }) => {
291
291
 
292
292
  // packages/ui/react-ui-table/src/hooks/useColumnSorting.ts
293
293
  import { useState as useState4 } from "react";
294
- var useColumnSorting = (column) => {
295
- const [sortDirection, setSortDirection] = useState4(column.getIsSorted());
296
- 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;
297
333
  const canSort = column.getCanSort();
298
334
  const updateColumnSort = (direction) => {
299
- setSortDirection(direction);
300
- switch (direction) {
301
- case false:
302
- column.clearSorting();
303
- break;
304
- case "asc":
305
- column.toggleSorting(false);
306
- break;
307
- case "desc":
308
- column.toggleSorting(true);
309
- break;
310
- }
335
+ sortColumn(column.id, direction);
311
336
  };
312
337
  const onToggleSort = () => {
313
- if (sortDirection === false) {
338
+ if (sortDirection === void 0) {
314
339
  return;
315
340
  }
316
- if (sortDirection === firstSort) {
317
- updateColumnSort(firstSort === "asc" ? "desc" : "asc");
341
+ if (sortDirection === initialSortDirection) {
342
+ updateColumnSort(initialSortDirection === "asc" ? "desc" : "asc");
318
343
  } else {
319
- updateColumnSort(false);
344
+ clearColumnSorting();
320
345
  }
321
346
  };
322
347
  const onSelectSort = (direction) => {
323
- setFirstSort(direction);
348
+ clearColumnSorting();
349
+ setInitialSortDirection(direction);
324
350
  updateColumnSort(direction);
325
351
  };
326
352
  const onClearSort = () => {
327
- updateColumnSort(false);
353
+ clearColumnSorting();
328
354
  };
329
355
  return {
330
356
  canSort,
@@ -424,10 +450,10 @@ var useRowSelection = (props) => {
424
450
  };
425
451
 
426
452
  // packages/ui/react-ui-table/src/components/ColumnMenu/ColumnMenu.tsx
427
- var ColumnMenu = ({ column, ...props }) => {
453
+ var ColumnMenu = ({ column, columnOrderable, columnPosition, onColumnReorder, ...props }) => {
428
454
  const title = column.label?.length ? column.label : column.id;
429
455
  const header = props.context.header;
430
- const { canSort, sortDirection, onSelectSort, onToggleSort, onClearSort } = useColumnSorting(header.column);
456
+ const { canSort, sortDirection, onSelectSort, onToggleSort, onClearSort } = useSortColumn(header.column);
431
457
  const columnSettingsAnchorRef = useRef2(null);
432
458
  const [isColumnSettingsOpen, setIsColumnSettingsOpen] = useState6(false);
433
459
  const shouldPreventFocusOnClose = useRef2(false);
@@ -446,95 +472,111 @@ var ColumnMenu = ({ column, ...props }) => {
446
472
  }, [
447
473
  shouldPreventFocusOnClose
448
474
  ]);
449
- return /* @__PURE__ */ React3.createElement("div", {
475
+ return /* @__PURE__ */ React4.createElement("div", {
450
476
  className: "flex items-center justify-center justify-between"
451
- }, /* @__PURE__ */ React3.createElement("div", {
477
+ }, /* @__PURE__ */ React4.createElement("div", {
452
478
  className: "flex items-center gap-1 truncate",
453
479
  title
454
- }, /* @__PURE__ */ React3.createElement(SortIndicator, {
480
+ }, /* @__PURE__ */ React4.createElement(SortIndicator, {
455
481
  direction: sortDirection,
456
482
  onClick: onToggleSort
457
- }), /* @__PURE__ */ React3.createElement("div", {
483
+ }), /* @__PURE__ */ React4.createElement("div", {
458
484
  className: "truncate"
459
- }, title)), /* @__PURE__ */ React3.createElement(DropdownMenu.Root, {
485
+ }, title)), /* @__PURE__ */ React4.createElement(DropdownMenu.Root, {
460
486
  modal: false
461
- }, /* @__PURE__ */ React3.createElement(Popover2.Root, {
487
+ }, /* @__PURE__ */ React4.createElement(Popover2.Root, {
462
488
  open: isColumnSettingsOpen,
463
489
  onOpenChange: setIsColumnSettingsOpen,
464
490
  modal: false
465
- }, /* @__PURE__ */ React3.createElement(DropdownMenu.Trigger, {
491
+ }, /* @__PURE__ */ React4.createElement(DropdownMenu.Trigger, {
466
492
  ref: columnSettingsAnchorRef,
467
493
  asChild: true
468
- }, /* @__PURE__ */ React3.createElement(Popover2.Anchor, {
494
+ }, /* @__PURE__ */ React4.createElement(Popover2.Anchor, {
469
495
  asChild: true
470
- }, /* @__PURE__ */ React3.createElement(Button2, {
496
+ }, /* @__PURE__ */ React4.createElement(Button2, {
471
497
  variant: "ghost"
472
- }, /* @__PURE__ */ React3.createElement(CaretDown2, {
498
+ }, /* @__PURE__ */ React4.createElement(CaretDown2, {
473
499
  className: getSize3(4)
474
- })))), /* @__PURE__ */ React3.createElement(DropdownMenu.Portal, null, /* @__PURE__ */ React3.createElement(DropdownMenu.Content, {
500
+ })))), /* @__PURE__ */ React4.createElement(DropdownMenu.Portal, null, /* @__PURE__ */ React4.createElement(DropdownMenu.Content, {
475
501
  onCloseAutoFocus: onDropdownCloseAutoFocus,
476
502
  sideOffset: 4,
477
503
  collisionPadding: 8
478
- }, /* @__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, {
479
505
  onClick: () => onSelectSort("asc")
480
- }, /* @__PURE__ */ React3.createElement("span", {
506
+ }, /* @__PURE__ */ React4.createElement("span", {
481
507
  className: "grow"
482
- }, "Sort ascending"), /* @__PURE__ */ React3.createElement("span", {
508
+ }, "Sort ascending"), /* @__PURE__ */ React4.createElement("span", {
483
509
  className: "opacity-50"
484
- }, /* @__PURE__ */ React3.createElement(ArrowUp, {
510
+ }, /* @__PURE__ */ React4.createElement(ArrowUp, {
485
511
  className: getSize3(4)
486
- }))), /* @__PURE__ */ React3.createElement(DropdownMenu.Item, {
512
+ }))), /* @__PURE__ */ React4.createElement(DropdownMenu.Item, {
487
513
  onClick: () => onSelectSort("desc")
488
- }, /* @__PURE__ */ React3.createElement("span", {
514
+ }, /* @__PURE__ */ React4.createElement("span", {
489
515
  className: "grow"
490
- }, "Sort descending"), /* @__PURE__ */ React3.createElement("span", {
516
+ }, "Sort descending"), /* @__PURE__ */ React4.createElement("span", {
491
517
  className: "opacity-50"
492
- }, /* @__PURE__ */ React3.createElement(ArrowDown, {
518
+ }, /* @__PURE__ */ React4.createElement(ArrowDown, {
493
519
  className: getSize3(4)
494
- }))), sortDirection !== false && /* @__PURE__ */ React3.createElement(DropdownMenu.Item, {
520
+ }))), sortDirection && /* @__PURE__ */ React4.createElement(DropdownMenu.Item, {
495
521
  onClick: onClearSort
496
- }, /* @__PURE__ */ React3.createElement("span", {
522
+ }, /* @__PURE__ */ React4.createElement("span", {
497
523
  className: "grow"
498
- }, "Clear sort"), /* @__PURE__ */ React3.createElement("span", {
524
+ }, "Clear sort"), /* @__PURE__ */ React4.createElement("span", {
499
525
  className: "opacity-50"
500
- }, /* @__PURE__ */ React3.createElement(X2, {
526
+ }, /* @__PURE__ */ React4.createElement(X2, {
501
527
  className: getSize3(4)
502
- })))), /* @__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, {
503
545
  onClick: onOpenColumnSettings
504
- }, /* @__PURE__ */ React3.createElement("span", {
546
+ }, /* @__PURE__ */ React4.createElement("span", {
505
547
  className: "grow"
506
- }, "Column settings"), /* @__PURE__ */ React3.createElement("span", {
548
+ }, "Column settings"), /* @__PURE__ */ React4.createElement("span", {
507
549
  className: "opacity-50"
508
- }, /* @__PURE__ */ React3.createElement(GearSix, {
550
+ }, /* @__PURE__ */ React4.createElement(GearSix, {
509
551
  className: mx3(getSize3(4), "rotate-90")
510
- })))), /* @__PURE__ */ React3.createElement(DropdownMenu.Arrow, null))), /* @__PURE__ */ React3.createElement(Popover2.Portal, null, /* @__PURE__ */ React3.createElement(Popover2.Content, null, /* @__PURE__ */ React3.createElement(Popover2.Viewport, {
552
+ })))), /* @__PURE__ */ React4.createElement(DropdownMenu.Arrow, null))), /* @__PURE__ */ React4.createElement(Popover2.Portal, null, /* @__PURE__ */ React4.createElement(Popover2.Content, null, /* @__PURE__ */ React4.createElement(Popover2.Viewport, {
511
553
  classNames: "w-60"
512
- }, /* @__PURE__ */ React3.createElement(DensityProvider, {
554
+ }, /* @__PURE__ */ React4.createElement(DensityProvider, {
513
555
  density: "fine"
514
- }, /* @__PURE__ */ React3.createElement(ColumnSettingsForm, {
556
+ }, /* @__PURE__ */ React4.createElement(ColumnSettingsForm, {
515
557
  ...props,
516
558
  column,
517
559
  onClose: () => setIsColumnSettingsOpen(false)
518
- }))), /* @__PURE__ */ React3.createElement(Popover2.Arrow, null))))));
560
+ }))), /* @__PURE__ */ React4.createElement(Popover2.Arrow, null))))));
519
561
  };
520
562
  var SortIndicator = ({ direction, onClick }) => {
521
- if (direction === false) {
563
+ if (direction === void 0) {
522
564
  return null;
523
565
  }
524
566
  let icon;
525
567
  switch (direction) {
526
568
  case "asc":
527
- icon = /* @__PURE__ */ React3.createElement(ArrowUp, {
569
+ icon = /* @__PURE__ */ React4.createElement(ArrowUp, {
528
570
  className: getSize3(3)
529
571
  });
530
572
  break;
531
573
  case "desc":
532
- icon = /* @__PURE__ */ React3.createElement(ArrowDown, {
574
+ icon = /* @__PURE__ */ React4.createElement(ArrowDown, {
533
575
  className: getSize3(3)
534
576
  });
535
577
  break;
536
578
  }
537
- return /* @__PURE__ */ React3.createElement("div", {
579
+ return /* @__PURE__ */ React4.createElement("div", {
538
580
  onClick,
539
581
  className: "flex items-center cursor-pointer"
540
582
  }, icon);
@@ -552,19 +594,6 @@ import { useArrowNavigationGroup } from "@fluentui/react-tabster";
552
594
  import { flexRender } from "@tanstack/react-table";
553
595
  import React6, { useContext as useContext2 } from "react";
554
596
 
555
- // packages/ui/react-ui-table/src/components/Table/TableContext.tsx
556
- import React4, { useContext, createContext } from "react";
557
- var TableContext = /* @__PURE__ */ createContext({});
558
- var TableProvider = ({ children, ...value }) => {
559
- return /* @__PURE__ */ React4.createElement(TableContext.Provider, {
560
- value
561
- }, children);
562
- };
563
- var useTableContext = () => {
564
- const context = useContext(TableContext);
565
- return context;
566
- };
567
-
568
597
  // packages/ui/react-ui-table/src/components/Table/TableRootContext.ts
569
598
  import { useVirtualizer } from "@tanstack/react-virtual";
570
599
  import { createContext as createContext2, useRef as useRef3, useState as useState7 } from "react";
@@ -815,6 +844,7 @@ var TablePrimitive = (props) => {
815
844
  handleGroupingChange,
816
845
  props.grouping
817
846
  ]);
847
+ const columnSorting = useColumnSorting();
818
848
  const table = useReactTable({
819
849
  // Data
820
850
  meta: {},
@@ -830,6 +860,7 @@ var TablePrimitive = (props) => {
830
860
  columnVisibility,
831
861
  columnSizing,
832
862
  columnSizingInfo,
863
+ sorting: columnSorting.sorting,
833
864
  rowSelection,
834
865
  grouping
835
866
  },
@@ -864,6 +895,7 @@ var TablePrimitive = (props) => {
864
895
  const expand = false;
865
896
  return /* @__PURE__ */ React9.createElement(TableProvider2, {
866
897
  ...props,
898
+ ...columnSorting,
867
899
  table,
868
900
  header,
869
901
  expand,
@@ -1549,6 +1581,7 @@ export {
1549
1581
  useColumnSorting,
1550
1582
  usePinLastRow,
1551
1583
  useRowSelection,
1584
+ useSortColumn,
1552
1585
  useTableRootContext
1553
1586
  };
1554
1587
  //# sourceMappingURL=index.mjs.map