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 validate={required()}> displays empty choice #8296

Merged
merged 3 commits into from
Oct 25, 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
80 changes: 80 additions & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {
FormDataConsumer,
required,
testDataProvider,
TestTranslationProvider,
useRecordContext,
Expand Down Expand Up @@ -121,6 +122,85 @@ describe('<AutocompleteInput />', () => {

expect(input.value).toEqual('Default');
});

it('should display the emptyText when input is not required', async () => {
const emptyText = 'Default';
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm
onSubmit={jest.fn()}
defaultValues={{ role: 1 }}
>
<AutocompleteInput
emptyText={emptyText}
{...defaultProps}
choices={[]}
/>
</SimpleForm>
</AdminContext>
);
fireEvent.click(
await screen.findByLabelText('resources.users.fields.role')
);
await waitFor(() => {
expect(screen.queryAllByRole('option').length).toEqual(1);
});
expect(screen.queryByText('Default')).not.toBeNull();
});

it('should not display the emptyText when validate equals required', async () => {
const emptyText = 'Default';
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm
onSubmit={jest.fn()}
defaultValues={{ role: 1 }}
>
<AutocompleteInput
emptyText={emptyText}
{...defaultProps}
choices={[]}
validate={required()}
/>
</SimpleForm>
</AdminContext>
);
fireEvent.click(
await screen.findByLabelText('resources.users.fields.role *')
);
await waitFor(() => {
expect(screen.queryAllByRole('option').length).toEqual(0);
});
expect(screen.queryByText('Default')).toBeNull();
await screen.findByText('No options');
});

it('should not display the emptyText when isRequired is true', async () => {
const emptyText = 'Default';
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm
onSubmit={jest.fn()}
defaultValues={{ role: 1 }}
>
<AutocompleteInput
emptyText={emptyText}
{...defaultProps}
choices={[]}
isRequired
/>
</SimpleForm>
</AdminContext>
);
fireEvent.click(
await screen.findByLabelText('resources.users.fields.role *')
);
await waitFor(() => {
expect(screen.queryAllByRole('option').length).toEqual(0);
});
expect(screen.queryByText('Default')).toBeNull();
await screen.findByText('No options');
});
});

describe('optionValue', () => {
Expand Down
55 changes: 26 additions & 29 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,32 @@ export const AutocompleteInput = <

const finalEmptyText = emptyText ?? '';

const {
id,
field,
isRequired,
fieldState: { error, invalid, isTouched },
formState: { isSubmitted },
} = useInput({
defaultValue,
id: idOverride,
field: fieldOverride,
fieldState: fieldStateOverride,
formState: formStateOverride,
isRequired: isRequiredOverride,
onBlur,
onChange,
parse,
format,
resource,
source,
validate,
...rest,
});

const finalChoices = useMemo(
() =>
isRequiredOverride || multiple
isRequired || multiple
? allChoices
: [
{
Expand All @@ -213,36 +236,14 @@ export const AutocompleteInput = <
allChoices,
emptyValue,
finalEmptyText,
isRequiredOverride,
isRequired,
multiple,
optionText,
optionValue,
translate,
]
);

const {
id,
field,
isRequired,
fieldState: { error, invalid, isTouched },
formState: { isSubmitted },
} = useInput({
defaultValue,
id: idOverride,
field: fieldOverride,
fieldState: fieldStateOverride,
formState: formStateOverride,
onBlur,
onChange,
parse,
format,
resource,
source,
validate,
...rest,
});

const selectedChoice = useSelectedChoice<
OptionType,
Multiple,
Expand Down Expand Up @@ -537,11 +538,7 @@ If you provided a React element for the optionText prop, you must also provide t
label={label}
source={source}
resource={resourceProp}
isRequired={
typeof isRequiredOverride !== 'undefined'
? isRequiredOverride
: isRequired
}
isRequired={isRequired}
/>
}
error={
Expand Down