|
1
|
--- |
|
2
|
sidebar_label: "Configurable Data Table" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `ConfigurableDataTable - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
- This component is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). |
|
16
|
- This component requires the [view configuration feature](../../../commerce-modules/settings/view-configurations/page.mdx#enable-the-feature-flag) to be enabled. |
|
17
|
|
|
18
|
</Note> |
|
19
|
|
|
20
|
The `ConfigurableDataTable` component shows a [data table](!ui!/components/data-table) whose columns, filters, and saved views the user configures from the Medusa Admin. The Medusa Admin uses it on list pages that support view configurations, such as the products, customers, and regions lists. |
|
21
|
|
|
22
|
Use the `ConfigurableDataTable` component when you have the view configuration feature enabled and want to provide a customizable table experience for your users. |
|
23
|
|
|
24
|
```tsx |
|
25
|
import { |
|
26
|
ConfigurableDataTable, |
|
27
|
} from "@medusajs/dashboard/components" |
|
28
|
``` |
|
29
|
|
|
30
|
Unlike `DataTable`, you don't pass columns or data to `ConfigurableDataTable`. Instead, you pass an adapter that tells the component which entity to show and how to fetch its records. The component then resolves the columns and filters from the Medusa backend and applies the user's saved view. |
|
31
|
|
|
32
|
--- |
|
33
|
|
|
34
|
## Example |
|
35
|
|
|
36
|
Create the adapter with the `createTableAdapter` utility, then pass it to the component: |
|
37
|
|
|
38
|
```tsx title="src/admin/routes/brands/page.tsx" |
|
39
|
import { |
|
40
|
ConfigurableDataTable, |
|
41
|
} from "@medusajs/dashboard/components" |
|
42
|
import { createTableAdapter } from "@medusajs/dashboard/lib" |
|
43
|
import { useBrands } from "../../hooks/brands" |
|
44
|
|
|
45
|
const adapter = createTableAdapter<Brand>({ |
|
46
|
entity: "brands", |
|
47
|
entityName: "Brands", |
|
48
|
useData: (fields, params) => { |
|
49
|
const { brands, count, isLoading, isError, error } = |
|
50
|
useBrands({ fields, ...params }) |
|
51
|
|
|
52
|
return { data: brands, count, isLoading, isError, error } |
|
53
|
}, |
|
54
|
getRowHref: (row) => `/brands/${row.id}`, |
|
55
|
}) |
|
56
|
|
|
57
|
const BrandsPage = () => { |
|
58
|
return ( |
|
59
|
<ConfigurableDataTable |
|
60
|
adapter={adapter} |
|
61
|
heading="Brands" |
|
62
|
subHeading="Manage your brands" |
|
63
|
actions={[{ label: "Create", to: "create" }]} |
|
64
|
/> |
|
65
|
) |
|
66
|
} |
|
67
|
|
|
68
|
export default BrandsPage |
|
69
|
``` |
|
70
|
|
|
71
|
--- |
|
72
|
|
|
73
|
## Type Argument |
|
74
|
|
|
75
|
`ConfigurableDataTable` accepts one type argument, `TData`, which is the type of a single row in the table. |
|
76
|
|
|
77
|
You set it on the adapter rather than on the component. `createTableAdapter` infers `TData` from the `data` that your `useData` hook returns, and the component picks it up from the `adapter` prop: |
|
78
|
|
|
79
|
```tsx |
|
80
|
import { createTableAdapter } from "@medusajs/dashboard/lib" |
|
81
|
import { useProducts } from "@medusajs/dashboard/hooks" |
|
82
|
|
|
83
|
const adapter = createTableAdapter({ |
|
84
|
entity: "products", |
|
85
|
useData: (fields, params) => { |
|
86
|
const { products, count, isLoading, isError, error } = |
|
87
|
useProducts({ fields, ...params }) |
|
88
|
|
|
89
|
return { data: products, count, isLoading, isError, error } |
|
90
|
}, |
|
91
|
}) |
|
92
|
``` |
|
93
|
|
|
94
|
`TData` is now `HttpTypes.AdminProduct`, so the adapter's other functions receive typed rows: |
|
95
|
|
|
96
|
```tsx |
|
97
|
const adapter = createTableAdapter({ |
|
98
|
entity: "products", |
|
99
|
useData: (fields, params) => { |
|
100
|
// ... |
|
101
|
}, |
|
102
|
getRowId: (product) => product.id, |
|
103
|
getRowHref: (product) => `/products/${product.id}`, |
|
104
|
}) |
|
105
|
``` |
|
106
|
|
|
107
|
To set the type argument explicitly, pass it to `createTableAdapter`. This is useful when your hook's return type is wider than the rows you show: |
|
108
|
|
|
109
|
```tsx |
|
110
|
const adapter = createTableAdapter<Brand>({ |
|
111
|
entity: "brands", |
|
112
|
useData: (fields, params) => { |
|
113
|
// ... |
|
114
|
}, |
|
115
|
}) |
|
116
|
``` |
|
117
|
|
|
118
|
--- |
|
119
|
|
|
120
|
## Props |
|
121
|
|
|
122
|
The component accepts the `ConfigurableDataTableProps` type: |
|
123
|
|
|
124
|
<TypeList |
|
125
|
types={[ |
|
126
|
{ |
|
127
|
name: "adapter", |
|
128
|
type: "`TableAdapter`", |
|
129
|
optional: false, |
|
130
|
description: `The adapter that defines the table's entity, data fetching, and behavior. Create it with [createTableAdapter](../../utilities/table/page.mdx#createtableadapter), which also sets the [\`TData\` type argument](#type-argument). Refer to the [utility's documentation](../../utilities/table/page.mdx#createtableadapter) for the adapter's properties.`, |
|
131
|
}, |
|
132
|
{ |
|
133
|
name: "heading", |
|
134
|
type: "`string`", |
|
135
|
optional: true, |
|
136
|
description: "The table's heading. If you don't pass it, the component uses the adapter's `entityName`.", |
|
137
|
}, |
|
138
|
{ |
|
139
|
name: "subHeading", |
|
140
|
type: "`string`", |
|
141
|
optional: true, |
|
142
|
description: "A subheading shown under the heading.", |
|
143
|
}, |
|
144
|
{ |
|
145
|
name: "pageSize", |
|
146
|
type: "`number`", |
|
147
|
optional: true, |
|
148
|
description: "The number of records per page. If you don't pass it, the component uses the adapter's `pageSize`, which defaults to `20`.", |
|
149
|
}, |
|
150
|
{ |
|
151
|
name: "queryPrefix", |
|
152
|
type: "`string`", |
|
153
|
optional: true, |
|
154
|
description: "A prefix for the table's URL query parameters. If you don't pass it, the component uses the adapter's `queryPrefix`.", |
|
155
|
}, |
|
156
|
{ |
|
157
|
name: "layout", |
|
158
|
type: "`\"fill\"` \\| `\"auto\"`", |
|
159
|
optional: true, |
|
160
|
defaultValue: "fill", |
|
161
|
description: "Whether the table fills the available height.", |
|
162
|
}, |
|
163
|
{ |
|
164
|
name: "actions", |
|
165
|
type: "`object[]`", |
|
166
|
optional: true, |
|
167
|
description: "The buttons shown next to the table's heading.", |
|
168
|
children: [ |
|
169
|
{ |
|
170
|
name: "label", |
|
171
|
type: "`string`", |
|
172
|
optional: false, |
|
173
|
description: "The button's label.", |
|
174
|
}, |
|
175
|
{ |
|
176
|
name: "disabled", |
|
177
|
type: "`boolean`", |
|
178
|
optional: true, |
|
179
|
description: "Whether the button is disabled.", |
|
180
|
}, |
|
181
|
{ |
|
182
|
name: "to", |
|
183
|
type: "`string`", |
|
184
|
optional: true, |
|
185
|
description: "The path to navigate to when the user clicks the button. Pass either `to` or `onClick`, not both.", |
|
186
|
}, |
|
187
|
{ |
|
188
|
name: "onClick", |
|
189
|
type: "`() => void`", |
|
190
|
optional: true, |
|
191
|
description: "A function that runs when the user clicks the button. Pass either `to` or `onClick`, not both.", |
|
192
|
}, |
|
193
|
], |
|
194
|
}, |
|
195
|
]} |
|
196
|
/> |