@indico-data/design-system 2.60.11 → 2.60.13
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/lib/components/tanstackTable/TankstackTable.types.d.ts +2 -1
- package/lib/components/tanstackTable/TanstackTable.d.ts +1 -1
- package/lib/components/tanstackTable/docs/internalSorting/InternalClientSideSorting.stories.d.ts +7 -0
- package/lib/index.css +3 -0
- package/lib/index.d.ts +3 -2
- package/lib/index.esm.css +3 -0
- package/lib/index.esm.js +5 -2
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +5 -2
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/tanstackTable/TankstackTable.types.ts +2 -1
- package/src/components/tanstackTable/TanstackTable.stories.tsx +9 -0
- package/src/components/tanstackTable/TanstackTable.tsx +13 -2
- package/src/components/tanstackTable/docs/internalSorting/InternalClientSideSorting.mdx +31 -0
- package/src/components/tanstackTable/docs/internalSorting/InternalClientSideSorting.stories.tsx +71 -0
- package/src/components/tanstackTable/styles/table.scss +8 -0
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Row, ColumnDef } from '@tanstack/react-table';
|
|
1
|
+
import { Row, ColumnDef, SortingState } from '@tanstack/react-table';
|
|
2
2
|
|
|
3
3
|
export type WithPaginationProps = {
|
|
4
4
|
rowsPerPage: number;
|
|
@@ -42,4 +42,5 @@ export type Props<T extends object> = {
|
|
|
42
42
|
rowSelection?: Record<string, boolean>;
|
|
43
43
|
onRowSelectionChange?: (updater: Record<string, boolean>) => void;
|
|
44
44
|
onSelectAllChange?: (isSelected: boolean) => void;
|
|
45
|
+
defaultSorting?: SortingState;
|
|
45
46
|
} & PaginationProps;
|
|
@@ -25,6 +25,15 @@ const meta: Meta = {
|
|
|
25
25
|
isStriped: true,
|
|
26
26
|
},
|
|
27
27
|
argTypes: {
|
|
28
|
+
defaultSorting: {
|
|
29
|
+
description:
|
|
30
|
+
'You may pass a default sorting state to the table. This will be used to sort the table by default. This is useful if you want to sort the table by a column by default.',
|
|
31
|
+
control: false,
|
|
32
|
+
table: {
|
|
33
|
+
category: 'Props',
|
|
34
|
+
type: { summary: 'SortingState' },
|
|
35
|
+
},
|
|
36
|
+
},
|
|
28
37
|
isStriped: {
|
|
29
38
|
description: 'Striped rows',
|
|
30
39
|
defaultValue: { summary: 'true' },
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'react';
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
useReactTable,
|
|
5
|
+
getCoreRowModel,
|
|
6
|
+
getSortedRowModel,
|
|
7
|
+
SortingState,
|
|
8
|
+
} from '@tanstack/react-table';
|
|
4
9
|
import classNames from 'classnames';
|
|
5
10
|
|
|
6
11
|
import { ActionBar } from './components/ActionBar';
|
|
@@ -33,6 +38,7 @@ export function TanstackTable<T extends object>({
|
|
|
33
38
|
activeRows = [],
|
|
34
39
|
isStriped = true,
|
|
35
40
|
actionBarClassName,
|
|
41
|
+
defaultSorting,
|
|
36
42
|
...rest
|
|
37
43
|
}: Props<T & { id: string }>) {
|
|
38
44
|
const {
|
|
@@ -47,6 +53,8 @@ export function TanstackTable<T extends object>({
|
|
|
47
53
|
defaultColumns,
|
|
48
54
|
});
|
|
49
55
|
|
|
56
|
+
// handles internal sorting state.
|
|
57
|
+
const [sorting, setSorting] = useState<SortingState>(defaultSorting ?? []);
|
|
50
58
|
const thRefs = useRef<Record<string, HTMLTableCellElement | null>>({});
|
|
51
59
|
|
|
52
60
|
// Auto-compute column widths based on current table header cell widths for columns without a defined size.
|
|
@@ -73,10 +81,13 @@ export function TanstackTable<T extends object>({
|
|
|
73
81
|
columns: formattedColumns,
|
|
74
82
|
state: {
|
|
75
83
|
rowSelection,
|
|
84
|
+
sorting,
|
|
76
85
|
},
|
|
77
86
|
enableRowSelection,
|
|
78
87
|
onRowSelectionChange: setRowSelection,
|
|
79
88
|
getCoreRowModel: getCoreRowModel(),
|
|
89
|
+
getSortedRowModel: getSortedRowModel(),
|
|
90
|
+
onSortingChange: setSorting,
|
|
80
91
|
manualPagination: true,
|
|
81
92
|
getRowId: (row) => row?.id,
|
|
82
93
|
initialState: {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Canvas, Meta, Controls } from '@storybook/blocks';
|
|
2
|
+
import * as TableStories from './InternalClientSideSorting.stories';
|
|
3
|
+
|
|
4
|
+
<Meta title="Layout/Tanstack Table/Internal Client Side Sorting" name="Tanstack Table Internal Client Side Sorting" />
|
|
5
|
+
|
|
6
|
+
# Tanstack Table Internal Client Side Sorting
|
|
7
|
+
|
|
8
|
+
This is an example of how to implement internal client side sorting. This is useful when you want to sort the data on the client side without making a request to the server. It can also be used for simple data tables that don't require much state management outside of the table itself.
|
|
9
|
+
|
|
10
|
+
<Canvas of={TableStories.InternalClientSideSorting}
|
|
11
|
+
source={{
|
|
12
|
+
code: `
|
|
13
|
+
const SortableHeader = ({ column, label }: { column: any; label: string }) => {
|
|
14
|
+
const getIcon = () => {
|
|
15
|
+
if (!column.getIsSorted()) return 'fa-sort';
|
|
16
|
+
return column.getIsSorted() === 'asc' ? 'fa-sort-up' : 'fa-sort-down';
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<Button
|
|
21
|
+
type="button"
|
|
22
|
+
onClick={column.getToggleSortingHandler()}
|
|
23
|
+
iconRight={getIcon()}
|
|
24
|
+
ariaLabel={\`Sort by \${label}\`}
|
|
25
|
+
variant="link"
|
|
26
|
+
>
|
|
27
|
+
{label}
|
|
28
|
+
</Button>
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
`}} />
|
package/src/components/tanstackTable/docs/internalSorting/InternalClientSideSorting.stories.tsx
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
|
|
3
|
+
import { TanstackTable } from '../../TanstackTable';
|
|
4
|
+
import { people, Person } from '../../mock-data/mock-data';
|
|
5
|
+
import { columns } from '../../mock-data/table-configuration';
|
|
6
|
+
import { ColumnDef } from '@tanstack/react-table';
|
|
7
|
+
import { Button } from '../../../button';
|
|
8
|
+
|
|
9
|
+
const meta: Meta = {
|
|
10
|
+
title: 'Layout/Tanstack Table/Internal Client Side Sorting',
|
|
11
|
+
component: TanstackTable,
|
|
12
|
+
args: {
|
|
13
|
+
data: people as (Person & { id: string })[],
|
|
14
|
+
columns: columns as ColumnDef<Person & { id: string }>[],
|
|
15
|
+
},
|
|
16
|
+
decorators: [
|
|
17
|
+
(Story) => (
|
|
18
|
+
<div style={{ width: '100%', height: '100%' }}>
|
|
19
|
+
<Story />
|
|
20
|
+
</div>
|
|
21
|
+
),
|
|
22
|
+
],
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default meta;
|
|
26
|
+
|
|
27
|
+
type Story = StoryObj<typeof TanstackTable<Person>>;
|
|
28
|
+
|
|
29
|
+
const SortableHeader = ({ column, label }: { column: any; label: string }) => {
|
|
30
|
+
const getIcon = () => {
|
|
31
|
+
if (!column.getIsSorted()) return 'fa-sort';
|
|
32
|
+
return column.getIsSorted() === 'asc' ? 'fa-sort-up' : 'fa-sort-down';
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<Button
|
|
37
|
+
type="button"
|
|
38
|
+
onClick={column.getToggleSortingHandler()}
|
|
39
|
+
iconRight={getIcon()}
|
|
40
|
+
ariaLabel={`Sort by ${label}`}
|
|
41
|
+
variant="link"
|
|
42
|
+
>
|
|
43
|
+
{label}
|
|
44
|
+
</Button>
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const InternalClientSideSorting: Story = {
|
|
49
|
+
args: {
|
|
50
|
+
columns: [
|
|
51
|
+
{
|
|
52
|
+
header: ({ column }: { column: any }) => <SortableHeader column={column} label="Name" />,
|
|
53
|
+
accessorKey: 'firstName',
|
|
54
|
+
id: 'firstName',
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
header: ({ column }: { column: any }) => <SortableHeader column={column} label="Email" />,
|
|
58
|
+
accessorKey: 'lastName',
|
|
59
|
+
id: 'lastName',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
header: ({ column }: { column: any }) => <SortableHeader column={column} label="Age" />,
|
|
63
|
+
accessorKey: 'age',
|
|
64
|
+
id: 'age',
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
},
|
|
68
|
+
render: (args) => {
|
|
69
|
+
return <TanstackTable<Person> {...args} />;
|
|
70
|
+
},
|
|
71
|
+
};
|