Skip to content

Commit d5c68e9

Browse files
committed
[Doc] Improve DataTable custom field doc
1 parent d30278a commit d5c68e9

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

docs/DataTable.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -400,22 +400,28 @@ export const OrderList = () => (
400400
);
401401
```
402402

403-
To render data differently in a column, you can pass children to `<DataTable.Col>`. For example, you can use `<NumberField>` to render a numeric field:
403+
To render data differently in a column, you can pass a custom `<DataTable.Col field>`. For example, you can use [`<NumberField>`](./NumberField.md) to render a numeric field:
404+
405+
```tsx
406+
<DataTable.Col source="amount" align="right" field={NumberField} />
407+
```
408+
409+
If you need to pass props to the custom field, use the `<DataTable.Col children>` instead:
404410

405411
```tsx
406412
<DataTable.Col source="amount" align="right">
407-
<NumberField source="amount" />
413+
<NumberField source="amount" options={{ style: 'currency', currency: 'USD' }} />
408414
</DataTable.Col>
409415
```
410416

411-
`<NumberField>` is a Field component: it reads the record (via `useRecordContext`) and renders a value. React-admin includes many Field components that you can use as children of `<DataTable.Col>` ([`<TextField>`](./TextField.md), [`<NumberField>`](./NumberField.md), [`<DateField>`](./DateField.md), [`<ReferenceField>`](./ReferenceField.md), and many more). Check [the Fields documentation](./Fields.md) for more information.
412-
413417
**Tip**: Rendering numeric values in a table is such a common need that react-admin provides `<DataTable.NumberCol>` just for that:
414418

415419
```tsx
416-
<DataTable.NumberCol source="amount" />
420+
<DataTable.NumberCol source="amount" options={{ style: 'currency', currency: 'USD' }} />
417421
```
418422

423+
`<NumberField>` is a Field component: it reads the record (via `useRecordContext`) and renders a value. React-admin includes many Field components that you can use as `field` or `children` of `<DataTable.Col>` ([`<TextField>`](./TextField.md), [`<NumberField>`](./NumberField.md), [`<DateField>`](./DateField.md), [`<ReferenceField>`](./ReferenceField.md), and many more). Check [the Fields documentation](./Fields.md) for more information.
424+
419425
You can build your own if none of the react-admin Field components fit your need. For example, to render the first name and last name in a cell:
420426

421427
```tsx
@@ -436,9 +442,11 @@ export const OrderList = () => (
436442
<DataTable>
437443
<DataTable.Col source="reference" />
438444
<DataTable.Col source="date" />
439-
<DataTable.Col source="customer.lastName" label="Customer">
440-
<CustomerField />
441-
</DataTable.Col>
445+
<DataTable.Col
446+
source="customer.lastName"
447+
label="Customer"
448+
field={CustomerField}
449+
/>
442450
<DataTable.NumberCol source="amount" />
443451
<DataTable.Col source="status" />
444452
</DataTable>
@@ -909,9 +917,7 @@ export const PostList = () => (
909917
<DataTable.Col label="Author">
910918
<ReferenceField source="author" reference="users" />
911919
</DataTable.Col>
912-
<DataTable.Col source="published_at">
913-
<DateField source="published_at" />
914-
</DataTable.Col>
920+
<DataTable.Col source="published_at" field={DateField} />
915921
<DataTable.Col
916922
label="Summary"
917923
render={record => record.summary.substr(0, 10) + '...'}
@@ -925,8 +931,8 @@ export const PostList = () => (
925931
`<DataTable.Col>` lets you define how the data renders in 4 different ways:
926932
- By passing a `source` prop and no child.
927933
- By passing child elements (e.g. `<ReferenceField>`, `<DateField>`, etc.).
928-
- By passing a `render` prop to define a custom rendering function.
929934
- By using the `field` prop to specify a field component.
935+
- By passing a `render` prop to define a custom rendering function.
930936

931937
### Props
932938

@@ -1019,15 +1025,15 @@ const FullNameField = (props: Props) => {
10191025
textDecorationColor: '#bdbdbd',
10201026
}}
10211027
/>
1022-
{record.first_name} {record.last_name}
1028+
{record.firstName} {record.lastName}
10231029
</Typography>
10241030
);
10251031
};
10261032

10271033
const CustomerList = () => (
10281034
<List>
10291035
<DataTable>
1030-
<DataTable.Col label="Name" source="last_name">
1036+
<DataTable.Col label="Name" source="lastName">
10311037
<FullNameField />
10321038
</DataTable.Col>
10331039
...
@@ -1039,6 +1045,12 @@ const CustomerList = () => (
10391045

10401046
![DataTable Custom Field](./img/DataTableCustomField.png)
10411047

1048+
**Tip**: If you don't need to pass custom props to the field, you can use [the `field` prop](#field) instead:
1049+
1050+
```tsx
1051+
<DataTable.Col label="Name" source="lastName" field={FullNameField} />
1052+
```
1053+
10421054
### `cellClassName`
10431055

10441056
You can pass a custom class name to the cells of a column using the `cellClassName` prop. This class name will not be applied to the header cell.
@@ -1632,9 +1644,7 @@ const ProductList = () => (
16321644
<List>
16331645
<DataTable>
16341646
<CanAccess action="read" resource="products.thumbnail">
1635-
<DataTable.Col source="thumbnail" />
1636-
<ImageField source="thumbnail" />
1637-
</DataTable.Col>
1647+
<DataTable.Col source="thumbnail" field={ImageField} />
16381648
</CanAccess>
16391649
<CanAccess action="read" resource="products.reference">
16401650
<DataTable.Col source="reference" />

0 commit comments

Comments
 (0)