@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.
@@ -120,7 +120,7 @@ const MinimalTable = (props: any) => {
120
120
  return (
121
121
  <Table.Root>
122
122
  <Table.Viewport classNames='fixed inset-0 overflow-auto'>
123
- <Table.Table<any> {...props} />
123
+ <Table.Main<any> {...props} />
124
124
  </Table.Viewport>
125
125
  </Table.Root>
126
126
  );
@@ -216,7 +216,7 @@ export const Dynamic = {
216
216
  return (
217
217
  <Table.Root>
218
218
  <Table.Viewport classNames='fixed inset-0 overflow-auto'>
219
- <Table.Table<Item>
219
+ <Table.Main<Item>
220
220
  role='grid'
221
221
  rowsSelectable='multi'
222
222
  keyAccessor={(row) => row.publicKey.toHex()}
@@ -247,7 +247,7 @@ export const Editable = {
247
247
  return (
248
248
  <Table.Root>
249
249
  <Table.Viewport classNames='fixed inset-0 overflow-auto'>
250
- <Table.Table<Item>
250
+ <Table.Main<Item>
251
251
  role='grid'
252
252
  rowsSelectable='multi'
253
253
  keyAccessor={(row) => row.publicKey.toHex()}
@@ -278,7 +278,7 @@ export const PinnedLastRow = {
278
278
  return (
279
279
  <Table.Root>
280
280
  <Table.Viewport classNames='fixed inset-0 overflow-auto'>
281
- <Table.Table<Item>
281
+ <Table.Main<Item>
282
282
  role='grid'
283
283
  rowsSelectable='multi'
284
284
  keyAccessor={(row) => row.publicKey.toHex()}
@@ -332,7 +332,7 @@ export const InsertDelete = {
332
332
  </div>
333
333
  <Table.Root>
334
334
  <Table.Viewport classNames='fixed inset-0 overflow-auto'>
335
- <Table.Table<Item>
335
+ <Table.Main<Item>
336
336
  role='grid'
337
337
  rowsSelectable='multi'
338
338
  keyAccessor={(row) => row.publicKey.toHex()}
@@ -362,7 +362,7 @@ export const Resizable = {
362
362
  return (
363
363
  <Table.Root>
364
364
  <Table.Viewport classNames='fixed inset-0 overflow-auto'>
365
- <Table.Table<Item>
365
+ <Table.Main<Item>
366
366
  rowsSelectable='multi'
367
367
  keyAccessor={(row) => row.publicKey.toHex()}
368
368
  columns={makeColumns(onUpdate)}
@@ -390,7 +390,7 @@ export const TenThousandRows = {
390
390
  return (
391
391
  <Table.Root>
392
392
  <Table.Viewport classNames='fixed inset-0 overflow-auto'>
393
- <Table.Table<Item>
393
+ <Table.Main<Item>
394
394
  role='grid'
395
395
  rowsSelectable='multi'
396
396
  keyAccessor={(row) => row.publicKey.toHex()}
@@ -492,7 +492,7 @@ export const RealTimeUpdates = {
492
492
  return (
493
493
  <Table.Root>
494
494
  <Table.Viewport classNames='fixed inset-0 overflow-auto'>
495
- <Table.Table<Item>
495
+ <Table.Main<Item>
496
496
  role='grid'
497
497
  rowsSelectable='multi'
498
498
  keyAccessor={(row) => row.publicKey.toHex()}
@@ -24,7 +24,7 @@ import { TableFooter } from './TableFooter';
24
24
  import { TableHead } from './TableHead';
25
25
  import { TableRootContext, useTableRootContext } from './TableRootContext';
26
26
  import { type TableProps } from './props';
27
- import { useColumnResizing, usePinLastRow, useRowSelection } from '../../hooks';
27
+ import { useColumnResizing, useColumnSorting, usePinLastRow, useRowSelection } from '../../hooks';
28
28
  import { groupTh, tableRoot } from '../../theme';
29
29
 
30
30
  type TableRootProps = { children: React.ReactNode };
@@ -87,6 +87,8 @@ export const TablePrimitive = <TData extends RowData>(props: TableProps<TData>)
87
87
  const [grouping, handleGroupingChange] = useState<GroupingState>(props.grouping ?? []);
88
88
  useEffect(() => handleGroupingChange(props.grouping ?? []), [handleGroupingChange, props.grouping]);
89
89
 
90
+ const columnSorting = useColumnSorting();
91
+
90
92
  const table = useReactTable({
91
93
  // Data
92
94
  meta: {},
@@ -104,6 +106,7 @@ export const TablePrimitive = <TData extends RowData>(props: TableProps<TData>)
104
106
  columnVisibility,
105
107
  columnSizing,
106
108
  columnSizingInfo,
109
+ sorting: columnSorting.sorting,
107
110
  rowSelection,
108
111
  grouping,
109
112
  },
@@ -146,7 +149,7 @@ export const TablePrimitive = <TData extends RowData>(props: TableProps<TData>)
146
149
 
147
150
  return (
148
151
  <TableProvider
149
- {...props}
152
+ {...{ ...props, ...columnSorting }}
150
153
  table={table}
151
154
  header={header}
152
155
  expand={expand}
@@ -280,6 +283,6 @@ const GroupedTableContent = () => {
280
283
 
281
284
  export const Table = {
282
285
  Root: TableRoot,
283
- Table: TablePrimitive,
284
286
  Viewport: TableViewport,
287
+ Main: TablePrimitive,
285
288
  };
@@ -6,6 +6,7 @@ import { type RowData, type RowSelectionState, type Table, type VisibilityState
6
6
 
7
7
  import { type ClassNameValue } from '@dxos/react-ui-types';
8
8
 
9
+ import { type ColumnSorting } from '../../hooks';
9
10
  import { type KeyValue, type TableColumnDef } from '../../types';
10
11
 
11
12
  export type TableFlags = Partial<{
@@ -37,6 +38,7 @@ export type TableProps<TData extends RowData> = TableFlags &
37
38
  data: TData[];
38
39
  columns: TableColumnDef<TData>[];
39
40
  onColumnResize: (state: Record<string, number>) => void;
41
+ onColumnReorder: (columnId: string, direction: 'left' | 'right') => void;
40
42
  columnVisibility: VisibilityState;
41
43
  // Controllable row selection
42
44
  rowSelection: RowSelectionState;
@@ -56,4 +58,4 @@ export type TableContextValue<TData> = TableFlags &
56
58
  Pick<TableProps<TData>, 'keyAccessor'> & {
57
59
  table: Table<TData>;
58
60
  isGrid: boolean;
59
- };
61
+ } & ColumnSorting;
@@ -2,52 +2,62 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type SortDirection, type Column } from '@tanstack/react-table';
5
+ import { type SortDirection, type Column, type ColumnSort } from '@tanstack/react-table';
6
6
  import { useState } from 'react';
7
7
 
8
- export const useColumnSorting = (column: Column<any>) => {
9
- const [sortDirection, setSortDirection] = useState<SortDirection | false>(column.getIsSorted());
10
- const [firstSort, setFirstSort] = useState<SortDirection | undefined>();
8
+ import { useTableContext } from '../components/Table/TableContext';
9
+
10
+ export const useColumnSorting = () => {
11
+ const [columnSorting, setColumnSorting] = useState<ColumnSort[]>([]);
12
+
13
+ const sortColumn = (columnId: string, sortDirection: SortDirection) => {
14
+ setColumnSorting([{ id: columnId, desc: sortDirection === 'desc' }]);
15
+ };
16
+
17
+ const clearColumnSorting = () => {
18
+ setColumnSorting([]);
19
+ };
20
+
21
+ return { sorting: columnSorting, sortColumn, clearColumnSorting };
22
+ };
23
+
24
+ export type ColumnSorting = ReturnType<typeof useColumnSorting>;
25
+
26
+ export const useSortColumn = (column: Column<any>) => {
27
+ const { sorting, sortColumn, clearColumnSorting } = useTableContext();
28
+ const [initialSortDirection, setInitialSortDirection] = useState<SortDirection | undefined>();
29
+
30
+ const columnSort = sorting.find((s) => s.id === column.id);
31
+ const sortDirection = columnSort ? (columnSort.desc ? ('desc' as const) : ('asc' as const)) : undefined;
11
32
 
12
33
  const canSort = column.getCanSort();
13
34
 
14
- const updateColumnSort = (direction: SortDirection | false) => {
15
- setSortDirection(direction);
16
-
17
- switch (direction) {
18
- case false:
19
- column.clearSorting();
20
- break;
21
- case 'asc':
22
- column.toggleSorting(false); // descending false
23
- break;
24
- case 'desc':
25
- column.toggleSorting(true); // descending true
26
- break;
27
- }
35
+ const updateColumnSort = (direction: SortDirection) => {
36
+ sortColumn(column.id, direction);
28
37
  };
29
38
 
30
39
  // Whichever sort is explicitly set first, will be the first sort direction
31
40
  // When toggling, we always go to the other direction, and then to unsorted.
32
41
  const onToggleSort = () => {
33
- if (sortDirection === false) {
42
+ if (sortDirection === undefined) {
34
43
  return;
35
44
  }
36
45
 
37
- if (sortDirection === firstSort) {
38
- updateColumnSort(firstSort === 'asc' ? 'desc' : 'asc');
46
+ if (sortDirection === initialSortDirection) {
47
+ updateColumnSort(initialSortDirection === 'asc' ? 'desc' : 'asc');
39
48
  } else {
40
- updateColumnSort(false);
49
+ clearColumnSorting();
41
50
  }
42
51
  };
43
52
 
44
53
  const onSelectSort = (direction: SortDirection) => {
45
- setFirstSort(direction);
54
+ clearColumnSorting();
55
+ setInitialSortDirection(direction);
46
56
  updateColumnSort(direction);
47
57
  };
48
58
 
49
59
  const onClearSort = () => {
50
- updateColumnSort(false);
60
+ clearColumnSorting();
51
61
  };
52
62
 
53
63
  return { canSort, sortDirection, onSelectSort, onToggleSort, onClearSort };
@@ -54,7 +54,7 @@ export const SchemaTable = {
54
54
  return (
55
55
  <Table.Root>
56
56
  <Table.Viewport classNames='inset-0 fixed'>
57
- <Table.Table<ExampleSchema>
57
+ <Table.Main<ExampleSchema>
58
58
  role='grid'
59
59
  rowsSelectable='multi'
60
60
  keyAccessor={(row) => JSON.stringify(row)}