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 displayNull option in NullableBooleanInput and document it #4365

Merged
merged 2 commits into from
Jan 31, 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
21 changes: 21 additions & 0 deletions docs/Inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,27 @@ import { NullableBooleanInput } from 'react-admin';

![NullableBooleanInput](./img/nullable-boolean-input.png)

`<NullableBooleanInput />` doesn't display the empty option by default. If you want to customize its label and display it, you can use the `displayNull` prop.

```jsx
import { NullableBooleanInput } from 'react-admin';

<NullableBooleanInput
label="Commentable"
source="commentable"
displayNull
/>
```

```jsx
import englishMessages from 'ra-language-english';

englishMessages.ra.boolean.null = 'Null label';
```

![NullableBooleanInput](./img/nullable-boolean-input-null-label.png)


`<BooleanInput>` and `<NullableBooleanInput>` also accepts the [common input props](./Inputs.md#common-input-props).

## `<CheckboxGroupInput>`
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 17 additions & 1 deletion packages/ra-ui-materialui/src/input/NullableBooleanInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const NullableBooleanInput: FunctionComponent<
onChange,
onFocus,
options,
displayNull,
parse = getBooleanFromString,
resource,
source,
Expand All @@ -65,6 +66,21 @@ const NullableBooleanInput: FunctionComponent<
source,
validate,
});

const enhancedOptions = displayNull
JulienMattiussi marked this conversation as resolved.
Show resolved Hide resolved
? {
...options,
SelectProps: {
displayEmpty: true,
...(options && options.SelectProps),
},
InputLabelProps: {
shrink: true,
...(options && options.InputLabelProps),
},
}
: options;

return (
<TextField
id={id}
Expand All @@ -89,7 +105,7 @@ const NullableBooleanInput: FunctionComponent<
}
className={classnames(classes.input, className)}
variant={variant}
{...options}
{...enhancedOptions}
{...sanitizeRestProps(rest)}
>
<MenuItem value="">{translate('ra.boolean.null')}</MenuItem>
Expand Down