|
1
|
import { DataTable, DataTableRowSelectionState, Heading, createDataTableColumnHelper, createDataTableCommandHelper, useDataTable } from "@medusajs/ui" |
|
2
|
import { useState } from "react" |
|
3
|
|
|
4
|
let 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
|
// Commands requires a select column. |
|
21
|
columnHelper.select(), |
|
22
|
columnHelper.accessor("title", { |
|
23
|
header: "Title", |
|
24
|
enableSorting: true, |
|
25
|
}), |
|
26
|
columnHelper.accessor("price", { |
|
27
|
header: "Price", |
|
28
|
enableSorting: true, |
|
29
|
}), |
|
30
|
] |
|
31
|
|
|
32
|
const commandHelper = createDataTableCommandHelper() |
|
33
|
|
|
34
|
const useCommands = () => { |
|
35
|
return [ |
|
36
|
commandHelper.command({ |
|
37
|
label: "Delete", |
|
38
|
shortcut: "D", |
|
39
|
action: async (selection) => { |
|
40
|
const productsToDeleteIds = Object.keys(selection) |
|
41
|
|
|
42
|
alert(`You deleted product(s) with IDs: ${productsToDeleteIds.join()}`) |
|
43
|
} |
|
44
|
}) |
|
45
|
] |
|
46
|
} |
|
47
|
|
|
48
|
export default function ProductTable () { |
|
49
|
const [rowSelection, setRowSelection] = useState<DataTableRowSelectionState>({}) |
|
50
|
|
|
51
|
const commands = useCommands() |
|
52
|
|
|
53
|
const instance = useDataTable({ |
|
54
|
data: products, |
|
55
|
columns, |
|
56
|
getRowId: (product) => product.id, |
|
57
|
rowCount: products.length, |
|
58
|
isLoading: false, |
|
59
|
commands, |
|
60
|
rowSelection: { |
|
61
|
state: rowSelection, |
|
62
|
onRowSelectionChange: setRowSelection, |
|
63
|
}, |
|
64
|
}); |
|
65
|
|
|
66
|
return ( |
|
67
|
<DataTable instance={instance}> |
|
68
|
<DataTable.Toolbar className="flex justify-between items-center"> |
|
69
|
<Heading>Products</Heading> |
|
70
|
</DataTable.Toolbar> |
|
71
|
<DataTable.Table /> |
|
72
|
{/** This component will the command bar when the user has selected at least one row. **/} |
|
73
|
<DataTable.CommandBar selectedLabel={(count) => `${count} selected`} /> |
|
74
|
</DataTable> |
|
75
|
); |
|
76
|
}; |