Raw
1 import { Table } from "@medusajs/ui"
2 import { useMemo, useState } from "react"
3
4 type Order = {
5 id: string
6 displayId: number
7 customer: string
8 email: string
9 amount: number
10 currency: string
11 }
12
13 export default function TableDemo() {
14 const fakeData: Order[] = useMemo(
15 () => [
16 {
17 id: "order_6782",
18 displayId: 86078,
19 customer: "Jill Miller",
20 email: "32690@gmail.com",
21 amount: 493,
22 currency: "EUR",
23 },
24 {
25 id: "order_46487",
26 displayId: 42845,
27 customer: "Sarah Garcia",
28 email: "86379@gmail.com",
29 amount: 113,
30 currency: "JPY",
31 },
32 {
33 id: "order_8169",
34 displayId: 39129,
35 customer: "Josef Smith",
36 email: "89383@gmail.com",
37 amount: 43,
38 currency: "USD",
39 },
40 {
41 id: "order_67883",
42 displayId: 5548,
43 customer: "Elvis Jones",
44 email: "52860@gmail.com",
45 amount: 840,
46 currency: "GBP",
47 },
48 {
49 id: "order_61121",
50 displayId: 87668,
51 customer: "Charles Rodriguez",
52 email: "45675@gmail.com",
53 amount: 304,
54 currency: "GBP",
55 },
56 ],
57 []
58 )
59 const [currentPage, setCurrentPage] = useState(0)
60 const pageSize = 3
61 const pageCount = Math.ceil(fakeData.length / pageSize)
62 const canNextPage = useMemo(
63 () => currentPage < pageCount - 1,
64 [currentPage, pageCount]
65 )
66 const canPreviousPage = useMemo(() => currentPage - 1 >= 0, [currentPage])
67
68 const nextPage = () => {
69 if (canNextPage) {
70 setCurrentPage(currentPage + 1)
71 }
72 }
73
74 const previousPage = () => {
75 if (canPreviousPage) {
76 setCurrentPage(currentPage - 1)
77 }
78 }
79
80 const currentOrders = useMemo(() => {
81 const offset = currentPage * pageSize
82 const limit = Math.min(offset + pageSize, fakeData.length)
83
84 return fakeData.slice(offset, limit)
85 }, [currentPage, pageSize, fakeData])
86
87 return (
88 <div className="flex gap-1 flex-col">
89 <Table>
90 <Table.Header>
91 <Table.Row>
92 <Table.HeaderCell>#</Table.HeaderCell>
93 <Table.HeaderCell>Customer</Table.HeaderCell>
94 <Table.HeaderCell>Email</Table.HeaderCell>
95 <Table.HeaderCell className="text-right">Amount</Table.HeaderCell>
96 <Table.HeaderCell></Table.HeaderCell>
97 </Table.Row>
98 </Table.Header>
99 <Table.Body>
100 {currentOrders.map((order) => {
101 return (
102 <Table.Row
103 key={order.id}
104 className="[&_td:last-child]:w-[1%] [&_td:last-child]:whitespace-nowrap"
105 >
106 <Table.Cell>{order.displayId}</Table.Cell>
107 <Table.Cell>{order.customer}</Table.Cell>
108 <Table.Cell>{order.email}</Table.Cell>
109 <Table.Cell className="text-right">
110 {new Intl.NumberFormat("en-US", {
111 style: "currency",
112 currency: order.currency,
113 }).format(order.amount)}
114 </Table.Cell>
115 <Table.Cell className="text-ui-fg-muted">
116 {order.currency}
117 </Table.Cell>
118 </Table.Row>
119 )
120 })}
121 </Table.Body>
122 </Table>
123 <Table.Pagination
124 count={fakeData.length}
125 pageSize={pageSize}
126 pageIndex={currentPage}
127 pageCount={fakeData.length}
128 canPreviousPage={canPreviousPage}
129 canNextPage={canNextPage}
130 previousPage={previousPage}
131 nextPage={nextPage}
132 />
133 </div>
134 )
135 }