@dxos/react-ui-table 0.5.1-main.ff7d242 → 0.5.1-next.5e6b3f6

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 useState6, useCallback as useCallback5 } from "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
 
@@ -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,
@@ -427,7 +453,7 @@ var useRowSelection = (props) => {
427
453
  var ColumnMenu = ({ column, ...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,95 @@ 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
+ })))), /* @__PURE__ */ React4.createElement(DropdownMenu.Item, {
503
529
  onClick: onOpenColumnSettings
504
- }, /* @__PURE__ */ React3.createElement("span", {
530
+ }, /* @__PURE__ */ React4.createElement("span", {
505
531
  className: "grow"
506
- }, "Column settings"), /* @__PURE__ */ React3.createElement("span", {
532
+ }, "Column settings"), /* @__PURE__ */ React4.createElement("span", {
507
533
  className: "opacity-50"
508
- }, /* @__PURE__ */ React3.createElement(GearSix, {
534
+ }, /* @__PURE__ */ React4.createElement(GearSix, {
509
535
  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, {
536
+ })))), /* @__PURE__ */ React4.createElement(DropdownMenu.Arrow, null))), /* @__PURE__ */ React4.createElement(Popover2.Portal, null, /* @__PURE__ */ React4.createElement(Popover2.Content, null, /* @__PURE__ */ React4.createElement(Popover2.Viewport, {
511
537
  classNames: "w-60"
512
- }, /* @__PURE__ */ React3.createElement(DensityProvider, {
538
+ }, /* @__PURE__ */ React4.createElement(DensityProvider, {
513
539
  density: "fine"
514
- }, /* @__PURE__ */ React3.createElement(ColumnSettingsForm, {
540
+ }, /* @__PURE__ */ React4.createElement(ColumnSettingsForm, {
515
541
  ...props,
516
542
  column,
517
543
  onClose: () => setIsColumnSettingsOpen(false)
518
- }))), /* @__PURE__ */ React3.createElement(Popover2.Arrow, null))))));
544
+ }))), /* @__PURE__ */ React4.createElement(Popover2.Arrow, null))))));
519
545
  };
520
546
  var SortIndicator = ({ direction, onClick }) => {
521
- if (direction === false) {
547
+ if (direction === void 0) {
522
548
  return null;
523
549
  }
524
550
  let icon;
525
551
  switch (direction) {
526
552
  case "asc":
527
- icon = /* @__PURE__ */ React3.createElement(ArrowUp, {
553
+ icon = /* @__PURE__ */ React4.createElement(ArrowUp, {
528
554
  className: getSize3(3)
529
555
  });
530
556
  break;
531
557
  case "desc":
532
- icon = /* @__PURE__ */ React3.createElement(ArrowDown, {
558
+ icon = /* @__PURE__ */ React4.createElement(ArrowDown, {
533
559
  className: getSize3(3)
534
560
  });
535
561
  break;
536
562
  }
537
- return /* @__PURE__ */ React3.createElement("div", {
563
+ return /* @__PURE__ */ React4.createElement("div", {
538
564
  onClick,
539
565
  className: "flex items-center cursor-pointer"
540
566
  }, icon);
@@ -552,19 +578,6 @@ import { useArrowNavigationGroup } from "@fluentui/react-tabster";
552
578
  import { flexRender } from "@tanstack/react-table";
553
579
  import React6, { useContext as useContext2 } from "react";
554
580
 
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
581
  // packages/ui/react-ui-table/src/components/Table/TableRootContext.ts
569
582
  import { useVirtualizer } from "@tanstack/react-virtual";
570
583
  import { createContext as createContext2, useRef as useRef3, useState as useState7 } from "react";
@@ -815,6 +828,7 @@ var TablePrimitive = (props) => {
815
828
  handleGroupingChange,
816
829
  props.grouping
817
830
  ]);
831
+ const columnSorting = useColumnSorting();
818
832
  const table = useReactTable({
819
833
  // Data
820
834
  meta: {},
@@ -830,6 +844,7 @@ var TablePrimitive = (props) => {
830
844
  columnVisibility,
831
845
  columnSizing,
832
846
  columnSizingInfo,
847
+ sorting: columnSorting.sorting,
833
848
  rowSelection,
834
849
  grouping
835
850
  },
@@ -864,6 +879,7 @@ var TablePrimitive = (props) => {
864
879
  const expand = false;
865
880
  return /* @__PURE__ */ React9.createElement(TableProvider2, {
866
881
  ...props,
882
+ ...columnSorting,
867
883
  table,
868
884
  header,
869
885
  expand,
@@ -1549,6 +1565,7 @@ export {
1549
1565
  useColumnSorting,
1550
1566
  usePinLastRow,
1551
1567
  useRowSelection,
1568
+ useSortColumn,
1552
1569
  useTableRootContext
1553
1570
  };
1554
1571
  //# sourceMappingURL=index.mjs.map