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 not accepting 0 as a value #4693

Merged
merged 1 commit into from
Apr 16, 2020
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
24 changes: 21 additions & 3 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ describe('<AutocompleteInput />', () => {

it('should allow customized rendering of suggesting item', () => {
const SuggestionItem = ({ record }: { record?: any }) => (
<div aria-label={record.name} />
<div aria-label={record && record.name} />
);

const { getByLabelText, queryByLabelText } = render(
Expand All @@ -418,8 +418,8 @@ describe('<AutocompleteInput />', () => {
<AutocompleteInput
{...defaultProps}
optionText={<SuggestionItem />}
inputText={record => record.name}
matchSuggestion={(filter, choice) => true}
inputText={record => record && record.name}
matchSuggestion={() => true}
choices={[
{ id: 1, name: 'bar' },
{ id: 2, name: 'foo' },
Expand Down Expand Up @@ -572,4 +572,22 @@ describe('<AutocompleteInput />', () => {

expect(getByLabelText('My Sugggestions Container')).not.toBeNull();
});

describe('Fix issue #4660', () => {
it('should accept 0 as an input value', () => {
const { queryByDisplayValue } = render(
<Form
onSubmit={jest.fn()}
initialValues={{ role: 0 }}
render={() => (
<AutocompleteInput
{...defaultProps}
choices={[{ id: 0, name: 'foo' }]}
/>
)}
/>
);
expect(queryByDisplayValue('foo')).not.toBeNull();
});
});
});
12 changes: 7 additions & 5 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const AutocompleteInput: FunctionComponent<
variant = 'filled',
...rest
} = props;

if (isValidElement(optionText) && !inputText) {
throw new Error(`If the optionText prop is a React element, you must also specify the inputText prop:
<AutocompleteInput
Expand Down Expand Up @@ -229,11 +230,11 @@ const AutocompleteInput: FunctionComponent<
// If we have a value, set the filter to its text so that
// Downshift displays it correctly
setFilterValue(
input.value
? inputText
? inputText(getChoiceText(selectedItem).props.record)
: getChoiceText(selectedItem)
: ''
typeof input.value === 'undefined' || input.value === null
? ''
: inputText
? inputText(getChoiceText(selectedItem).props.record)
: getChoiceText(selectedItem)
);
}, [
input.value,
Expand Down Expand Up @@ -286,6 +287,7 @@ const AutocompleteInput: FunctionComponent<
const handleBlur = useCallback(
event => {
handleFilterChange('');

// If we had a value before, set the filter back to its text so that
// Downshift displays it correctly
setFilterValue(
Expand Down