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 optionText returning an element throws error #7289

Merged
merged 2 commits into from
Feb 23, 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: 3 additions & 1 deletion packages/ra-core/src/form/useChoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ const useChoices = ({
? optionText(choice)
: get(choice, optionText);

return translateChoice
return isValidElement(choiceName)
? choiceName
: translateChoice
? translate(choiceName, { _: choiceName })
: String(choiceName);
},
Expand Down
34 changes: 33 additions & 1 deletion packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { render, fireEvent, waitFor, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { AutocompleteInput } from './AutocompleteInput';
Expand Down Expand Up @@ -534,6 +534,38 @@ describe('<AutocompleteInput />', () => {
});
});

it('should throw an error if no inputText was provided when the optionText returns an element', () => {
const mock = jest.spyOn(console, 'error').mockImplementation(() => {});
const SuggestionItem = ({ record }: { record?: any }) => (
<div aria-label={record && record.name} />
);

const t = () => {
act(() => {
render(
<Form
onSubmit={jest.fn()}
render={() => (
<AutocompleteInput
{...defaultProps}
optionText={() => <SuggestionItem />}
matchSuggestion={() => true}
choices={[
{ id: 1, name: 'bar' },
{ id: 2, name: 'foo' },
]}
/>
)}
/>
);
});
};
expect(t).toThrow(
'When optionText returns a React element, you must also provide the inputText prop'
);
mock.mockRestore();
});

it('should display helperText if specified', () => {
const { queryByText } = render(
<Form
Expand Down
19 changes: 16 additions & 3 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,20 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
const isFunction = typeof optionText === 'function';

if (isFunction) {
const selectedItemText = optionText(selectedItem);

if (isValidElement(selectedItemText) && !inputText) {
throw new Error(
'When optionText returns a React element, you must also provide the inputText prop'
);
}

const hasOption = choices.some(choice => {
const text = optionText(choice) as string;

return get(choice, text) === filterValue;
});

const selectedItemText = optionText(selectedItem);

return hasOption || selectedItemText === filterValue;
}

Expand All @@ -246,7 +252,14 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
});

return selectedItemText === filterValue || hasOption;
}, [choices, optionText, filterValue, matchSuggestion, selectedItem]);
}, [
optionText,
selectedItem,
choices,
filterValue,
matchSuggestion,
inputText,
]);

const shouldAllowCreate = !doesQueryMatchSuggestion;

Expand Down