@dxos/react-ui-table 0.5.1-next.2ad6c47 → 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
 
@@ -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, {
@@ -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,
@@ -423,7 +453,7 @@ var useRowSelection = (props) => {
423
453
  var ColumnMenu = ({ column, ...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,95 @@ 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
+ })))), /* @__PURE__ */ React4.createElement(DropdownMenu.Item, {
499
529
  onClick: onOpenColumnSettings
500
- }, /* @__PURE__ */ React3.createElement("span", {
530
+ }, /* @__PURE__ */ React4.createElement("span", {
501
531
  className: "grow"
502
- }, "Column settings"), /* @__PURE__ */ React3.createElement("span", {
532
+ }, "Column settings"), /* @__PURE__ */ React4.createElement("span", {
503
533
  className: "opacity-50"
504
- }, /* @__PURE__ */ React3.createElement(GearSix, {
534
+ }, /* @__PURE__ */ React4.createElement(GearSix, {
505
535
  className: mx3(getSize3(4), "rotate-90")
506
- })))), /* @__PURE__ */ React3.createElement(DropdownMenu.Arrow, null))), /* @__PURE__ */ React3.createElement(ColumnSettingsPanel, {
536
+ })))), /* @__PURE__ */ React4.createElement(DropdownMenu.Arrow, null))), /* @__PURE__ */ React4.createElement(Popover2.Portal, null, /* @__PURE__ */ React4.createElement(Popover2.Content, null, /* @__PURE__ */ React4.createElement(Popover2.Viewport, {
537
+ classNames: "w-60"
538
+ }, /* @__PURE__ */ React4.createElement(DensityProvider, {
539
+ density: "fine"
540
+ }, /* @__PURE__ */ React4.createElement(ColumnSettingsForm, {
507
541
  ...props,
508
542
  column,
509
543
  onClose: () => setIsColumnSettingsOpen(false)
510
- }))));
544
+ }))), /* @__PURE__ */ React4.createElement(Popover2.Arrow, null))))));
511
545
  };
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
546
  var SortIndicator = ({ direction, onClick }) => {
525
- if (direction === false) {
547
+ if (direction === void 0) {
526
548
  return null;
527
549
  }
528
550
  let icon;
529
551
  switch (direction) {
530
552
  case "asc":
531
- icon = /* @__PURE__ */ React3.createElement(ArrowUp, {
553
+ icon = /* @__PURE__ */ React4.createElement(ArrowUp, {
532
554
  className: getSize3(3)
533
555
  });
534
556
  break;
535
557
  case "desc":
536
- icon = /* @__PURE__ */ React3.createElement(ArrowDown, {
558
+ icon = /* @__PURE__ */ React4.createElement(ArrowDown, {
537
559
  className: getSize3(3)
538
560
  });
539
561
  break;
540
562
  }
541
- return /* @__PURE__ */ React3.createElement("div", {
563
+ return /* @__PURE__ */ React4.createElement("div", {
542
564
  onClick,
543
565
  className: "flex items-center cursor-pointer"
544
566
  }, icon);
@@ -556,19 +578,6 @@ import { useArrowNavigationGroup } from "@fluentui/react-tabster";
556
578
  import { flexRender } from "@tanstack/react-table";
557
579
  import React6, { useContext as useContext2 } from "react";
558
580
 
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
581
  // packages/ui/react-ui-table/src/components/Table/TableRootContext.ts
573
582
  import { useVirtualizer } from "@tanstack/react-virtual";
574
583
  import { createContext as createContext2, useRef as useRef3, useState as useState7 } from "react";
@@ -819,6 +828,7 @@ var TablePrimitive = (props) => {
819
828
  handleGroupingChange,
820
829
  props.grouping
821
830
  ]);
831
+ const columnSorting = useColumnSorting();
822
832
  const table = useReactTable({
823
833
  // Data
824
834
  meta: {},
@@ -834,6 +844,7 @@ var TablePrimitive = (props) => {
834
844
  columnVisibility,
835
845
  columnSizing,
836
846
  columnSizingInfo,
847
+ sorting: columnSorting.sorting,
837
848
  rowSelection,
838
849
  grouping
839
850
  },
@@ -868,6 +879,7 @@ var TablePrimitive = (props) => {
868
879
  const expand = false;
869
880
  return /* @__PURE__ */ React9.createElement(TableProvider2, {
870
881
  ...props,
882
+ ...columnSorting,
871
883
  table,
872
884
  header,
873
885
  expand,
@@ -1522,7 +1534,6 @@ export {
1522
1534
  ColumnBuilder,
1523
1535
  ColumnMenu,
1524
1536
  ColumnSettingsForm,
1525
- ColumnSettingsPanel,
1526
1537
  MemoizedVirtualisedTableContent,
1527
1538
  SortIndicator,
1528
1539
  Table,
@@ -1554,6 +1565,7 @@ export {
1554
1565
  useColumnSorting,
1555
1566
  usePinLastRow,
1556
1567
  useRowSelection,
1568
+ useSortColumn,
1557
1569
  useTableRootContext
1558
1570
  };
1559
1571
  //# sourceMappingURL=index.mjs.map