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

[useAutocomplete] Pass only valid values for the getOptionLabel prop #36088

Conversation

rangoo94
Copy link
Contributor

@rangoo94 rangoo94 commented Feb 7, 2023

The getOptionLabel prop shouldn't be called with fallback values, as it will not match the TypeScript's contract for generic getOptionLabel value.

Fixes #36045

Before: https://codesandbox.io/s/angry-tree-2jcp3o?file=/src/App.tsx

After: https://codesandbox.io/s/frosty-tdd-euyttl?file=/src/App.tsx

@rangoo94 rangoo94 changed the title [useAutocomplete] pass only valid values for the getOptionLabel prop [useAutocomplete] Pass only valid values for the getOptionLabel prop Feb 7, 2023
@mui-bot
Copy link

mui-bot commented Feb 7, 2023

Netlify deploy preview

https://deploy-preview-36088--material-ui.netlify.app/

Bundle size report

Details of bundle changes

Generated by 🚫 dangerJS against 4859534

@rangoo94 rangoo94 force-pushed the 36045-autocomplete-passes-string-to-getoptionlabel-instead-of-option branch 3 times, most recently from 85a2ee4 to d78cb1a Compare February 7, 2023 10:43
@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Feb 8, 2023
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rangoo94 Thanks for the PR. Can you add a test showcasing and covering the fix for the issue provided in the reproduction?

@ZeeshanTamboli ZeeshanTamboli added bug 🐛 Something doesn't work PR: needs test The pull request can't be merged regression A bug, but worse labels Feb 10, 2023
@rangoo94
Copy link
Contributor Author

@ZeeshanTamboli, sure, not a problem - I added the unit test for regression.

@rangoo94 rangoo94 force-pushed the 36045-autocomplete-passes-string-to-getoptionlabel-instead-of-option branch from 3dd9626 to b78c1ec Compare February 11, 2023 00:04
@ZeeshanTamboli ZeeshanTamboli removed the PR: needs test The pull request can't be merged label Feb 13, 2023
Comment on lines 468 to 469
const label1 = value1 === null ? '' : getOptionLabel(value1);
const label2 = value2 === null ? '' : getOptionLabel(value2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not check with strict equality, in case value is undefined or empty string '' using a controlled Autocomplete.

Suggested change
const label1 = value1 === null ? '' : getOptionLabel(value1);
const label2 = value2 === null ? '' : getOptionLabel(value2);
const label1 = !value1 ? '' : getOptionLabel(value1);
const label2 = !value2 ? '' : getOptionLabel(value2);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will have small edge cases, i.e. false will be treated equally to null. I will do that though, as I believe that it will be ultimately rare, and even then the inconvenience will be low 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases, or perhaps never, value will not be false. 

@rangoo94
Copy link
Contributor Author

@ZeeshanTamboli Thanks, I have applied the expected changes.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Feb 13, 2023
@rangoo94 rangoo94 force-pushed the 36045-autocomplete-passes-string-to-getoptionlabel-instead-of-option branch from c84d7d8 to 4859534 Compare February 13, 2023 16:17
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Feb 13, 2023
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rangoo94 It's a great first pull request on MUI 👌 Thank you for working on it!

@michaldudak michaldudak merged commit 7e6b64d into mui:master Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module! regression A bug, but worse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Autocomplete] Non-freesolo autocomplete acts like freesolo: passes string to getOptionLabel instead of option
5 participants