-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
[useAutocomplete] Improve isOptionEqualToValue value argument type #47801
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
base: master
Are you sure you want to change the base?
Changes from all commits
df26e9e
a66c577
62319d5
bdd5cab
2405264
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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ import Autocomplete, { | |||||||||||||||||||||||||||||||
| } from '@mui/material/Autocomplete'; | ||||||||||||||||||||||||||||||||
| import TextField from '@mui/material/TextField'; | ||||||||||||||||||||||||||||||||
| import { ChipTypeMap } from '@mui/material/Chip'; | ||||||||||||||||||||||||||||||||
| import { AutocompleteValueOrFreeSoloValueMapping } from '../useAutocomplete'; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| interface MyAutocompleteProps< | ||||||||||||||||||||||||||||||||
| T, | ||||||||||||||||||||||||||||||||
|
|
@@ -182,3 +183,42 @@ function CustomListboxRef() { | |||||||||||||||||||||||||||||||
| >(event); | ||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||
| />; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // freeSolo prop adds string to the getOptionLabel and isOptionEqualToValue value argument type | ||||||||||||||||||||||||||||||||
| <MyAutocomplete | ||||||||||||||||||||||||||||||||
| options={[{ label: '1' }, { label: '2' }]} | ||||||||||||||||||||||||||||||||
| renderInput={() => null} | ||||||||||||||||||||||||||||||||
| freeSolo | ||||||||||||||||||||||||||||||||
| getOptionLabel={(option) => { | ||||||||||||||||||||||||||||||||
| expectType<AutocompleteValueOrFreeSoloValueMapping<{ label: string }, true>, typeof option>( | ||||||||||||||||||||||||||||||||
| option, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return typeof option === 'string' ? option : option.label; | ||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||
| isOptionEqualToValue={(option, value) => { | ||||||||||||||||||||||||||||||||
| expectType<AutocompleteValueOrFreeSoloValueMapping<{ label: string }, true>, typeof value>( | ||||||||||||||||||||||||||||||||
| value, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| expectType<{ label: string }, typeof option>(option); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return typeof value === 'string' ? option.label === value : option.label === value.label; | ||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||
| />; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // getOptionLabel and isOptionEqualToValue value argument type should not include string when freeSolo is false | ||||||||||||||||||||||||||||||||
| <MyAutocomplete | ||||||||||||||||||||||||||||||||
| options={[{ label: '1' }, { label: '2' }]} | ||||||||||||||||||||||||||||||||
| renderInput={() => null} | ||||||||||||||||||||||||||||||||
| getOptionLabel={(option) => { | ||||||||||||||||||||||||||||||||
| expectType<{ label: string }, typeof option>(option); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return option.label; | ||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||
| isOptionEqualToValue={(option, value) => { | ||||||||||||||||||||||||||||||||
| expectType<{ label: string }, typeof value>(value); | ||||||||||||||||||||||||||||||||
| expectType<{ label: string }, typeof option>(option); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return option.label === value.label; | ||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||
| />; | ||||||||||||||||||||||||||||||||
|
Member
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.
Suggested change
If my understanding is correct, this should be added to ensure typesafety right?
Member
Author
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. I've updated the tests, but I'm not sure I follow what you're saying. To me, when getOptionLabel={(option) => {
expectType<AutocompleteValueOrFreeSoloValueMapping<{ label: string }, true>, typeof option>(
option,
);
return typeof option === 'string' ? option : option.label;
}}
isOptionEqualToValue={(option, value) => {
expectType<AutocompleteValueOrFreeSoloValueMapping<{ label: string }, true>, typeof value>(
value,
);
expectType<{ label: string }, typeof option>(option);
return typeof value === 'string' ? option.label === value : option.label === value.label;
}}and getOptionLabel={(option) => {
expectType<{ label: string }, typeof option>(option);
return option.label;
}}
isOptionEqualToValue={(option, value) => {
expectType<{ label: string }, typeof value>(value);
expectType<{ label: string }, typeof option>(option);
return option.label === value.label;
}} |
||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
Just to have a different placeholder than others (https://deploy-preview-47801--material-ui.netlify.app/material-ui/react-autocomplete/#search-input):