|
1
|
import { createDataTableColumnHelper, useDataTable, DataTable, Heading } 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
|
|
|
17
|
const columnHelper = createDataTableColumnHelper<typeof products[0]>() |
|
18
|
|
|
19
|
const columns = [ |
|
20
|
columnHelper.accessor("title", { |
|
21
|
header: "Title", |
|
22
|
enableSorting: true, |
|
23
|
}), |
|
24
|
columnHelper.accessor("price", { |
|
25
|
header: "Price", |
|
26
|
enableSorting: true, |
|
27
|
}), |
|
28
|
] |
|
29
|
|
|
30
|
export default function ProductTable () { |
|
31
|
const [search, setSearch] = useState<string>("") |
|
32
|
|
|
33
|
const shownProducts = useMemo(() => { |
|
34
|
return products.filter((product) => product.title.toLowerCase().includes(search.toLowerCase())) |
|
35
|
}, [search]) |
|
36
|
|
|
37
|
const table = useDataTable({ |
|
38
|
columns, |
|
39
|
data: shownProducts, |
|
40
|
getRowId: (product) => product.id, |
|
41
|
rowCount: products.length, |
|
42
|
isLoading: false, |
|
43
|
// Pass the state and onSearchChange to the table instance. |
|
44
|
search: { |
|
45
|
state: search, |
|
46
|
onSearchChange: setSearch |
|
47
|
} |
|
48
|
}) |
|
49
|
|
|
50
|
return ( |
|
51
|
<DataTable instance={table}> |
|
52
|
<DataTable.Toolbar className="flex flex-col items-start justify-between gap-2 md:flex-row md:items-center"> |
|
53
|
<Heading>Products</Heading> |
|
54
|
{/* This component renders the search bar */} |
|
55
|
<DataTable.Search placeholder="Search..." /> |
|
56
|
</DataTable.Toolbar> |
|
57
|
<DataTable.Table /> |
|
58
|
</DataTable> |
|
59
|
) |
|
60
|
} |