@dxos/react-ui-table 0.4.9 → 0.4.10-main.0be5154
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 +323 -183
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/ColumnMenu.d.ts +8 -2
- package/dist/types/src/components/ColumnMenu.d.ts.map +1 -1
- package/dist/types/src/components/ColumnMenu.stories.d.ts +4 -0
- package/dist/types/src/components/ColumnMenu.stories.d.ts.map +1 -1
- package/dist/types/src/components/ColumnSettingsForm.d.ts +10 -0
- package/dist/types/src/components/ColumnSettingsForm.d.ts.map +1 -0
- package/dist/types/src/components/Table/Table.d.ts.map +1 -1
- package/dist/types/src/components/Table/Table.stories.d.ts +3 -0
- package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
- package/dist/types/src/components/Table/TableBody.d.ts.map +1 -1
- package/dist/types/src/components/hooks/useColumnSorting.d.ts +9 -0
- package/dist/types/src/components/hooks/useColumnSorting.d.ts.map +1 -0
- package/dist/types/src/components/hooks/usePinLastRow.d.ts +3 -0
- package/dist/types/src/components/hooks/usePinLastRow.d.ts.map +1 -0
- package/dist/types/src/theme.d.ts +1 -0
- package/dist/types/src/theme.d.ts.map +1 -1
- package/package.json +14 -14
- package/src/components/ColumnMenu.stories.tsx +52 -1
- package/src/components/ColumnMenu.tsx +129 -211
- package/src/components/ColumnSettingsForm.tsx +206 -0
- package/src/components/Table/Table.stories.tsx +31 -0
- package/src/components/Table/Table.tsx +6 -15
- package/src/components/Table/TableBody.tsx +6 -1
- package/src/components/hooks/useColumnSorting.ts +54 -0
- package/src/components/hooks/usePinLastRow.ts +43 -0
- package/src/helpers.tsx +1 -1
- package/src/theme.ts +3 -2
- package/dist/types/src/components/SortButton.d.ts +0 -8
- package/dist/types/src/components/SortButton.d.ts.map +0 -1
- package/src/components/SortButton.tsx +0 -34
|
@@ -28,10 +28,15 @@ const TableBody = ({ rows }: TableBodyProps) => {
|
|
|
28
28
|
{rows.map((row) => {
|
|
29
29
|
const isCurrent = currentDatum === row.original;
|
|
30
30
|
const isSelected = rowSelection?.[row.id];
|
|
31
|
+
|
|
32
|
+
const isPinned = row.getIsPinned();
|
|
33
|
+
|
|
34
|
+
const classNames = tbodyTr({ canBeCurrent, isPinned: isPinned !== false });
|
|
35
|
+
|
|
31
36
|
return (
|
|
32
37
|
<tr
|
|
33
38
|
key={keyAccessor ? keyAccessor(row.original) : row.id}
|
|
34
|
-
className={
|
|
39
|
+
className={classNames}
|
|
35
40
|
{...(isCurrent && { 'aria-current': 'location' })}
|
|
36
41
|
{...(isSelected && { 'aria-selected': 'true' })}
|
|
37
42
|
{...(canBeCurrent && {
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type SortDirection, type Column } from '@tanstack/react-table';
|
|
6
|
+
import { useState } from 'react';
|
|
7
|
+
|
|
8
|
+
export const useColumnSorting = (column: Column<any>) => {
|
|
9
|
+
const [sortDirection, setSortDirection] = useState<SortDirection | false>(column.getIsSorted());
|
|
10
|
+
const [firstSort, setFirstSort] = useState<SortDirection | undefined>();
|
|
11
|
+
|
|
12
|
+
const canSort = column.getCanSort();
|
|
13
|
+
|
|
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
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// Whichever sort is explicitly set first, will be the first sort direction
|
|
31
|
+
// When toggling, we always go to the other direction, and then to unsorted.
|
|
32
|
+
const onToggleSort = () => {
|
|
33
|
+
if (sortDirection === false) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (sortDirection === firstSort) {
|
|
38
|
+
updateColumnSort(firstSort === 'asc' ? 'desc' : 'asc');
|
|
39
|
+
} else {
|
|
40
|
+
updateColumnSort(false);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const onSelectSort = (direction: SortDirection) => {
|
|
45
|
+
setFirstSort(direction);
|
|
46
|
+
updateColumnSort(direction);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const onClearSort = () => {
|
|
50
|
+
updateColumnSort(false);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return { canSort, sortDirection, onSelectSort, onToggleSort, onClearSort };
|
|
54
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Table } from '@tanstack/react-table';
|
|
6
|
+
import { useEffect, useState } from 'react';
|
|
7
|
+
|
|
8
|
+
export const usePinLastRow = (
|
|
9
|
+
pinLastRow: boolean | undefined,
|
|
10
|
+
table: Table<any>,
|
|
11
|
+
data: any,
|
|
12
|
+
getScrollElement?: () => Element | null,
|
|
13
|
+
) => {
|
|
14
|
+
const [pinnedRowKey, setPinnedRowKey] = useState<string>();
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (!pinLastRow) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const rows = table.getRowModel().rows;
|
|
22
|
+
const rowToPin = rows[rows.length - 1];
|
|
23
|
+
const rowId = rowToPin.id;
|
|
24
|
+
|
|
25
|
+
if (rowId === pinnedRowKey) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
table.resetRowPinning();
|
|
30
|
+
rowToPin.pin('bottom');
|
|
31
|
+
|
|
32
|
+
const scrollElement = getScrollElement?.();
|
|
33
|
+
const isFirstRender = pinnedRowKey === undefined;
|
|
34
|
+
|
|
35
|
+
if (scrollElement && !isFirstRender) {
|
|
36
|
+
requestAnimationFrame(() => {
|
|
37
|
+
scrollElement.scrollTo({ top: scrollElement.scrollHeight });
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
setPinnedRowKey(rowId);
|
|
42
|
+
}, [pinLastRow, table, getScrollElement, pinnedRowKey, setPinnedRowKey, data]);
|
|
43
|
+
};
|
package/src/helpers.tsx
CHANGED
|
@@ -340,7 +340,7 @@ export class ColumnBuilder<TData extends RowData> {
|
|
|
340
340
|
...props.meta,
|
|
341
341
|
onUpdate,
|
|
342
342
|
digits,
|
|
343
|
-
header: {
|
|
343
|
+
header: { ...props.meta?.header },
|
|
344
344
|
cell: { ...props.meta?.cell, classNames },
|
|
345
345
|
},
|
|
346
346
|
};
|
package/src/theme.ts
CHANGED
|
@@ -75,13 +75,14 @@ export const theadResizeRoot: ComponentFunction<{ isResizing: boolean }> = ({ is
|
|
|
75
75
|
export type TbodyStyleProps = Partial<TableContextValue<any>>;
|
|
76
76
|
export const tbodyRoot: ComponentFunction<TbodyStyleProps> = (_props, ...etc) => mx(...etc);
|
|
77
77
|
|
|
78
|
-
export type TbodyTrStyleProps = Partial<{ canBeCurrent?: boolean }>;
|
|
79
|
-
export const tbodyTr: ComponentFunction<TbodyTrStyleProps> = ({ canBeCurrent }, ...etc) =>
|
|
78
|
+
export type TbodyTrStyleProps = Partial<{ canBeCurrent?: boolean; isPinned?: boolean }>;
|
|
79
|
+
export const tbodyTr: ComponentFunction<TbodyTrStyleProps> = ({ canBeCurrent, isPinned }, ...etc) =>
|
|
80
80
|
mx(
|
|
81
81
|
'group',
|
|
82
82
|
canBeCurrent ? ghostSelectedCurrent : ghostSelected,
|
|
83
83
|
canBeCurrent && focusRing,
|
|
84
84
|
canBeCurrent && 'cursor-pointer rounded',
|
|
85
|
+
isPinned && 'sticky z-1 bottom-[-1px] base-surface',
|
|
85
86
|
...etc,
|
|
86
87
|
);
|
|
87
88
|
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { type SortDirection } from '@tanstack/react-table';
|
|
2
|
-
type SortButtonProps = {
|
|
3
|
-
sortDirection: false | SortDirection;
|
|
4
|
-
onClick: (e: any) => void;
|
|
5
|
-
};
|
|
6
|
-
export declare const SortButton: ({ sortDirection, onClick }: SortButtonProps) => JSX.Element;
|
|
7
|
-
export {};
|
|
8
|
-
//# sourceMappingURL=SortButton.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SortButton.d.ts","sourceRoot":"","sources":["../../../../src/components/SortButton.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAM3D,KAAK,eAAe,GAAG;IAAE,aAAa,EAAE,KAAK,GAAG,aAAa,CAAC;IAAC,OAAO,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;CAAE,CAAC;AAE3F,eAAO,MAAM,UAAU,+BAAgC,eAAe,gBAoBrE,CAAC"}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { CaretUpDown, SortAscending, SortDescending } from '@phosphor-icons/react';
|
|
6
|
-
import { type SortDirection } from '@tanstack/react-table';
|
|
7
|
-
import React, { type ReactElement } from 'react';
|
|
8
|
-
|
|
9
|
-
import { Button } from '@dxos/react-ui';
|
|
10
|
-
import { getSize } from '@dxos/react-ui-theme';
|
|
11
|
-
|
|
12
|
-
type SortButtonProps = { sortDirection: false | SortDirection; onClick: (e: any) => void };
|
|
13
|
-
|
|
14
|
-
export const SortButton = ({ sortDirection, onClick }: SortButtonProps) => {
|
|
15
|
-
let icon: ReactElement;
|
|
16
|
-
const size = getSize(4);
|
|
17
|
-
|
|
18
|
-
switch (sortDirection) {
|
|
19
|
-
case 'asc':
|
|
20
|
-
icon = <SortAscending className={size} />;
|
|
21
|
-
break;
|
|
22
|
-
case 'desc':
|
|
23
|
-
icon = <SortDescending className={size} />;
|
|
24
|
-
break;
|
|
25
|
-
default:
|
|
26
|
-
icon = <CaretUpDown className={size} />;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return (
|
|
30
|
-
<Button variant='ghost' onClick={onClick}>
|
|
31
|
-
{icon}
|
|
32
|
-
</Button>
|
|
33
|
-
);
|
|
34
|
-
};
|