|
1
|
import type { Meta, StoryObj } from "@storybook/react" |
|
2
|
import * as React from "react" |
|
3
|
|
|
4
|
import { Table } from "./table" |
|
5
|
|
|
6
|
const meta: Meta<typeof Table> = { |
|
7
|
title: "Components/Table", |
|
8
|
component: Table, |
|
9
|
parameters: { |
|
10
|
layout: "centered", |
|
11
|
}, |
|
12
|
} |
|
13
|
|
|
14
|
export default meta |
|
15
|
|
|
16
|
type Story = StoryObj<typeof Table> |
|
17
|
|
|
18
|
type Order = { |
|
19
|
id: string |
|
20
|
displayId: number |
|
21
|
customer: string |
|
22
|
email: string |
|
23
|
amount: number |
|
24
|
currency: string |
|
25
|
} |
|
26
|
|
|
27
|
const firstNames = [ |
|
28
|
"Charles", |
|
29
|
"Cooper", |
|
30
|
"Johhny", |
|
31
|
"Elvis", |
|
32
|
"John", |
|
33
|
"Jane", |
|
34
|
"Joe", |
|
35
|
"Jack", |
|
36
|
"Jill", |
|
37
|
"Jenny", |
|
38
|
] |
|
39
|
const lastNames = [ |
|
40
|
"Brown", |
|
41
|
"Smith", |
|
42
|
"Johnson", |
|
43
|
"Williams", |
|
44
|
"Jones", |
|
45
|
"Miller", |
|
46
|
"Davis", |
|
47
|
"Garcia", |
|
48
|
"Rodriguez", |
|
49
|
"Wilson", |
|
50
|
] |
|
51
|
const currencies = ["USD", "EUR", "GBP", "JPY"] |
|
52
|
|
|
53
|
function makeDate(x: number): Order[] { |
|
54
|
// get random name |
|
55
|
const getRandomName = () => { |
|
56
|
const firstName = firstNames[Math.floor(Math.random() * firstNames.length)] |
|
57
|
const lastName = lastNames[Math.floor(Math.random() * lastNames.length)] |
|
58
|
return `${firstName} ${lastName}` |
|
59
|
} |
|
60
|
|
|
61
|
const getRandomId = () => { |
|
62
|
return `order_${Math.floor(Math.random() * 100000)}` |
|
63
|
} |
|
64
|
|
|
65
|
const getRandomDisplayId = () => { |
|
66
|
return Math.floor(Math.random() * 100000) |
|
67
|
} |
|
68
|
|
|
69
|
const getRandomAmount = () => { |
|
70
|
return Math.floor(Math.random() * 1000) |
|
71
|
} |
|
72
|
|
|
73
|
const getRandomCurrency = () => { |
|
74
|
return currencies[Math.floor(Math.random() * currencies.length)] |
|
75
|
} |
|
76
|
|
|
77
|
const getRandomEmail = () => { |
|
78
|
return `${Math.floor(Math.random() * 100000)}@gmail.com` |
|
79
|
} |
|
80
|
|
|
81
|
// Create x random orders and resolve them after 1 second |
|
82
|
const orders = Array.from({ length: x }, () => ({ |
|
83
|
id: getRandomId(), |
|
84
|
displayId: getRandomDisplayId(), |
|
85
|
customer: getRandomName(), |
|
86
|
email: getRandomEmail(), |
|
87
|
amount: getRandomAmount(), |
|
88
|
currency: getRandomCurrency(), |
|
89
|
})) |
|
90
|
|
|
91
|
return orders |
|
92
|
} |
|
93
|
|
|
94
|
type UseFakeOrdersProps = { |
|
95
|
limit: number |
|
96
|
offset: number |
|
97
|
} |
|
98
|
|
|
99
|
const useFakeOrders = ({ offset, limit }: UseFakeOrdersProps) => { |
|
100
|
const COUNT = 1000 |
|
101
|
|
|
102
|
const [orders, setOrders] = React.useState<Order[]>(makeDate(limit)) |
|
103
|
const [offsetState, setOffsetState] = React.useState<number | undefined>(0) |
|
104
|
const [isLoading, setIsLoading] = React.useState(false) |
|
105
|
|
|
106
|
// Fake API call |
|
107
|
React.useEffect(() => { |
|
108
|
const fetchOrders = async () => { |
|
109
|
if (offset === offsetState) { |
|
110
|
return |
|
111
|
} |
|
112
|
|
|
113
|
await new Promise((resolve) => setTimeout(resolve, 500)) |
|
114
|
|
|
115
|
if (offset > COUNT) { |
|
116
|
return |
|
117
|
} |
|
118
|
|
|
119
|
const newOrders = makeDate(limit) |
|
120
|
|
|
121
|
setOrders(newOrders) |
|
122
|
|
|
123
|
setOffsetState(offset) |
|
124
|
} |
|
125
|
|
|
126
|
setIsLoading(true) |
|
127
|
|
|
128
|
fetchOrders().then(() => { |
|
129
|
setIsLoading(false) |
|
130
|
}) |
|
131
|
}, [offset, limit, orders, offsetState]) |
|
132
|
|
|
133
|
return { |
|
134
|
orders, |
|
135
|
isLoading, |
|
136
|
count: COUNT, |
|
137
|
} |
|
138
|
} |
|
139
|
|
|
140
|
const fakeData = makeDate(10) |
|
141
|
|
|
142
|
console.log(JSON.stringify(fakeData, null, 2)) |
|
143
|
|
|
144
|
const formatCurrency = (amount: number, currency: string) => { |
|
145
|
return new Intl.NumberFormat("en-US", { |
|
146
|
style: "currency", |
|
147
|
currency, |
|
148
|
signDisplay: "always", |
|
149
|
}).format(amount) |
|
150
|
} |
|
151
|
|
|
152
|
export const Default: Story = { |
|
153
|
render: () => { |
|
154
|
return ( |
|
155
|
<div className="flex w-[80vw] items-center justify-center"> |
|
156
|
<Table> |
|
157
|
<Table.Header> |
|
158
|
<Table.Row> |
|
159
|
<Table.HeaderCell>#</Table.HeaderCell> |
|
160
|
<Table.HeaderCell>Customer</Table.HeaderCell> |
|
161
|
<Table.HeaderCell>Email</Table.HeaderCell> |
|
162
|
<Table.HeaderCell className="text-right">Amount</Table.HeaderCell> |
|
163
|
<Table.HeaderCell></Table.HeaderCell> |
|
164
|
</Table.Row> |
|
165
|
</Table.Header> |
|
166
|
<Table.Body> |
|
167
|
{fakeData.map((order) => { |
|
168
|
return ( |
|
169
|
<Table.Row |
|
170
|
key={order.id} |
|
171
|
className="[&_td:last-child]:w-[1%] [&_td:last-child]:whitespace-nowrap" |
|
172
|
> |
|
173
|
<Table.Cell>{order.displayId}</Table.Cell> |
|
174
|
<Table.Cell>{order.customer}</Table.Cell> |
|
175
|
<Table.Cell>{order.email}</Table.Cell> |
|
176
|
<Table.Cell className="text-right"> |
|
177
|
{formatCurrency(order.amount, order.currency)} |
|
178
|
</Table.Cell> |
|
179
|
<Table.Cell className="text-ui-fg-muted"> |
|
180
|
{order.currency} |
|
181
|
</Table.Cell> |
|
182
|
</Table.Row> |
|
183
|
) |
|
184
|
})} |
|
185
|
</Table.Body> |
|
186
|
</Table> |
|
187
|
</div> |
|
188
|
) |
|
189
|
}, |
|
190
|
} |
|
191
|
|
|
192
|
const PaginatedDemo = () => { |
|
193
|
const [pageIndex, setPageIndex] = React.useState(0) |
|
194
|
const pageSize = 10 |
|
195
|
|
|
196
|
const { orders, isLoading, count } = useFakeOrders({ |
|
197
|
offset: pageIndex * pageSize, |
|
198
|
limit: pageSize, |
|
199
|
}) |
|
200
|
|
|
201
|
const pageCount = Math.ceil(count / pageSize) |
|
202
|
|
|
203
|
const canNextPage = pageIndex < pageCount - 1 && !isLoading |
|
204
|
const canPreviousPage = pageIndex > 0 && !isLoading |
|
205
|
|
|
206
|
const nextPage = () => { |
|
207
|
if (canNextPage) { |
|
208
|
setPageIndex(pageIndex + 1) |
|
209
|
} |
|
210
|
} |
|
211
|
|
|
212
|
const previousPage = () => { |
|
213
|
if (canPreviousPage) { |
|
214
|
setPageIndex(pageIndex - 1) |
|
215
|
} |
|
216
|
} |
|
217
|
|
|
218
|
return ( |
|
219
|
<div className="flex w-[80vw] flex-col items-center justify-center"> |
|
220
|
<Table> |
|
221
|
<Table.Header> |
|
222
|
<Table.Row> |
|
223
|
<Table.HeaderCell>#</Table.HeaderCell> |
|
224
|
<Table.HeaderCell>Customer</Table.HeaderCell> |
|
225
|
<Table.HeaderCell>Email</Table.HeaderCell> |
|
226
|
<Table.HeaderCell className="text-right">Amount</Table.HeaderCell> |
|
227
|
<Table.HeaderCell></Table.HeaderCell> |
|
228
|
</Table.Row> |
|
229
|
</Table.Header> |
|
230
|
<Table.Body> |
|
231
|
{orders.map((order) => { |
|
232
|
return ( |
|
233
|
<Table.Row |
|
234
|
key={order.id} |
|
235
|
className="[&_td:last-child]:w-[1%] [&_td:last-child]:whitespace-nowrap" |
|
236
|
> |
|
237
|
<Table.Cell>{order.displayId}</Table.Cell> |
|
238
|
<Table.Cell>{order.customer}</Table.Cell> |
|
239
|
<Table.Cell>{order.email}</Table.Cell> |
|
240
|
<Table.Cell className="text-right">{order.amount}</Table.Cell> |
|
241
|
<Table.Cell className="text-ui-fg-muted"> |
|
242
|
{order.currency} |
|
243
|
</Table.Cell> |
|
244
|
</Table.Row> |
|
245
|
) |
|
246
|
})} |
|
247
|
</Table.Body> |
|
248
|
</Table> |
|
249
|
<Table.Pagination |
|
250
|
pageCount={pageCount} |
|
251
|
canNextPage={canNextPage} |
|
252
|
canPreviousPage={canPreviousPage} |
|
253
|
count={count} |
|
254
|
pageSize={pageSize} |
|
255
|
pageIndex={pageIndex} |
|
256
|
nextPage={nextPage} |
|
257
|
previousPage={previousPage} |
|
258
|
/> |
|
259
|
<div className="mt-12 flex flex-col items-center gap-y-4 font-mono text-xs"> |
|
260
|
<div className="flex items-center gap-x-4"> |
|
261
|
<p>Page Index: {pageIndex}</p> |
|
262
|
<p>Page Count: {pageCount}</p> |
|
263
|
<p>Count: {count}</p> |
|
264
|
<p>Page Size: {pageSize}</p> |
|
265
|
</div> |
|
266
|
<div className="flex items-center gap-x-4"> |
|
267
|
<p>Can Next Page: {canNextPage ? "true" : "false"}</p> |
|
268
|
<p>Can Previous Page: {canPreviousPage ? "true" : "false"}</p> |
|
269
|
<p>Is Loading: {isLoading ? "true" : "false"}</p> |
|
270
|
</div> |
|
271
|
</div> |
|
272
|
</div> |
|
273
|
) |
|
274
|
} |
|
275
|
|
|
276
|
export const Paginated: Story = { |
|
277
|
render: () => { |
|
278
|
return <PaginatedDemo /> |
|
279
|
}, |
|
280
|
} |