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 Creation Support #6328

Merged
merged 1 commit into from
Jun 3, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,9 @@ const AutocompleteArrayInput = (props: AutocompleteArrayInputProps) => {
onKeyDown: handleKeyDown,
});

const createItem = getCreateItem();
const suggestions = [
...getSuggestions(suggestionFilter),
...(onCreate || create ? [createItem] : []),
...(onCreate || create ? [getCreateItem()] : []),
];
return (
<div className={classes.container}>
Expand Down Expand Up @@ -512,7 +511,7 @@ const AutocompleteArrayInput = (props: AutocompleteArrayInputProps) => {
{suggestions.map((suggestion, index) => (
<AutocompleteSuggestionItem
key={getChoiceValue(suggestion)}
createValue={createItem.id}
createValue={createValue}
suggestion={suggestion}
index={index}
highlightedIndex={highlightedIndex}
Expand Down
83 changes: 68 additions & 15 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,89 +69,142 @@ describe('<AutocompleteInput />', () => {
expect(queryByDisplayValue('foo')).not.toBeNull();
});

it('should use optionValue as value identifier', () => {
const { queryByDisplayValue } = render(
it('should use optionValue as value identifier', async () => {
const { getByLabelText, queryByText, queryByDisplayValue } = render(
<Form
onSubmit={jest.fn()}
initialValues={{ role: 2 }}
render={() => (
<AutocompleteInput
{...defaultProps}
optionValue="foobar"
choices={[{ foobar: 2, name: 'foo' }]}
choices={[
{ foobar: 2, name: 'foo' },
{ foobar: 3, name: 'bar' },
]}
/>
)}
/>
);
expect(queryByDisplayValue('foo')).not.toBeNull();
const input = getByLabelText('resources.users.fields.role', {
selector: 'input',
});
fireEvent.focus(input);
await waitFor(() => {
expect(queryByText('bar')).not.toBeNull();
});
});

it('should use optionValue including "." as value identifier', () => {
const { queryByDisplayValue } = render(
it('should use optionValue including "." as value identifier', async () => {
const { getByLabelText, queryByDisplayValue, queryByText } = render(
<Form
onSubmit={jest.fn()}
initialValues={{ role: 2 }}
render={() => (
<AutocompleteInput
{...defaultProps}
optionValue="foobar.id"
choices={[{ foobar: { id: 2 }, name: 'foo' }]}
choices={[
{ foobar: { id: 2 }, name: 'foo' },
{ foobar: { id: 3 }, name: 'bar' },
]}
/>
)}
/>
);
expect(queryByDisplayValue('foo')).not.toBeNull();
const input = getByLabelText('resources.users.fields.role', {
selector: 'input',
});
fireEvent.focus(input);
await waitFor(() => {
expect(queryByText('bar')).not.toBeNull();
});
});

it('should use optionText with a string value as text identifier', () => {
const { queryByDisplayValue } = render(
it('should use optionText with a string value as text identifier', async () => {
const { getByLabelText, queryByText, queryByDisplayValue } = render(
<Form
onSubmit={jest.fn()}
initialValues={{ role: 2 }}
render={() => (
<AutocompleteInput
{...defaultProps}
optionText="foobar"
choices={[{ id: 2, foobar: 'foo' }]}
choices={[
{ id: 2, foobar: 'foo' },
{ id: 3, foobar: 'bar' },
]}
/>
)}
/>
);
expect(queryByDisplayValue('foo')).not.toBeNull();

const input = getByLabelText('resources.users.fields.role', {
selector: 'input',
});
fireEvent.focus(input);
await waitFor(() => {
expect(queryByText('bar')).not.toBeNull();
});
});

it('should use optionText with a string value including "." as text identifier', () => {
const { queryByDisplayValue } = render(
it('should use optionText with a string value including "." as text identifier', async () => {
const { getByLabelText, queryByText, queryByDisplayValue } = render(
<Form
onSubmit={jest.fn()}
initialValues={{ role: 2 }}
render={() => (
<AutocompleteInput
{...defaultProps}
optionText="foobar.name"
choices={[{ id: 2, foobar: { name: 'foo' } }]}
choices={[
{ id: 2, foobar: { name: 'foo' } },
{ id: 3, foobar: { name: 'bar' } },
]}
/>
)}
/>
);
expect(queryByDisplayValue('foo')).not.toBeNull();

const input = getByLabelText('resources.users.fields.role', {
selector: 'input',
});
fireEvent.focus(input);
await waitFor(() => {
expect(queryByText('bar')).not.toBeNull();
});
});

it('should use optionText with a function value as text identifier', () => {
const { queryByDisplayValue } = render(
it('should use optionText with a function value as text identifier', async () => {
const { getByLabelText, queryByText, queryByDisplayValue } = render(
<Form
onSubmit={jest.fn()}
initialValues={{ role: 2 }}
render={() => (
<AutocompleteInput
{...defaultProps}
optionText={choice => choice.foobar}
choices={[{ id: 2, foobar: 'foo' }]}
choices={[
{ id: 2, foobar: 'foo' },
{ id: 3, foobar: 'bar' },
]}
/>
)}
/>
);
expect(queryByDisplayValue('foo')).not.toBeNull();

const input = getByLabelText('resources.users.fields.role', {
selector: 'input',
});
fireEvent.focus(input);
await waitFor(() => {
expect(queryByText('bar')).not.toBeNull();
});
});

it('should translate the value by default', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
}
filterValue={filterValue}
getSuggestionText={getChoiceText}
createValue={createValue}
{...getItemProps({
item: suggestion,
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const AutocompleteSuggestionItem: FunctionComponent<
const classes = useStyles(props);
const isHighlighted = highlightedIndex === index;
const suggestionText =
suggestion?.id === createValue
'id' in suggestion && suggestion.id === createValue
? suggestion.name
: getSuggestionText(suggestion);
let matches;
Expand Down