Skip to content

Commit

Permalink
Display null label in NullableBooleanInput
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienMattiussi committed Jan 28, 2020
1 parent 2a409ac commit 582e28a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
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
? {
...options,
SelectProps: {
...(options && options.SelectProps),
displayEmpty: true,
},
InputLabelProps: {
...(options && options.InputLabelProps),
shrink: true,
},
}
: { ...options };

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

0 comments on commit 582e28a

Please sign in to comment.