|
1
|
import { DataTable, Heading, createDataTableColumnHelper, useDataTable, type DataTablePaginationState } from "@medusajs/ui" |
|
2
|
import { useMemo, useState } from "react" |
|
3
|
|
|
4
|
const products = [ |
|
5
|
{ |
|
6
|
id: "1", |
|
7
|
title: "Shirt", |
|
8
|
price: 10 |
|
9
|
}, |
|
10
|
{ |
|
11
|
id: "2", |
|
12
|
title: "Pants", |
|
13
|
price: 20 |
|
14
|
}, |
|
15
|
{ |
|
16
|
id: "3", |
|
17
|
title: "Hat", |
|
18
|
price: 15 |
|
19
|
}, |
|
20
|
{ |
|
21
|
id: "4", |
|
22
|
title: "Socks", |
|
23
|
price: 5 |
|
24
|
}, |
|
25
|
{ |
|
26
|
id: "5", |
|
27
|
title: "Shoes", |
|
28
|
price: 50 |
|
29
|
}, |
|
30
|
{ |
|
31
|
id: "6", |
|
32
|
title: "Jacket", |
|
33
|
price: 100 |
|
34
|
}, |
|
35
|
{ |
|
36
|
id: "7", |
|
37
|
title: "Scarf", |
|
38
|
price: 25 |
|
39
|
}, |
|
40
|
{ |
|
41
|
id: "8", |
|
42
|
title: "Gloves", |
|
43
|
price: 12 |
|
44
|
}, |
|
45
|
{ |
|
46
|
id: "9", |
|
47
|
title: "Belt", |
|
48
|
price: 18 |
|
49
|
}, |
|
50
|
{ |
|
51
|
id: "10", |
|
52
|
title: "Sunglasses", |
|
53
|
price: 30 |
|
54
|
}, |
|
55
|
{ |
|
56
|
id: "11", |
|
57
|
title: "Watch", |
|
58
|
price: 200 |
|
59
|
}, |
|
60
|
{ |
|
61
|
id: "12", |
|
62
|
title: "Tie", |
|
63
|
price: 20 |
|
64
|
}, |
|
65
|
{ |
|
66
|
id: "13", |
|
67
|
title: "Sweater", |
|
68
|
price: 40 |
|
69
|
}, |
|
70
|
{ |
|
71
|
id: "14", |
|
72
|
title: "Jeans", |
|
73
|
price: 60 |
|
74
|
}, |
|
75
|
{ |
|
76
|
id: "15", |
|
77
|
title: "Shorts", |
|
78
|
price: 25 |
|
79
|
}, |
|
80
|
{ |
|
81
|
id: "16", |
|
82
|
title: "Blouse", |
|
83
|
price: 35 |
|
84
|
}, |
|
85
|
{ |
|
86
|
id: "17", |
|
87
|
title: "Dress", |
|
88
|
price: 80 |
|
89
|
} |
|
90
|
] |
|
91
|
|
|
92
|
const columnHelper = createDataTableColumnHelper<typeof products[0]>() |
|
93
|
|
|
94
|
const columns = [ |
|
95
|
columnHelper.accessor("title", { |
|
96
|
header: "Title", |
|
97
|
enableSorting: true, |
|
98
|
}), |
|
99
|
columnHelper.accessor("price", { |
|
100
|
header: "Price", |
|
101
|
enableSorting: true, |
|
102
|
}), |
|
103
|
] |
|
104
|
|
|
105
|
const PAGE_SIZE = 10; |
|
106
|
|
|
107
|
export default function ProductTable () { |
|
108
|
const [pagination, setPagination] = useState<DataTablePaginationState>({ |
|
109
|
pageSize: PAGE_SIZE, |
|
110
|
pageIndex: 0, |
|
111
|
}) |
|
112
|
|
|
113
|
const shownProducts = useMemo(() => { |
|
114
|
return products.slice( |
|
115
|
pagination.pageIndex * pagination.pageSize, |
|
116
|
(pagination.pageIndex + 1) * pagination.pageSize |
|
117
|
) |
|
118
|
}, [pagination]) |
|
119
|
|
|
120
|
const table = useDataTable({ |
|
121
|
data: shownProducts, |
|
122
|
columns, |
|
123
|
rowCount: products.length, |
|
124
|
getRowId: (product) => product.id, |
|
125
|
pagination: { |
|
126
|
// Pass the pagination state and updater to the table instance |
|
127
|
state: pagination, |
|
128
|
onPaginationChange: setPagination, |
|
129
|
}, |
|
130
|
isLoading: false, |
|
131
|
}); |
|
132
|
|
|
133
|
return ( |
|
134
|
<DataTable instance={table}> |
|
135
|
<DataTable.Toolbar> |
|
136
|
<Heading>Products</Heading> |
|
137
|
</DataTable.Toolbar> |
|
138
|
<DataTable.Table /> |
|
139
|
{/** This component will render the pagination controls **/} |
|
140
|
<DataTable.Pagination /> |
|
141
|
</DataTable> |
|
142
|
); |
|
143
|
}; |