Skip to content

Commit

Permalink
[Autocomplete] Fix inferred value type when multiple prop is true (
Browse files Browse the repository at this point in the history
  • Loading branch information
fenghan34 committed Nov 28, 2022
1 parent 5fe8592 commit 86a3404
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
12 changes: 5 additions & 7 deletions packages/mui-base/src/AutocompleteUnstyled/useAutocomplete.d.ts
Expand Up @@ -27,13 +27,11 @@ export function createFilterOptions<T>(

export type AutocompleteFreeSoloValueMapping<FreeSolo> = FreeSolo extends true ? string : never;

export type AutocompleteValue<T, Multiple, DisableClearable, FreeSolo> = Multiple extends
| undefined
| false
? DisableClearable extends true
? NonNullable<T | AutocompleteFreeSoloValueMapping<FreeSolo>>
: T | null | AutocompleteFreeSoloValueMapping<FreeSolo>
: Array<T | AutocompleteFreeSoloValueMapping<FreeSolo>>;
export type AutocompleteValue<T, Multiple, DisableClearable, FreeSolo> = Multiple extends true
? Array<T | AutocompleteFreeSoloValueMapping<FreeSolo>>
: DisableClearable extends true
? NonNullable<T | AutocompleteFreeSoloValueMapping<FreeSolo>>
: T | null | AutocompleteFreeSoloValueMapping<FreeSolo>;

export interface UseAutocompleteProps<
T,
Expand Down
18 changes: 18 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.spec.tsx
Expand Up @@ -66,6 +66,24 @@ function MyAutocomplete<
renderInput={(params) => <TextField {...params} value={params.inputProps.value} />}
/>;

interface Option {
label: string;
value: string;
}
const options: Option[] = [
{ label: '1', value: '1' },
{ label: '2', value: '2' },
];
const defaultOptions = [options[0], options[1]];
<MyAutocomplete
multiple
options={options}
defaultValue={defaultOptions}
isOptionEqualToValue={(o, v) => o.label === v.label}
getOptionLabel={(o) => o.label}
renderInput={() => null}
/>;

interface Tag {
color: string;
label: string;
Expand Down

0 comments on commit 86a3404

Please sign in to comment.