Skip to content

Commit

Permalink
[Autocomplete] Type multiple values with readonly arrays. (mui#38253)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcorpet authored and xcode-it committed Sep 11, 2023
1 parent c84d78d commit fcb6535
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
7 changes: 5 additions & 2 deletions docs/data/joy/components/autocomplete/GitHubLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ const Listbox = React.forwardRef<HTMLUListElement, any>((props, ref) => (

export default function GitHubLabel() {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const [value, setValue] = React.useState<LabelType[]>([labels[1], labels[11]]);
const [pendingValue, setPendingValue] = React.useState<LabelType[]>([]);
const [value, setValue] = React.useState<readonly LabelType[]>([
labels[1],
labels[11],
]);
const [pendingValue, setPendingValue] = React.useState<readonly LabelType[]>([]);

const handleClick = (event: React.MouseEvent<HTMLElement>) => {
setPendingValue(value);
Expand Down
7 changes: 5 additions & 2 deletions docs/data/material/components/autocomplete/GitHubLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@ const Button = styled(ButtonBase)(({ theme }) => ({

export default function GitHubLabel() {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const [value, setValue] = React.useState<LabelType[]>([labels[1], labels[11]]);
const [pendingValue, setPendingValue] = React.useState<LabelType[]>([]);
const [value, setValue] = React.useState<readonly LabelType[]>([
labels[1],
labels[11],
]);
const [pendingValue, setPendingValue] = React.useState<readonly LabelType[]>([]);
const theme = useTheme();

const handleClick = (event: React.MouseEvent<HTMLElement>) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/useAutocomplete/useAutocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function createFilterOptions<Value>(
export type AutocompleteFreeSoloValueMapping<FreeSolo> = FreeSolo extends true ? string : never;

export type AutocompleteValue<Value, Multiple, DisableClearable, FreeSolo> = Multiple extends true
? Array<Value | AutocompleteFreeSoloValueMapping<FreeSolo>>
? ReadonlyArray<Value | AutocompleteFreeSoloValueMapping<FreeSolo>>
: DisableClearable extends true
? NonNullable<Value | AutocompleteFreeSoloValueMapping<FreeSolo>>
: Value | null | AutocompleteFreeSoloValueMapping<FreeSolo>;
Expand Down
20 changes: 15 additions & 5 deletions packages/mui-base/src/useAutocomplete/useAutocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function Component() {
options: ['1', '2', '3'],
multiple: true,
onChange(event, value) {
expectType<string[], typeof value>(value);
expectType<readonly string[], typeof value>(value);
value;
},
});
Expand All @@ -83,7 +83,7 @@ function Component() {
options: ['1', '2', '3', 4, true],
multiple: true,
onChange(event, value) {
expectType<Array<string | number | boolean>, typeof value>(value);
expectType<ReadonlyArray<string | number | boolean>, typeof value>(value);
},
});

Expand All @@ -92,7 +92,7 @@ function Component() {
options: persons,
multiple: true,
onChange(event, value) {
expectType<Person[], typeof value>(value);
expectType<readonly Person[], typeof value>(value);
value;
},
});
Expand All @@ -101,7 +101,7 @@ function Component() {
useAutocomplete({
options: persons,
multiple: true,
onChange(event, value: Person[]) {},
onChange(event, value: readonly Person[]) {},
});

// options accepts const and value has correct type
Expand All @@ -112,6 +112,16 @@ function Component() {
},
});

// values accepts const and value has correct type
useAutocomplete({
options: ['1', '2', '3'] as const,
multiple: true,
value: ['1', '3'] as const,
onChange(event, value) {
expectType<ReadonlyArray<'1' | '2' | '3'>, typeof value>(value);
},
});

// Disable clearable

useAutocomplete({
Expand Down Expand Up @@ -159,7 +169,7 @@ function Component() {
options: persons,
multiple: true,
onChange(event, value) {
expectType<Array<string | Person>, typeof value>(value);
expectType<ReadonlyArray<string | Person>, typeof value>(value);
},
freeSolo: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function MyAutocomplete<
<MyAutocomplete
options={['1', '2', '3']}
onChange={(event, value) => {
expectType<string[], typeof value>(value);
expectType<readonly string[], typeof value>(value);
}}
renderInput={() => null}
multiple
Expand Down

0 comments on commit fcb6535

Please sign in to comment.