-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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] Warn wrong comparison getOptionSelected implementation #19595
Comments
This comment has been minimized.
This comment has been minimized.
@tagaychinova Interesting case, I propose the following new error message: diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index dfd6171c8..47e2a4c59 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -439,6 +439,24 @@ export default function useAutocomplete(props) {
const item = newValue;
newValue = Array.isArray(value) ? [...value] : [];
+ if (process.env.NODE_ENV !== 'production') {
+ let matches = 0;
+ for (let i = 0; i < newValue.length; i += 1) {
+ if (getOptionSelected(item, newValue[i])) {
+ matches += 1;
+ }
+ }
+
+ if (matches > 1) {
+ console.error(
+ [
+ 'Material-UI: the `getOptionSelected` method of useAutocomplete do not handle the arguments correctly.',
+ `The component expects a single value to match a given option but found ${matches}.`,
+ ].join('\n'),
+ );
+ }
+ }
+
const itemIndex = findIndex(newValue, valueItem => getOptionSelected(item, valueItem));
if (itemIndex === -1) { |
@oliviertassinari why not we try to solve this issue instead of giving error message? |
@ahmad-reza619 This error message is a lead/tip on how to solve the problem in your codebase. If you can't figure it out with it, then this warning might not be good enough. I wonder what lead to this wrong usage of the API and how we could better help 🤔. |
Maybe we should rename |
sounds good, and btw why not replace the |
something like this const matches = newValue.some(val => optionEqualValue(item, val));
if (matches) {
console.error(
[
'Material-UI: the `getOptionSelected` method of useAutocomplete do not handle the arguments correctly.',
`The component expects a single value to match a given option but found ${matches}.`,
].join('\n')
)
} |
I'd prefer a warning here and I'm not even sure there aren't valid use cases for this e.g. fallbacks. And I'm not sure why this should warn. What is wrong with the provided codesandbox? It seems like a bug on our side to me. |
@ahmad-reza619 What about IE 11? Ok, here is the solution. The |
How does this apply here? Each option and current value is unique in the codesandbox.
Tried that in the codesandbox. The second parameter is undefined. |
I hope it will be clearer with https://codesandbox.io/s/material-demo-b3nsz. -getOptionSelected={option => value.find(v => v.id === option.id)}
+getOptionSelected={(option, value) => value.id === option.id} |
@oliviertassinari Ok sorry for misunderstanding, but looks like Array.prototype.some is actually supported in IE based on this MDN article. But well yeah it only return true if there is one of its predicate is true 🤔 |
Can i give it a go? |
Perfect for IE 11 :) but I think that it could be interesting display the number of matches. |
Honestly, I'm not sure the warning will be enough. It took me a good chunk of time to figure out what was wrong with your example. Considering I have myself designed most of the component, I can only imagine how it can be confusing for somebody else. |
FWIW, I would favor (also) renaming the prop, as you suggested, @oliviertassinari. It specifically does not do what it name implies. I have to rethink it every time I come back to it. BTW, I love this component. :-) |
@scrozier What name you would suggest? |
@oliviertassinari It's tricky, isn't it? I wonder if that means that there's an underlying architectural issue? You're really wanting to find out which option is selected, ergo BTW, thank you for your shepherding of this excellent project. You do a great job. |
@scrozier Both options sound great. |
@oliviertassinari Should I open an issue for this? |
@scrozier Sure, if you want to submit a pull request directly, that will do it too :) |
@oliviertassinari I shall give it a whirl.... |
Bug Demo: https://codesandbox.io/s/material-demo-q3fno
The text was updated successfully, but these errors were encountered: