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

[Autocomplete] Fix crash with freeSolo and rich options #18161

Merged
merged 3 commits into from
Nov 4, 2019
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
33 changes: 33 additions & 0 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { expect } from 'chai';
import { createMount, getClasses } from '@material-ui/core/test-utils';
import describeConformance from '@material-ui/core/test-utils/describeConformance';
import consoleErrorMock from 'test/utils/consoleErrorMock';
import { spy } from 'sinon';
import { createClientRender, fireEvent } from 'test/utils/createClientRender';
import Autocomplete from './Autocomplete';
Expand Down Expand Up @@ -369,4 +370,36 @@ describe('<Autocomplete />', () => {
});
});
});

describe('warnings', () => {
beforeEach(() => {
consoleErrorMock.spy();
});

afterEach(() => {
consoleErrorMock.reset();
});

it('warn if getOptionLabel do not return a string', () => {
const handleChange = spy();
const { container } = render(
<Autocomplete
freeSolo
onChange={handleChange}
options={[{ name: 'test' }, { name: 'foo' }]}
getOptionLabel={option => option.name}
renderInput={params => <TextField {...params} />}
/>,
);
const input = container.querySelector('input');
fireEvent.change(input, { target: { value: 'a' } });
fireEvent.keyDown(input, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.equal('a');
expect(consoleErrorMock.callCount()).to.equal(2); // strict mode renders twice
expect(consoleErrorMock.args()[0][0]).to.include(
'For the input option: "a", `getOptionLabel` returns: undefined',
);
});
});
});
20 changes: 19 additions & 1 deletion packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,26 @@ export default function useAutocomplete(props) {
let newInputValue;
if (multiple) {
newInputValue = '';
} else if (newValue == null) {
newInputValue = '';
} else {
newInputValue = newValue != null ? getOptionLabel(newValue) : '';
const optionLabel = getOptionLabel(newValue);

if (process.env.NODE_ENV !== 'production') {
if (typeof optionLabel !== 'string') {
console.error(
[
'Material-UI: the `getOptionLabel` method of useAutocomplete do not handle the options correctly.',
`The component expect a string but received ${typeof optionLabel}.`,
`For the input option: ${JSON.stringify(
newValue,
)}, \`getOptionLabel\` returns: ${newInputValue}.`,
].join('\n'),
);
}
}

newInputValue = typeof optionLabel === 'string' ? optionLabel : '';
}

setInputValue(newInputValue);
Expand Down