@dxos/react-ui-table 0.5.1-next.2ad6c47 → 0.5.1-next.39e0ee2
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.
- package/dist/lib/browser/index.mjs +116 -88
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/ColumnMenu/ColumnMenu.d.ts +6 -6
- package/dist/types/src/components/ColumnMenu/ColumnMenu.d.ts.map +1 -1
- package/dist/types/src/components/ColumnMenu/ColumnMenu.stories.d.ts +2 -2
- package/dist/types/src/components/ColumnMenu/ColumnSettingsForm.d.ts +2 -2
- package/dist/types/src/components/ColumnMenu/ColumnSettingsForm.d.ts.map +1 -1
- package/dist/types/src/components/Table/Table.d.ts.map +1 -1
- package/dist/types/src/components/Table/props.d.ts +3 -1
- package/dist/types/src/components/Table/props.d.ts.map +1 -1
- package/dist/types/src/hooks/useColumnSorting.d.ts +9 -3
- package/dist/types/src/hooks/useColumnSorting.d.ts.map +1 -1
- package/package.json +15 -15
- package/src/components/ColumnMenu/ColumnMenu.stories.tsx +2 -2
- package/src/components/ColumnMenu/ColumnMenu.tsx +49 -36
- package/src/components/ColumnMenu/ColumnSettingsForm.tsx +10 -5
- package/src/components/Table/Table.tsx +5 -2
- package/src/components/Table/props.ts +3 -1
- package/src/hooks/useColumnSorting.ts +34 -24
|
@@ -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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
15
|
-
|
|
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 ===
|
|
42
|
+
if (sortDirection === undefined) {
|
|
34
43
|
return;
|
|
35
44
|
}
|
|
36
45
|
|
|
37
|
-
if (sortDirection ===
|
|
38
|
-
updateColumnSort(
|
|
46
|
+
if (sortDirection === initialSortDirection) {
|
|
47
|
+
updateColumnSort(initialSortDirection === 'asc' ? 'desc' : 'asc');
|
|
39
48
|
} else {
|
|
40
|
-
|
|
49
|
+
clearColumnSorting();
|
|
41
50
|
}
|
|
42
51
|
};
|
|
43
52
|
|
|
44
53
|
const onSelectSort = (direction: SortDirection) => {
|
|
45
|
-
|
|
54
|
+
clearColumnSorting();
|
|
55
|
+
setInitialSortDirection(direction);
|
|
46
56
|
updateColumnSort(direction);
|
|
47
57
|
};
|
|
48
58
|
|
|
49
59
|
const onClearSort = () => {
|
|
50
|
-
|
|
60
|
+
clearColumnSorting();
|
|
51
61
|
};
|
|
52
62
|
|
|
53
63
|
return { canSort, sortDirection, onSelectSort, onToggleSort, onClearSort };
|