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] Simplify error for wrong getOptionLabel #20103

Merged
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 @@ -778,7 +778,7 @@ describe('<Autocomplete />', () => {
expect(handleChange.args[0][1]).to.equal('a');
expect(consoleErrorMock.callCount()).to.equal(2); // strict mode renders twice
expect(consoleErrorMock.messages()[0]).to.include(
'For the input option: "a", `getOptionLabel` returns: undefined',
'the `getOptionLabel` method of Autocomplete returned undefined instead of a string',
);
});

Expand Down
11 changes: 5 additions & 6 deletions packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,12 @@ export default function useAutocomplete(props) {

if (process.env.NODE_ENV !== 'production') {
if (typeof optionLabel !== 'string') {
const erroneousReturn =
optionLabel === undefined ? 'undefined' : `${typeof optionLabel} (${optionLabel})`;
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: ${optionLabel}.`,
`Material-UI: the \`getOptionLabel\` method of ${componentName} returned ${erroneousReturn} instead of a string for`,
JSON.stringify(newValue),
].join('\n'),
);
}
Expand Down Expand Up @@ -465,7 +464,7 @@ export default function useAutocomplete(props) {
if (matches.length > 1) {
console.error(
[
'Material-UI: the `getOptionSelected` method of useAutocomplete do not handle the arguments correctly.',
`Material-UI: the \`getOptionSelected\` method of ${componentName} do not handle the arguments correctly.`,
`The component expects a single value to match a given option but found ${
matches.length
} matches.`,
Expand Down