Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass BooleanField custom icons to show as values #5281

Merged
merged 1 commit into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions docs/Fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,12 @@ import { BooleanField } from 'react-admin';

### Properties

| Prop | Required | Type | Default | Description |
| ----------------- | -------- | ------ | ------- | ------------------------------- |
| `valueLabelTrue` | Optional | string | 'true' | Aria label for the truthy value |
| `valueLabelFalse` | Optional | string | 'false' | Aria label for the falsy value |
| Prop | Required | Type | Default | Description |
| ----------------- | -------- | ---------------- | -------------------------- | --------------------------------- |
| `valueLabelTrue` | Optional | string | 'true' | Aria label for the truthy value |
| `valueLabelFalse` | Optional | string | 'false' | Aria label for the falsy value |
| `TrueIcon` | Optional | SvgIconComponent | `@material-ui/icons/Done` | Icon to show for the truthy value |
| `FalseIcon` | Optional | SvgIconComponent | `@material-ui/icons/Clear` | Icon to show for the falsy value |

`<BooleanField>` also accepts the [common field props](./Fields.md#common-field-props).

Expand All @@ -233,6 +235,15 @@ If you need to override it, you can use the `valueLabelTrue` and `valueLabelFals
<BooleanField source="published" valueLabelTrue="myapp.published.true" valueLabelFalse="myapp.published.false" />
```

You can customize the icons to show by setting the `TrueIcon` and `FalseIcon` props which accept a SvgIcon type.

```jsx
import AlarmOnIcon from '@material-ui/icons/AlarmOn';
import AlarmOffIcon from '@material-ui/icons/AlarmOff';

<BooleanField source="alarm" TrueIcon={AlarmOnIcon} FalseIcon={AlarmOffIcon} />
```

## `<ChipField>`

Displays a value inside a ["Chip"](https://material-ui.com/components/chips), which is Material UI's term for a label.
Expand Down
17 changes: 13 additions & 4 deletions packages/ra-ui-materialui/src/field/BooleanField.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as React from 'react';
import { FC, memo } from 'react';
import PropTypes from 'prop-types';
import { ComponentType, FC, memo, ReactElement } from 'react';
import { SvgIconComponent } from '@material-ui/icons';
import PropTypes, { ReactComponentLike } from 'prop-types';
import get from 'lodash/get';
import classnames from 'classnames';
import FalseIcon from '@material-ui/icons/Clear';
import TrueIcon from '@material-ui/icons/Done';
import DoneIcon from '@material-ui/icons/Done';
import ClearIcon from '@material-ui/icons/Clear';
import { Tooltip, Typography } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import { TypographyProps } from '@material-ui/core/Typography';
Expand Down Expand Up @@ -34,6 +35,8 @@ export const BooleanField: FC<BooleanFieldProps> = memo<BooleanFieldProps>(
record = {},
valueLabelTrue,
valueLabelFalse,
TrueIcon,
FalseIcon,
...rest
} = props;
const translate = useTranslate();
Expand Down Expand Up @@ -80,6 +83,8 @@ export const BooleanField: FC<BooleanFieldProps> = memo<BooleanFieldProps>(

BooleanField.defaultProps = {
addLabel: true,
TrueIcon: DoneIcon,
FalseIcon: ClearIcon,
};

BooleanField.propTypes = {
Expand All @@ -88,6 +93,8 @@ BooleanField.propTypes = {
...fieldPropTypes,
valueLabelFalse: PropTypes.string,
valueLabelTrue: PropTypes.string,
TrueIcon: PropTypes.elementType,
FalseIcon: PropTypes.elementType,
};

export interface BooleanFieldProps
Expand All @@ -96,6 +103,8 @@ export interface BooleanFieldProps
TypographyProps {
valueLabelTrue?: string;
valueLabelFalse?: string;
TrueIcon?: SvgIconComponent;
FalseIcon?: SvgIconComponent;
}

export default BooleanField;