Skip to content

Commit ab1a933

Browse files
authored
Merge pull request #7799 from marmelab/doc-theming-conditinoal-formatting
[Doc] Fix Theming doc uses outdated syntax for conditional formatting example
2 parents 2e17a92 + eb9eb3e commit ab1a933

File tree

1 file changed

+10
-59
lines changed

1 file changed

+10
-59
lines changed

docs/Theming.md

+10-59
Original file line numberDiff line numberDiff line change
@@ -398,73 +398,24 @@ const ThemeToggler = () => {
398398

399399
## Conditional Formatting
400400

401-
Sometimes you want the format to depend on the value. The following example shows how to create a new custom `NumberField` component which highlight its text in red when its value is 100 or higher.
401+
Sometimes you want the format to depend on the value. Use `useRecordContext` to grab the record in a component, and the `sx` prop to apply the format.
402402

403-
{% raw %}
404-
```jsx
405-
import * as React from 'react';
406-
import { NumberField, List, Datagrid, TextField, EditButton } from 'react-admin';
407-
408-
const ColoredNumberFieldStyles = {
409-
small: { color: 'black' },
410-
big: { color: 'red' },
411-
};
412-
413-
const ColoredNumberField = (props) => (
414-
<NumberField
415-
sx={{
416-
...(props.record[props.source] < 100 &&
417-
ColoredNumberFieldStyles.small),
418-
...(props.record[props.source] >= 100 &&
419-
ColoredNumberFieldStyles.big),
420-
}}
421-
{...props}
422-
/>
423-
);
424-
425-
// Ensure the original component defaultProps are still applied as they may be used by its parents (such as the `Show` component):
426-
ColoredNumberField.defaultProps = NumberField.defaultProps;
427-
428-
export const PostList = () => (
429-
<List>
430-
<Datagrid>
431-
<TextField source="id" />
432-
...
433-
<ColoredNumberField source="nb_views" />
434-
<EditButton />
435-
</Datagrid>
436-
</List>
437-
);
438-
```
439-
{% endraw %}
440-
441-
Furthermore, you may extract this highlighting strategy into a Higher Order Component if you'd like to reuse it for other components as well:
403+
The following example shows how to create a new `<ColoredNumberField>` component, which renders with red text when its value is less than 0.
442404

443405
{% raw %}
444406
```jsx
445-
import * as React from 'react';
446-
import { NumberField, List, Datagrid, TextField, EditButton } from 'react-admin';
407+
import { iseRecordContext, NumberField, List, Datagrid, TextField, EditButton } from 'react-admin';
447408

448-
const ColoredNumberFieldStyles = {
449-
small: { color: 'black' },
450-
big: { color: 'red' },
451-
};
452-
453-
const colored = (WrappedComponent) => (props) =>
454-
(
455-
<WrappedComponent
456-
sx={{
457-
...(props.record[props.source] < 100 &&
458-
ColoredNumberFieldStyles.small),
459-
...(props.record[props.source] >= 100 &&
460-
ColoredNumberFieldStyles.big),
461-
}}
409+
const ColoredNumberField = (props) => {
410+
const record = useRecordContext();
411+
return (
412+
<NumberField
413+
sx={{ color: record[prop.source] < 0 ? 'red' : 'black' }}
462414
{...props}
463415
/>
464416
);
417+
};
465418

466-
467-
const ColoredNumberField = colored(NumberField);
468419
// Ensure the original component defaultProps are still applied as they may be used by its parents (such as the `Show` component):
469420
ColoredNumberField.defaultProps = NumberField.defaultProps;
470421

@@ -481,7 +432,7 @@ export const PostList = () => (
481432
```
482433
{% endraw %}
483434

484-
If you want to read more about higher-order components, check out this SitePoint tutorial: [Higher Order Components: A React Application Design Pattern](https://www.sitepoint.com/react-higher-order-components/)
435+
**Tip**: if you don't want to create a custom component to apply conditional formatting, you can also use [the `<WithRecord>` component](./WithRecord.md)].
485436

486437
## `useMediaQuery` Hook
487438

0 commit comments

Comments
 (0)