|
1
|
import { faker } from "@faker-js/faker" |
|
2
|
import type { Meta, StoryObj } from "@storybook/react" |
|
3
|
import * as React from "react" |
|
4
|
|
|
5
|
import { Container } from "@/components/container" |
|
6
|
import { PencilSquare, Trash } from "@medusajs/icons" |
|
7
|
import { Button } from "../../components/button" |
|
8
|
import { Heading } from "../../components/heading" |
|
9
|
import { TooltipProvider } from "../../components/tooltip" |
|
10
|
import { DataTable } from "./data-table" |
|
11
|
import { |
|
12
|
DataTableFilteringState, |
|
13
|
DataTablePaginationState, |
|
14
|
DataTableRowSelectionState, |
|
15
|
DataTableSortingState, |
|
16
|
} from "./types" |
|
17
|
import { useDataTable } from "./use-data-table" |
|
18
|
import { createDataTableColumnHelper } from "./utils/create-data-table-column-helper" |
|
19
|
import { createDataTableCommandHelper } from "./utils/create-data-table-command-helper" |
|
20
|
import { createDataTableFilterHelper } from "./utils/create-data-table-filter-helper" |
|
21
|
import { isDateComparisonOperator } from "./utils/is-date-comparison-operator" |
|
22
|
|
|
23
|
const meta: Meta<typeof DataTable> = { |
|
24
|
title: "Blocks/DataTable", |
|
25
|
component: DataTable, |
|
26
|
} |
|
27
|
|
|
28
|
export default meta |
|
29
|
|
|
30
|
type Story = StoryObj<typeof DataTable> |
|
31
|
|
|
32
|
type Employee = { |
|
33
|
id: string |
|
34
|
name: string |
|
35
|
email: string |
|
36
|
position: string |
|
37
|
age: number |
|
38
|
birthday: Date |
|
39
|
relationshipStatus: "single" | "married" | "divorced" | "widowed" |
|
40
|
} |
|
41
|
|
|
42
|
const generateEmployees = (count: number): Employee[] => { |
|
43
|
return Array.from({ length: count }, (_, i) => { |
|
44
|
const age = faker.number.int({ min: 18, max: 65 }) |
|
45
|
const birthday = faker.date.birthdate({ |
|
46
|
mode: "age", |
|
47
|
min: age, |
|
48
|
max: age, |
|
49
|
}) |
|
50
|
|
|
51
|
return { |
|
52
|
id: i.toString(), |
|
53
|
name: faker.person.fullName(), |
|
54
|
email: faker.internet.email(), |
|
55
|
position: faker.person.jobTitle(), |
|
56
|
age, |
|
57
|
birthday, |
|
58
|
relationshipStatus: faker.helpers.arrayElement([ |
|
59
|
"single", |
|
60
|
"married", |
|
61
|
"divorced", |
|
62
|
"widowed", |
|
63
|
]), |
|
64
|
} |
|
65
|
}) |
|
66
|
} |
|
67
|
|
|
68
|
const data: Employee[] = generateEmployees(100) |
|
69
|
|
|
70
|
const usePeople = ({ |
|
71
|
q, |
|
72
|
order, |
|
73
|
filters, |
|
74
|
offset, |
|
75
|
limit, |
|
76
|
}: { |
|
77
|
q?: string |
|
78
|
order?: DataTableSortingState | null |
|
79
|
filters?: DataTableFilteringState |
|
80
|
offset?: number |
|
81
|
limit?: number |
|
82
|
}) => { |
|
83
|
return React.useMemo(() => { |
|
84
|
let results = [...data] |
|
85
|
|
|
86
|
if (q) { |
|
87
|
results = results.filter((person) => |
|
88
|
person.name.toLowerCase().includes(q.toLowerCase()) |
|
89
|
) |
|
90
|
} |
|
91
|
|
|
92
|
if (filters && Object.keys(filters).length > 0) { |
|
93
|
results = results.filter((person) => { |
|
94
|
return Object.entries(filters).every(([key, filter]) => { |
|
95
|
if (!filter) return true |
|
96
|
|
|
97
|
const value = person[key as keyof Employee] |
|
98
|
|
|
99
|
if (key === "birthday") { |
|
100
|
if (isDateComparisonOperator(filter)) { |
|
101
|
if (!(value instanceof Date)) { |
|
102
|
return false |
|
103
|
} |
|
104
|
|
|
105
|
if (filter.$gte) { |
|
106
|
const compareDate = new Date(filter.$gte) |
|
107
|
if (value < compareDate) { |
|
108
|
return false |
|
109
|
} |
|
110
|
} |
|
111
|
|
|
112
|
if (filter.$lte) { |
|
113
|
const compareDate = new Date(filter.$lte) |
|
114
|
if (value > compareDate) { |
|
115
|
return false |
|
116
|
} |
|
117
|
} |
|
118
|
|
|
119
|
if (filter.$gt) { |
|
120
|
const compareDate = new Date(filter.$gt) |
|
121
|
if (value <= compareDate) { |
|
122
|
return false |
|
123
|
} |
|
124
|
} |
|
125
|
|
|
126
|
if (filter.$lt) { |
|
127
|
const compareDate = new Date(filter.$lt) |
|
128
|
if (value >= compareDate) { |
|
129
|
return false |
|
130
|
} |
|
131
|
} |
|
132
|
|
|
133
|
return true |
|
134
|
} |
|
135
|
} |
|
136
|
|
|
137
|
if (Array.isArray(filter)) { |
|
138
|
if (filter.length === 0) return true |
|
139
|
|
|
140
|
return filter.includes(value) |
|
141
|
} |
|
142
|
|
|
143
|
return filter === value |
|
144
|
}) |
|
145
|
}) |
|
146
|
} |
|
147
|
|
|
148
|
// Apply sorting |
|
149
|
if (order) { |
|
150
|
const key = order.id as keyof Employee |
|
151
|
const desc = order.desc |
|
152
|
|
|
153
|
results.sort((a, b) => { |
|
154
|
const aVal = a[key] |
|
155
|
const bVal = b[key] |
|
156
|
|
|
157
|
if (aVal instanceof Date && bVal instanceof Date) { |
|
158
|
return desc |
|
159
|
? bVal.getTime() - aVal.getTime() |
|
160
|
: aVal.getTime() - bVal.getTime() |
|
161
|
} |
|
162
|
|
|
163
|
if (aVal < bVal) return desc ? 1 : -1 |
|
164
|
if (aVal > bVal) return desc ? -1 : 1 |
|
165
|
return 0 |
|
166
|
}) |
|
167
|
} |
|
168
|
|
|
169
|
if (offset) { |
|
170
|
results = results.slice(offset) |
|
171
|
} |
|
172
|
|
|
173
|
if (limit) { |
|
174
|
results = results.slice(0, limit) |
|
175
|
} |
|
176
|
|
|
177
|
return { |
|
178
|
data: results, |
|
179
|
count: data.length, |
|
180
|
} |
|
181
|
}, [q, order, filters, offset, limit]) // Add filters to dependencies |
|
182
|
} |
|
183
|
|
|
184
|
const columnHelper = createDataTableColumnHelper<Employee>() |
|
185
|
|
|
186
|
const columns = [ |
|
187
|
columnHelper.select(), |
|
188
|
columnHelper.accessor("name", { |
|
189
|
header: "Name", |
|
190
|
enableSorting: true, |
|
191
|
sortAscLabel: "A-Z", |
|
192
|
sortDescLabel: "Z-A", |
|
193
|
}), |
|
194
|
columnHelper.accessor("email", { |
|
195
|
header: "Email", |
|
196
|
enableSorting: true, |
|
197
|
sortAscLabel: "A-Z", |
|
198
|
sortDescLabel: "Z-A", |
|
199
|
maxSize: 200, |
|
200
|
}), |
|
201
|
columnHelper.accessor("position", { |
|
202
|
header: "Position", |
|
203
|
enableSorting: true, |
|
204
|
sortAscLabel: "A-Z", |
|
205
|
sortDescLabel: "Z-A", |
|
206
|
}), |
|
207
|
columnHelper.accessor("age", { |
|
208
|
header: "Age", |
|
209
|
enableSorting: true, |
|
210
|
sortAscLabel: "Low to High", |
|
211
|
sortDescLabel: "High to Low", |
|
212
|
sortLabel: "Age", |
|
213
|
}), |
|
214
|
columnHelper.accessor("birthday", { |
|
215
|
header: "Birthday", |
|
216
|
cell: ({ row }) => { |
|
217
|
return ( |
|
218
|
<div> |
|
219
|
{row.original.birthday.toLocaleDateString("en-US", { |
|
220
|
year: "numeric", |
|
221
|
month: "long", |
|
222
|
day: "numeric", |
|
223
|
})} |
|
224
|
</div> |
|
225
|
) |
|
226
|
}, |
|
227
|
enableSorting: true, |
|
228
|
sortAscLabel: "Oldest to Youngest", |
|
229
|
sortDescLabel: "Youngest to Oldest", |
|
230
|
}), |
|
231
|
columnHelper.accessor("relationshipStatus", { |
|
232
|
header: "Relationship Status", |
|
233
|
cell: ({ row }) => { |
|
234
|
return ( |
|
235
|
<div> |
|
236
|
{row.original.relationshipStatus.charAt(0).toUpperCase() + |
|
237
|
row.original.relationshipStatus.slice(1)} |
|
238
|
</div> |
|
239
|
) |
|
240
|
}, |
|
241
|
enableSorting: true, |
|
242
|
sortAscLabel: "A-Z", |
|
243
|
sortDescLabel: "Z-A", |
|
244
|
}), |
|
245
|
columnHelper.action({ |
|
246
|
actions: (ctx) => { |
|
247
|
const actions = [ |
|
248
|
[ |
|
249
|
{ |
|
250
|
label: "Edit", |
|
251
|
onClick: () => { }, |
|
252
|
icon: <PencilSquare />, |
|
253
|
}, |
|
254
|
{ |
|
255
|
label: "Edit", |
|
256
|
onClick: () => { }, |
|
257
|
icon: <PencilSquare />, |
|
258
|
}, |
|
259
|
{ |
|
260
|
label: "Edit", |
|
261
|
onClick: () => { }, |
|
262
|
icon: <PencilSquare />, |
|
263
|
}, |
|
264
|
], |
|
265
|
[ |
|
266
|
{ |
|
267
|
label: "Delete", |
|
268
|
onClick: () => { }, |
|
269
|
icon: <Trash />, |
|
270
|
}, |
|
271
|
], |
|
272
|
] |
|
273
|
|
|
274
|
return actions |
|
275
|
}, |
|
276
|
}), |
|
277
|
] |
|
278
|
|
|
279
|
const filterHelper = createDataTableFilterHelper<Employee>() |
|
280
|
|
|
281
|
const filters = [ |
|
282
|
filterHelper.accessor("birthday", { |
|
283
|
label: "Birthday", |
|
284
|
type: "date", |
|
285
|
format: "date", |
|
286
|
options: [ |
|
287
|
{ |
|
288
|
label: "18 - 25 years old", |
|
289
|
value: { |
|
290
|
$lte: new Date( |
|
291
|
new Date().setFullYear(new Date().getFullYear() - 18) |
|
292
|
).toISOString(), |
|
293
|
$gte: new Date( |
|
294
|
new Date().setFullYear(new Date().getFullYear() - 25) |
|
295
|
).toISOString(), |
|
296
|
}, |
|
297
|
}, |
|
298
|
{ |
|
299
|
label: "26 - 35 years old", |
|
300
|
value: { |
|
301
|
$lte: new Date( |
|
302
|
new Date().setFullYear(new Date().getFullYear() - 26) |
|
303
|
).toISOString(), |
|
304
|
$gte: new Date( |
|
305
|
new Date().setFullYear(new Date().getFullYear() - 35) |
|
306
|
).toISOString(), |
|
307
|
}, |
|
308
|
}, |
|
309
|
{ |
|
310
|
label: "36 - 45 years old", |
|
311
|
value: { |
|
312
|
$lte: new Date( |
|
313
|
new Date().setFullYear(new Date().getFullYear() - 36) |
|
314
|
).toISOString(), |
|
315
|
$gte: new Date( |
|
316
|
new Date().setFullYear(new Date().getFullYear() - 45) |
|
317
|
).toISOString(), |
|
318
|
}, |
|
319
|
}, |
|
320
|
{ |
|
321
|
label: "46 - 55 years old", |
|
322
|
value: { |
|
323
|
$lte: new Date( |
|
324
|
new Date().setFullYear(new Date().getFullYear() - 46) |
|
325
|
).toISOString(), |
|
326
|
$gte: new Date( |
|
327
|
new Date().setFullYear(new Date().getFullYear() - 55) |
|
328
|
).toISOString(), |
|
329
|
}, |
|
330
|
}, |
|
331
|
{ |
|
332
|
label: "Over 55 years old", |
|
333
|
value: { |
|
334
|
$lt: new Date( |
|
335
|
new Date().setFullYear(new Date().getFullYear() - 55) |
|
336
|
).toISOString(), |
|
337
|
}, |
|
338
|
}, |
|
339
|
], |
|
340
|
}), |
|
341
|
filterHelper.accessor("relationshipStatus", { |
|
342
|
label: "Relationship Status", |
|
343
|
type: "select", |
|
344
|
options: [ |
|
345
|
{ label: "Single", value: "single" }, |
|
346
|
{ label: "Married", value: "married" }, |
|
347
|
{ label: "Divorced", value: "divorced" }, |
|
348
|
{ label: "Widowed", value: "widowed" }, |
|
349
|
], |
|
350
|
}), |
|
351
|
] |
|
352
|
|
|
353
|
const commandHelper = createDataTableCommandHelper() |
|
354
|
|
|
355
|
const commands = [ |
|
356
|
commandHelper.command({ |
|
357
|
label: "Archive", |
|
358
|
action: (selection) => { |
|
359
|
alert(`Archive ${Object.keys(selection).length} items`) |
|
360
|
}, |
|
361
|
shortcut: "A", |
|
362
|
}), |
|
363
|
commandHelper.command({ |
|
364
|
label: "Delete", |
|
365
|
action: (selection) => { |
|
366
|
alert(`Delete ${Object.keys(selection).length} items`) |
|
367
|
}, |
|
368
|
shortcut: "D", |
|
369
|
}), |
|
370
|
] |
|
371
|
|
|
372
|
const KitchenSinkDemo = () => { |
|
373
|
const [search, setSearch] = React.useState("") |
|
374
|
|
|
375
|
const [rowSelection, setRowSelection] = |
|
376
|
React.useState<DataTableRowSelectionState>({}) |
|
377
|
const [sorting, setSorting] = React.useState<DataTableSortingState | null>( |
|
378
|
null |
|
379
|
) |
|
380
|
const [filtering, setFiltering] = React.useState<DataTableFilteringState>({ |
|
381
|
birthday: { |
|
382
|
$gte: new Date( |
|
383
|
new Date().setFullYear(new Date().getFullYear() - 18) |
|
384
|
).toISOString(), |
|
385
|
}, |
|
386
|
}) |
|
387
|
|
|
388
|
const handleFilteringChange = ( |
|
389
|
state: DataTableFilteringState, |
|
390
|
) => { |
|
391
|
console.log("Filtering changed:", state) |
|
392
|
setFiltering(state) |
|
393
|
} |
|
394
|
|
|
395
|
const [pagination, setPagination] = React.useState<DataTablePaginationState>({ |
|
396
|
pageIndex: 0, |
|
397
|
pageSize: 10, |
|
398
|
}) |
|
399
|
|
|
400
|
const { data, count } = usePeople({ |
|
401
|
q: search, |
|
402
|
order: sorting, |
|
403
|
filters: filtering, |
|
404
|
offset: pagination.pageIndex * pagination.pageSize, |
|
405
|
limit: pagination.pageSize, |
|
406
|
}) |
|
407
|
|
|
408
|
const table = useDataTable({ |
|
409
|
data, |
|
410
|
columns, |
|
411
|
filters, |
|
412
|
commands, |
|
413
|
rowCount: count, |
|
414
|
getRowId: (row) => row.id, |
|
415
|
onRowClick: (_event, row) => { |
|
416
|
alert(`Navigate to ${row.id}`) |
|
417
|
}, |
|
418
|
search: { |
|
419
|
state: search, |
|
420
|
onSearchChange: setSearch, |
|
421
|
}, |
|
422
|
filtering: { |
|
423
|
state: filtering, |
|
424
|
onFilteringChange: handleFilteringChange, |
|
425
|
}, |
|
426
|
rowSelection: { |
|
427
|
state: rowSelection, |
|
428
|
onRowSelectionChange: setRowSelection, |
|
429
|
enableRowSelection: (row) => Number(row.original.id) > 4, |
|
430
|
}, |
|
431
|
sorting: { |
|
432
|
state: sorting, |
|
433
|
onSortingChange: setSorting, |
|
434
|
}, |
|
435
|
pagination: { |
|
436
|
state: pagination, |
|
437
|
onPaginationChange: setPagination, |
|
438
|
}, |
|
439
|
}) |
|
440
|
|
|
441
|
return ( |
|
442
|
<TooltipProvider> |
|
443
|
<Container className="flex flex-col overflow-hidden p-0"> |
|
444
|
<DataTable instance={table}> |
|
445
|
<DataTable.Toolbar className="flex flex-col items-start justify-between gap-2 md:flex-row md:items-center"> |
|
446
|
<Heading>Employees</Heading> |
|
447
|
<div className="flex w-full items-center gap-2 md:w-auto"> |
|
448
|
<DataTable.Search placeholder="Search" autoFocus /> |
|
449
|
<DataTable.FilterMenu tooltip="Filter" /> |
|
450
|
<DataTable.SortingMenu tooltip="Sort" /> |
|
451
|
<Button size="small" variant="secondary"> |
|
452
|
Create |
|
453
|
</Button> |
|
454
|
</div> |
|
455
|
</DataTable.Toolbar> |
|
456
|
<DataTable.Table |
|
457
|
emptyState={{ |
|
458
|
empty: { |
|
459
|
heading: "No employees", |
|
460
|
description: "There are no employees to display.", |
|
461
|
}, |
|
462
|
filtered: { |
|
463
|
heading: "No results", |
|
464
|
description: |
|
465
|
"No employees match the current filter criteria. Try adjusting your filters.", |
|
466
|
}, |
|
467
|
}} |
|
468
|
/> |
|
469
|
<DataTable.Pagination /> |
|
470
|
<DataTable.CommandBar |
|
471
|
selectedLabel={(count) => `${count} selected`} |
|
472
|
/> |
|
473
|
</DataTable> |
|
474
|
</Container> |
|
475
|
</TooltipProvider> |
|
476
|
) |
|
477
|
} |
|
478
|
|
|
479
|
export const KitchenSink: Story = { |
|
480
|
render: () => <KitchenSinkDemo />, |
|
481
|
} |