-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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] Zero (0) integer key display throws #18994
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,8 +232,8 @@ export default function useAutocomplete(props) { | |
const [openState, setOpenState] = React.useState(false); | ||
const open = isOpenControlled ? openProp : openState; | ||
|
||
const inputValueFilter = | ||
!multiple && value && inputValue === getOptionLabel(value) ? '' : inputValue; | ||
const inputValueIsSelectedValue = | ||
!multiple && value != null && inputValue === getOptionLabel(value); | ||
|
||
let popupOpen = open; | ||
|
||
|
@@ -250,7 +250,9 @@ export default function useAutocomplete(props) { | |
} | ||
return true; | ||
}), | ||
{ inputValue: inputValueFilter }, | ||
// we use the empty string to manipulate `filterOptions` to not filter any options | ||
// i.e. the filter predicate always returns true | ||
{ inputValue: inputValueIsSelectedValue ? '' : inputValue }, | ||
) | ||
: []; | ||
|
||
|
@@ -585,7 +587,7 @@ export default function useAutocomplete(props) { | |
inputRef.current.value.length, | ||
); | ||
} | ||
} else if (freeSolo && inputValueFilter !== '') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Introduced in #18786.
For future reference: Prefer to codify this explicitly rather than communicating this implicitly with a type (here: the empty string). A variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds fair, it will help our future self. |
||
} else if (freeSolo && inputValue !== '' && inputValueIsSelectedValue === false) { | ||
selectNewValue(event, inputValue); | ||
} | ||
break; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should start linting against these. It's hidden loose equality checking and I think we had like 5-10 issues the last year concerning these. In a typed language this might be ok (if you know the type already is boolean). In a weakly typed language this is dangerous while not improving readability (I'd say it's even worse). The added bytes is negligible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also remember a few occurrences of this type of bug. It's fascinating to see the length of which the components get battle-tested.