Skip to content

Commit 1920b80

Browse files
[useAutocomplete] Improve TS typing of groupedOptions prop (#44657)
Co-authored-by: Zeeshan Tamboli <[email protected]>
1 parent 7f04342 commit 1920b80

File tree

10 files changed

+50
-16
lines changed

10 files changed

+50
-16
lines changed

docs/data/material/components/autocomplete/CustomizedHook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import PropTypes from 'prop-types';
3-
import { useAutocomplete } from '@mui/base/useAutocomplete';
3+
import useAutocomplete from '@mui/material/useAutocomplete';
44
import CheckIcon from '@mui/icons-material/Check';
55
import CloseIcon from '@mui/icons-material/Close';
66
import { styled } from '@mui/material/styles';

docs/data/material/components/autocomplete/CustomizedHook.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as React from 'react';
2-
import { useAutocomplete, AutocompleteGetTagProps } from '@mui/base/useAutocomplete';
2+
import useAutocomplete, {
3+
AutocompleteGetTagProps,
4+
} from '@mui/material/useAutocomplete';
35
import CheckIcon from '@mui/icons-material/Check';
46
import CloseIcon from '@mui/icons-material/Close';
57
import { styled } from '@mui/material/styles';
@@ -188,7 +190,7 @@ export default function CustomizedHook() {
188190
</div>
189191
{groupedOptions.length > 0 ? (
190192
<Listbox {...getListboxProps()}>
191-
{(groupedOptions as typeof top100Films).map((option, index) => {
193+
{groupedOptions.map((option, index) => {
192194
const { key, ...optionProps } = getOptionProps({ option, index });
193195
return (
194196
<li key={key} {...optionProps}>

docs/data/material/components/autocomplete/UseAutocomplete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { useAutocomplete } from '@mui/base/useAutocomplete';
2+
import useAutocomplete from '@mui/material/useAutocomplete';
33
import { styled } from '@mui/system';
44

55
const Label = styled('label')({

docs/data/material/components/autocomplete/UseAutocomplete.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { useAutocomplete } from '@mui/base/useAutocomplete';
2+
import useAutocomplete from '@mui/material/useAutocomplete';
33
import { styled } from '@mui/system';
44

55
const Label = styled('label')({
@@ -63,7 +63,7 @@ export default function UseAutocomplete() {
6363
</div>
6464
{groupedOptions.length > 0 ? (
6565
<Listbox {...getListboxProps()}>
66-
{(groupedOptions as typeof top100Films).map((option, index) => {
66+
{groupedOptions.map((option, index) => {
6767
const { key, ...optionProps } = getOptionProps({ option, index });
6868
return (
6969
<li key={key} {...optionProps}>

docs/data/material/components/autocomplete/UseAutocomplete.tsx.preview

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</div>
55
{groupedOptions.length > 0 ? (
66
<Listbox {...getListboxProps()}>
7-
{(groupedOptions as typeof top100Films).map((option, index) => {
7+
{groupedOptions.map((option, index) => {
88
const { key, ...optionProps } = getOptionProps({ option, index });
99
return (
1010
<li key={key} {...optionProps}>

docs/pages/material-ui/api/autocomplete.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
},
8484
"groupBy": {
8585
"type": { "name": "func" },
86-
"signature": { "type": "function(options: Value) => string", "describedArgs": ["options"] }
86+
"signature": { "type": "function(option: Value) => string", "describedArgs": ["option"] }
8787
},
8888
"handleHomeEndKeys": { "type": { "name": "bool" }, "default": "!props.freeSolo" },
8989
"id": { "type": { "name": "string" } },

docs/translations/api-docs/autocomplete/autocomplete.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
},
8383
"groupBy": {
8484
"description": "If provided, the options will be grouped under the returned string. The groupBy value is also used as the text for group headings when <code>renderGroup</code> is not provided.",
85-
"typeDescriptions": { "options": "The options to group." }
85+
"typeDescriptions": { "option": "The Autocomplete option." }
8686
},
8787
"handleHomeEndKeys": {
8888
"description": "If <code>true</code>, the component handles the &quot;Home&quot; and &quot;End&quot; keys when the popup is open. It should move focus to the first option and last option, respectively."

packages/mui-material/src/Autocomplete/Autocomplete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ Autocomplete.propTypes /* remove-proptypes */ = {
994994
* If provided, the options will be grouped under the returned string.
995995
* The groupBy value is also used as the text for group headings when `renderGroup` is not provided.
996996
*
997-
* @param {Value} options The options to group.
997+
* @param {Value} option The Autocomplete option.
998998
* @returns {string}
999999
*/
10001000
groupBy: PropTypes.func,

packages/mui-material/src/useAutocomplete/useAutocomplete.d.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import { PartiallyRequired } from '@mui/types';
23

34
export interface CreateFilterOptionsConfig<Value> {
45
ignoreAccents?: boolean;
@@ -178,7 +179,7 @@ export interface UseAutocompleteProps<
178179
* If provided, the options will be grouped under the returned string.
179180
* The groupBy value is also used as the text for group headings when `renderGroup` is not provided.
180181
*
181-
* @param {Value} options The options to group.
182+
* @param {Value} option The Autocomplete option.
182183
* @returns {string}
183184
*/
184185
groupBy?: (option: Value) => string;
@@ -359,8 +360,19 @@ export function useAutocomplete<
359360
DisableClearable extends boolean | undefined = false,
360361
FreeSolo extends boolean | undefined = false,
361362
>(
362-
props: UseAutocompleteProps<Value, Multiple, DisableClearable, FreeSolo>,
363-
): UseAutocompleteReturnValue<Value, Multiple, DisableClearable, FreeSolo>;
363+
props: PartiallyRequired<
364+
UseAutocompleteProps<Value, Multiple, DisableClearable, FreeSolo>,
365+
'groupBy'
366+
>,
367+
): UseAutocompleteReturnValue<Value, Multiple, DisableClearable, FreeSolo, true>;
368+
export function useAutocomplete<
369+
Value,
370+
Multiple extends boolean | undefined = false,
371+
DisableClearable extends boolean | undefined = false,
372+
FreeSolo extends boolean | undefined = false,
373+
>(
374+
props: Omit<UseAutocompleteProps<Value, Multiple, DisableClearable, FreeSolo>, 'groupBy'>,
375+
): UseAutocompleteReturnValue<Value, Multiple, DisableClearable, FreeSolo, false>;
364376

365377
export interface UseAutocompleteRenderedOption<Value> {
366378
option: Value;
@@ -372,6 +384,7 @@ export interface UseAutocompleteReturnValue<
372384
Multiple extends boolean | undefined = false,
373385
DisableClearable extends boolean | undefined = false,
374386
FreeSolo extends boolean | undefined = false,
387+
HasGroupBy extends boolean = false,
375388
> {
376389
/**
377390
* Resolver for the root slot's props.
@@ -460,9 +473,11 @@ export interface UseAutocompleteReturnValue<
460473
*/
461474
focusedTag: number;
462475
/**
463-
* The options to render. It's either `Value[]` or `AutocompleteGroupedOption<Value>[]` if the groupBy prop is provided.
476+
* The options to render.
477+
* - If `groupBy` is provided, the options are grouped and represented as `AutocompleteGroupedOption<Value>[]`.
478+
* - Otherwise, the options are represented as a flat array of `Value[]`.
464479
*/
465-
groupedOptions: Value[] | Array<AutocompleteGroupedOption<Value>>;
480+
groupedOptions: HasGroupBy extends true ? AutocompleteGroupedOption<Value>[] : Value[];
466481
}
467482

468483
export default useAutocomplete;

packages/mui-material/src/useAutocomplete/useAutocomplete.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { expectType } from '@mui/types';
2-
import { useAutocomplete, FilterOptionsState } from '@mui/material/useAutocomplete';
2+
import {
3+
useAutocomplete,
4+
FilterOptionsState,
5+
AutocompleteGroupedOption,
6+
} from '@mui/material/useAutocomplete';
37

48
interface Person {
59
id: string;
@@ -181,4 +185,17 @@ function Component() {
181185
},
182186
freeSolo: true,
183187
});
188+
189+
const ungroupedAutocomplete = useAutocomplete({ options: persons });
190+
expectType<Person[], typeof ungroupedAutocomplete.groupedOptions>(
191+
ungroupedAutocomplete.groupedOptions,
192+
);
193+
194+
const groupedAutocomplete = useAutocomplete({
195+
options: persons,
196+
groupBy: ({ id }) => id,
197+
});
198+
expectType<AutocompleteGroupedOption<Person>[], typeof groupedAutocomplete.groupedOptions>(
199+
groupedAutocomplete.groupedOptions,
200+
);
184201
}

0 commit comments

Comments
 (0)