@dust-tt/sparkle 0.2.196 → 0.2.198
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/cjs/components/DataTable.d.ts +40 -10
- package/dist/cjs/index.js +3366 -52
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/stories/DataTable.stories.d.ts +9 -0
- package/dist/esm/components/DataTable.d.ts +40 -10
- package/dist/esm/index.js +3366 -52
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/stories/DataTable.stories.d.ts +9 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/src/components/DataTable.tsx +267 -101
- package/src/stories/DataTable.stories.tsx +111 -21
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dust-tt/sparkle",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.198",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"build": "rm -rf dist && rollup -c",
|
|
6
6
|
"build:with-tw-base": "rollup -c --environment INCLUDE_TW_BASE:true",
|
|
@@ -81,6 +81,7 @@
|
|
|
81
81
|
"@emoji-mart/data": "^1.1.2",
|
|
82
82
|
"@emoji-mart/react": "^1.1.1",
|
|
83
83
|
"@headlessui/react": "^1.7.19",
|
|
84
|
+
"@tanstack/react-table": "^8.13.0",
|
|
84
85
|
"emoji-mart": "^5.5.2",
|
|
85
86
|
"esbuild": "^0.20.0",
|
|
86
87
|
"eslint-plugin-import": "^2.29.1",
|
|
@@ -1,108 +1,274 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
type ColumnDef,
|
|
3
|
+
ColumnFiltersState,
|
|
4
|
+
flexRender,
|
|
5
|
+
getCoreRowModel,
|
|
6
|
+
getFilteredRowModel,
|
|
7
|
+
getSortedRowModel,
|
|
8
|
+
type SortingState,
|
|
9
|
+
useReactTable,
|
|
10
|
+
} from "@tanstack/react-table";
|
|
11
|
+
import React, { ReactNode, useEffect, useState } from "react";
|
|
2
12
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
13
|
+
import { Avatar, MoreIcon } from "@sparkle/index";
|
|
14
|
+
import { ArrowDownIcon, ArrowUpIcon } from "@sparkle/index";
|
|
15
|
+
import { classNames } from "@sparkle/lib/utils";
|
|
6
16
|
|
|
7
|
-
|
|
17
|
+
import { Icon } from "./Icon";
|
|
8
18
|
|
|
9
|
-
interface DataTableProps {
|
|
10
|
-
|
|
11
|
-
columns:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
downloadButton?: ReactNode;
|
|
19
|
+
interface DataTableProps<TData, TValue> {
|
|
20
|
+
data: TData[];
|
|
21
|
+
columns: ColumnDef<TData, TValue>[];
|
|
22
|
+
className?: string;
|
|
23
|
+
filter?: string;
|
|
24
|
+
filterColumn?: string;
|
|
16
25
|
}
|
|
17
26
|
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
27
|
+
export function DataTable<TData, TValue>({
|
|
28
|
+
data,
|
|
29
|
+
columns,
|
|
30
|
+
className,
|
|
31
|
+
filter,
|
|
32
|
+
filterColumn,
|
|
33
|
+
}: DataTableProps<TData, TValue>) {
|
|
34
|
+
const [sorting, setSorting] = useState<SortingState>([]);
|
|
35
|
+
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
|
|
36
|
+
|
|
37
|
+
const table = useReactTable({
|
|
38
|
+
data,
|
|
39
|
+
columns,
|
|
40
|
+
onSortingChange: setSorting,
|
|
41
|
+
getCoreRowModel: getCoreRowModel(),
|
|
42
|
+
getSortedRowModel: getSortedRowModel(),
|
|
43
|
+
getFilteredRowModel: getFilteredRowModel(),
|
|
44
|
+
onColumnFiltersChange: setColumnFilters,
|
|
45
|
+
state: {
|
|
46
|
+
columnFilters,
|
|
47
|
+
sorting,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
if (filterColumn) {
|
|
53
|
+
table.getColumn(filterColumn)?.setFilterValue(filter);
|
|
54
|
+
}
|
|
55
|
+
}, [filter, filterColumn]);
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<DataTable.Root className={className}>
|
|
59
|
+
<DataTable.Header>
|
|
60
|
+
{table.getHeaderGroups().map((headerGroup) => (
|
|
61
|
+
<DataTable.Row key={headerGroup.id}>
|
|
62
|
+
{headerGroup.headers.map((header) => (
|
|
63
|
+
<DataTable.Head
|
|
64
|
+
key={header.id}
|
|
65
|
+
onClick={header.column.getToggleSortingHandler()}
|
|
66
|
+
className={header.column.getCanSort() ? "s-cursor-pointer" : ""}
|
|
67
|
+
>
|
|
68
|
+
<div className="s-flex s-items-center s-space-x-1 s-whitespace-nowrap">
|
|
69
|
+
{flexRender(
|
|
70
|
+
header.column.columnDef.header,
|
|
71
|
+
header.getContext()
|
|
72
|
+
)}
|
|
73
|
+
{header.column.getCanSort() && (
|
|
74
|
+
<Icon
|
|
75
|
+
visual={
|
|
76
|
+
header.column.getIsSorted() === "asc"
|
|
77
|
+
? ArrowUpIcon
|
|
78
|
+
: header.column.getIsSorted() === "desc"
|
|
79
|
+
? ArrowDownIcon
|
|
80
|
+
: ArrowDownIcon
|
|
81
|
+
}
|
|
82
|
+
className={classNames(
|
|
83
|
+
"s-ml-1 s-w-2.5 s-font-extralight",
|
|
84
|
+
header.column.getIsSorted()
|
|
85
|
+
? "s-opacity-100"
|
|
86
|
+
: "s-opacity-0"
|
|
87
|
+
)}
|
|
88
|
+
/>
|
|
89
|
+
)}
|
|
90
|
+
</div>
|
|
91
|
+
</DataTable.Head>
|
|
92
|
+
))}
|
|
93
|
+
</DataTable.Row>
|
|
94
|
+
))}
|
|
95
|
+
</DataTable.Header>
|
|
96
|
+
<DataTable.Body>
|
|
97
|
+
{table.getRowModel().rows.map((row) => (
|
|
98
|
+
<DataTable.Row
|
|
99
|
+
key={row.id}
|
|
100
|
+
onClick={row.original.onClick}
|
|
101
|
+
onMoreClick={row.original.onMoreClick}
|
|
102
|
+
>
|
|
103
|
+
{row.getVisibleCells().map((cell) => (
|
|
104
|
+
<DataTable.Cell key={cell.id}>
|
|
105
|
+
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
106
|
+
</DataTable.Cell>
|
|
107
|
+
))}
|
|
108
|
+
</DataTable.Row>
|
|
109
|
+
))}
|
|
110
|
+
</DataTable.Body>
|
|
111
|
+
</DataTable.Root>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface DataTableRootProps extends React.HTMLAttributes<HTMLTableElement> {
|
|
116
|
+
children: ReactNode;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
DataTable.Root = function DataTableRoot({
|
|
120
|
+
children,
|
|
121
|
+
className,
|
|
122
|
+
...props
|
|
123
|
+
}: DataTableRootProps) {
|
|
124
|
+
return (
|
|
125
|
+
<table
|
|
126
|
+
className={classNames("s-w-full s-border-collapse", className || "")}
|
|
127
|
+
{...props}
|
|
128
|
+
>
|
|
129
|
+
{children}
|
|
130
|
+
</table>
|
|
131
|
+
);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
interface HeaderProps extends React.HTMLAttributes<HTMLTableSectionElement> {
|
|
135
|
+
children: ReactNode;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
DataTable.Header = function Header({
|
|
139
|
+
children,
|
|
140
|
+
className,
|
|
141
|
+
...props
|
|
142
|
+
}: HeaderProps) {
|
|
143
|
+
return (
|
|
144
|
+
<thead
|
|
145
|
+
className={classNames("s-text-xs s-capitalize", className || "")}
|
|
146
|
+
{...props}
|
|
147
|
+
>
|
|
148
|
+
{children}
|
|
149
|
+
</thead>
|
|
150
|
+
);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
interface HeadProps extends React.ThHTMLAttributes<HTMLTableCellElement> {
|
|
154
|
+
children?: ReactNode;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
DataTable.Head = function Head({ children, className, ...props }: HeadProps) {
|
|
158
|
+
return (
|
|
159
|
+
<th
|
|
160
|
+
className={classNames(
|
|
161
|
+
"s-w-full s-py-1 s-pr-3 s-text-left s-font-medium s-text-element-800",
|
|
162
|
+
className || ""
|
|
163
|
+
)}
|
|
164
|
+
{...props}
|
|
165
|
+
>
|
|
166
|
+
{children}
|
|
167
|
+
</th>
|
|
168
|
+
);
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
DataTable.Body = function Body({
|
|
172
|
+
children,
|
|
173
|
+
className,
|
|
174
|
+
...props
|
|
175
|
+
}: React.HTMLAttributes<HTMLTableSectionElement>) {
|
|
176
|
+
return (
|
|
177
|
+
<tbody className={className} {...props}>
|
|
178
|
+
{children}
|
|
179
|
+
</tbody>
|
|
180
|
+
);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
interface RowProps extends React.HTMLAttributes<HTMLTableRowElement> {
|
|
184
|
+
children: ReactNode;
|
|
185
|
+
onClick?: () => void;
|
|
186
|
+
onMoreClick?: () => void;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
DataTable.Row = function Row({
|
|
190
|
+
children,
|
|
191
|
+
className,
|
|
192
|
+
onClick,
|
|
193
|
+
onMoreClick,
|
|
194
|
+
...props
|
|
195
|
+
}: RowProps) {
|
|
196
|
+
return (
|
|
197
|
+
<tr
|
|
198
|
+
className={classNames(
|
|
199
|
+
"s-border-b s-border-structure-200 s-text-sm",
|
|
200
|
+
onMoreClick ? "s-cursor-pointer" : "",
|
|
201
|
+
className || ""
|
|
202
|
+
)}
|
|
203
|
+
onClick={onClick ? onClick : undefined}
|
|
204
|
+
{...props}
|
|
205
|
+
>
|
|
206
|
+
{children}
|
|
207
|
+
{onMoreClick && (
|
|
208
|
+
<td
|
|
209
|
+
className="s-w-1 s-cursor-pointer s-pl-1 s-text-element-600"
|
|
210
|
+
onClick={(e) => {
|
|
211
|
+
e.stopPropagation(); // Prevent row click event
|
|
212
|
+
onMoreClick?.();
|
|
213
|
+
}}
|
|
214
|
+
>
|
|
215
|
+
<Icon visual={MoreIcon} size="sm" />
|
|
216
|
+
</td>
|
|
217
|
+
)}
|
|
218
|
+
</tr>
|
|
219
|
+
);
|
|
220
|
+
};
|
|
221
|
+
interface CellProps extends React.TdHTMLAttributes<HTMLTableCellElement> {
|
|
222
|
+
avatarUrl?: string;
|
|
223
|
+
icon?: React.ComponentType<{ className?: string }>;
|
|
224
|
+
children?: ReactNode;
|
|
225
|
+
description?: string;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
DataTable.Cell = function Cell({
|
|
229
|
+
children,
|
|
230
|
+
className,
|
|
231
|
+
avatarUrl,
|
|
232
|
+
icon,
|
|
233
|
+
description,
|
|
234
|
+
...props
|
|
235
|
+
}: CellProps) {
|
|
236
|
+
return (
|
|
237
|
+
<td
|
|
238
|
+
className={classNames(
|
|
239
|
+
"s-whitespace-nowrap s-py-2 s-pl-0.5 s-text-element-800",
|
|
240
|
+
className || ""
|
|
241
|
+
)}
|
|
242
|
+
{...props}
|
|
243
|
+
>
|
|
244
|
+
<div className="s-flex">
|
|
245
|
+
{avatarUrl && (
|
|
246
|
+
<Avatar visual={avatarUrl} size="xs" className="s-mr-2" />
|
|
247
|
+
)}
|
|
248
|
+
{icon && (
|
|
249
|
+
<Icon visual={icon} size="sm" className="s-mr-2 s-text-element-600" />
|
|
250
|
+
)}
|
|
251
|
+
<div className="s-flex">
|
|
252
|
+
<span className="s-text-sm s-text-element-800">{children}</span>
|
|
253
|
+
{description && (
|
|
254
|
+
<span className="s-pl-2 s-text-sm s-text-element-600">
|
|
255
|
+
{description}
|
|
256
|
+
</span>
|
|
257
|
+
)}
|
|
104
258
|
</div>
|
|
105
259
|
</div>
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
260
|
+
</td>
|
|
261
|
+
);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
DataTable.Caption = function Caption({
|
|
265
|
+
children,
|
|
266
|
+
className,
|
|
267
|
+
...props
|
|
268
|
+
}: React.HTMLAttributes<HTMLTableCaptionElement>) {
|
|
269
|
+
return (
|
|
270
|
+
<caption className={className} {...props}>
|
|
271
|
+
{children}
|
|
272
|
+
</caption>
|
|
273
|
+
);
|
|
274
|
+
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { Meta } from "@storybook/react";
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react";
|
|
2
|
+
import { ColumnDef } from "@tanstack/react-table";
|
|
2
3
|
import React from "react";
|
|
3
4
|
|
|
4
|
-
import {
|
|
5
|
+
import { DataTable, FolderIcon } from "../index_with_tw_base";
|
|
5
6
|
|
|
6
7
|
const meta = {
|
|
7
8
|
title: "Components/DataTable",
|
|
@@ -9,28 +10,117 @@ const meta = {
|
|
|
9
10
|
} satisfies Meta<typeof DataTable>;
|
|
10
11
|
|
|
11
12
|
export default meta;
|
|
13
|
+
type Story = StoryObj<typeof meta>;
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
type Data = {
|
|
16
|
+
name: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
usedBy: number;
|
|
19
|
+
addedBy: string;
|
|
20
|
+
lastUpdated: string;
|
|
21
|
+
size: string;
|
|
22
|
+
avatarUrl?: string;
|
|
23
|
+
icon?: React.ComponentType<{ className?: string }>;
|
|
24
|
+
clickable?: boolean;
|
|
25
|
+
onClick?: () => void;
|
|
26
|
+
showMore?: boolean;
|
|
27
|
+
onMoreClick?: () => void;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const DataTableExample = () => {
|
|
31
|
+
const data: Data[] = [
|
|
32
|
+
{
|
|
33
|
+
name: "Marketing",
|
|
34
|
+
description: "(23 items)",
|
|
35
|
+
usedBy: 8,
|
|
36
|
+
addedBy: "User1",
|
|
37
|
+
lastUpdated: "July 8, 2023",
|
|
38
|
+
size: "32kb",
|
|
39
|
+
avatarUrl: "https://dust.tt/static/droidavatar/Droid_Lime_3.jpg",
|
|
40
|
+
onClick: () => console.log("hehe"),
|
|
41
|
+
onMoreClick: () => console.log("show more"),
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: "Design",
|
|
45
|
+
usedBy: 2,
|
|
46
|
+
addedBy: "User2",
|
|
47
|
+
lastUpdated: "2023-07-09",
|
|
48
|
+
size: "64kb",
|
|
49
|
+
icon: FolderIcon,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: "Development",
|
|
53
|
+
usedBy: 5,
|
|
54
|
+
addedBy: "User3",
|
|
55
|
+
lastUpdated: "2023-07-07",
|
|
56
|
+
size: "128kb",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "Sales",
|
|
60
|
+
usedBy: 10,
|
|
61
|
+
addedBy: "User4",
|
|
62
|
+
lastUpdated: "2023-07-10",
|
|
63
|
+
size: "16kb",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: "HR",
|
|
67
|
+
usedBy: 3,
|
|
68
|
+
addedBy: "User5",
|
|
69
|
+
lastUpdated: "2023-07-06",
|
|
70
|
+
size: "48kb",
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
const columns: ColumnDef<Data>[] = [
|
|
75
|
+
{
|
|
76
|
+
accessorKey: "name",
|
|
77
|
+
header: "Name",
|
|
78
|
+
cell: (info) => (
|
|
79
|
+
<DataTable.Cell
|
|
80
|
+
avatarUrl={info.row.original.avatarUrl}
|
|
81
|
+
icon={info.row.original.icon}
|
|
82
|
+
description={info.row.original.description}
|
|
83
|
+
>
|
|
84
|
+
{info.row.original.name}
|
|
85
|
+
</DataTable.Cell>
|
|
86
|
+
),
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
accessorKey: "usedBy",
|
|
90
|
+
header: "Used by",
|
|
91
|
+
cell: (info) => (
|
|
92
|
+
<DataTable.Cell>{info.row.original.usedBy}</DataTable.Cell>
|
|
93
|
+
),
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
accessorKey: "addedBy",
|
|
97
|
+
header: "Added by",
|
|
98
|
+
cell: (info) => (
|
|
99
|
+
<DataTable.Cell>{info.row.original.addedBy}</DataTable.Cell>
|
|
100
|
+
),
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
accessorKey: "lastUpdated",
|
|
104
|
+
header: "Last updated",
|
|
105
|
+
cell: (info) => (
|
|
106
|
+
<DataTable.Cell>{info.row.original.lastUpdated}</DataTable.Cell>
|
|
107
|
+
),
|
|
108
|
+
enableSorting: false,
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
accessorKey: "size",
|
|
112
|
+
header: "Size",
|
|
113
|
+
cell: (info) => <DataTable.Cell>{info.row.original.size}</DataTable.Cell>,
|
|
114
|
+
},
|
|
21
115
|
];
|
|
22
|
-
const downloadButton = (
|
|
23
|
-
<Button className="mt-4" size="sm" variant="tertiary" label="Download" />
|
|
24
|
-
);
|
|
25
116
|
|
|
26
117
|
return (
|
|
27
|
-
<
|
|
28
|
-
columns={columns}
|
|
29
|
-
|
|
30
|
-
name="Test table"
|
|
31
|
-
enableCopy={true}
|
|
32
|
-
showLimit={3}
|
|
33
|
-
downloadButton={downloadButton}
|
|
34
|
-
/>
|
|
118
|
+
<div className="s-w-full s-max-w-4xl s-overflow-x-auto">
|
|
119
|
+
<DataTable data={data} columns={columns} />
|
|
120
|
+
</div>
|
|
35
121
|
);
|
|
36
122
|
};
|
|
123
|
+
|
|
124
|
+
export const Default: Story = {
|
|
125
|
+
render: () => <DataTableExample />,
|
|
126
|
+
};
|