@dxos/react-ui-table 0.5.1-main.c37e119 → 0.5.1-main.d3f3a55

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.
@@ -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 };