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

Fix <AutocompleteInput> should only add an empty option when there is an emptyText #8305

Merged
merged 3 commits into from
Oct 26, 2022
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
4 changes: 1 addition & 3 deletions docs/AutocompleteInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ See [Using in a `ReferenceInput>`](#using-in-a-referenceinput) below for more in

## `emptyText`

If the input isn't required (using `validate={required()}`), users can select an empty choice with an empty text `''` as label.

You can override that label with the `emptyText` prop.
If the input isn't required (using `validate={required()}`), and you need a choice to represent the empty value, set `emptyText` prop and a choice will be added at the top, with its value as label.

```jsx
<AutocompleteInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ const BookEditWithEmptyText = () => {
>
<SimpleForm>
<AutocompleteInput
label="emptyValue set to 'no-author', emptyText set to '' by default"
label="emptyValue set to 'no-author', emptyText not set"
source="author"
choices={choices}
emptyValue="no-author"
Expand Down
11 changes: 5 additions & 6 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ export const AutocompleteInput = <

const translate = useTranslate();

const finalEmptyText = emptyText ?? '';

const {
id,
field,
Expand All @@ -220,22 +218,23 @@ export const AutocompleteInput = <

const finalChoices = useMemo(
() =>
isRequired || multiple
// eslint-disable-next-line eqeqeq
emptyText == undefined || isRequired || multiple
? allChoices
: [
{
[optionValue || 'id']: emptyValue,
[typeof optionText === 'string'
? optionText
: 'name']: translate(finalEmptyText, {
_: finalEmptyText,
: 'name']: translate(emptyText, {
_: emptyText,
}),
},
].concat(allChoices),
[
allChoices,
emptyValue,
finalEmptyText,
emptyText,
isRequired,
multiple,
optionText,
Expand Down