Skip to content

Commit cdf1cd2

Browse files
[docs][autocomplete] Fix console in unstyled demo (#46804)
Signed-off-by: Olivier Tassinari <oliviertassinari@users.noreply.github.com> Co-authored-by: Zeeshan Tamboli <zeeshan.tamboli@gmail.com>
1 parent 6dc327e commit cdf1cd2

File tree

4 files changed

+78
-27
lines changed

4 files changed

+78
-27
lines changed

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

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const InputWrapper = styled('div')(({ theme }) => ({
6464
},
6565
}));
6666

67-
function Tag(props) {
67+
function Item(props) {
6868
const { label, onDelete, ...other } = props;
6969
return (
7070
<div {...other}>
@@ -74,12 +74,12 @@ function Tag(props) {
7474
);
7575
}
7676

77-
Tag.propTypes = {
77+
Item.propTypes = {
7878
label: PropTypes.string.isRequired,
7979
onDelete: PropTypes.func.isRequired,
8080
};
8181

82-
const StyledTag = styled(Tag)(({ theme }) => ({
82+
const StyledItem = styled(Item)(({ theme }) => ({
8383
display: 'flex',
8484
alignItems: 'center',
8585
height: '24px',
@@ -163,24 +163,21 @@ const Listbox = styled('ul')(({ theme }) => ({
163163
},
164164
}));
165165

166-
export default function CustomizedHook() {
166+
function CustomAutocomplete(props) {
167167
const {
168168
getRootProps,
169169
getInputLabelProps,
170170
getInputProps,
171-
getTagProps,
171+
getItemProps,
172172
getListboxProps,
173173
getOptionProps,
174174
groupedOptions,
175175
value,
176176
focused,
177177
setAnchorEl,
178178
} = useAutocomplete({
179-
id: 'customized-hook-demo',
180-
defaultValue: [top100Films[1]],
181179
multiple: true,
182-
options: top100Films,
183-
getOptionLabel: (option) => option.title,
180+
...props,
184181
});
185182

186183
return (
@@ -189,8 +186,14 @@ export default function CustomizedHook() {
189186
<Label {...getInputLabelProps()}>Customized hook</Label>
190187
<InputWrapper ref={setAnchorEl} className={focused ? 'focused' : ''}>
191188
{value.map((option, index) => {
192-
const { key, ...tagProps } = getTagProps({ index });
193-
return <StyledTag key={key} {...tagProps} label={option.title} />;
189+
const { key, ...itemProps } = getItemProps({ index });
190+
return (
191+
<StyledItem
192+
key={key}
193+
{...itemProps}
194+
label={props.getOptionLabel(option)}
195+
/>
196+
);
194197
})}
195198
<input {...getInputProps()} />
196199
</InputWrapper>
@@ -201,7 +204,7 @@ export default function CustomizedHook() {
201204
const { key, ...optionProps } = getOptionProps({ option, index });
202205
return (
203206
<li key={key} {...optionProps}>
204-
<span>{option.title}</span>
207+
<span>{props.getOptionLabel(option)}</span>
205208
<CheckIcon fontSize="small" />
206209
</li>
207210
);
@@ -212,6 +215,31 @@ export default function CustomizedHook() {
212215
);
213216
}
214217

218+
CustomAutocomplete.propTypes = {
219+
/**
220+
* Used to determine the string value for a given option.
221+
* It's used to fill the input (and the list box options if `renderOption` is not provided).
222+
*
223+
* If used in free solo mode, it must accept both the type of the options and a string.
224+
*
225+
* @param {Value} option
226+
* @returns {string}
227+
* @default (option) => option.label ?? option
228+
*/
229+
getOptionLabel: PropTypes.func,
230+
};
231+
232+
export default function CustomizedHook() {
233+
return (
234+
<CustomAutocomplete
235+
id="customized-hook-demo"
236+
defaultValue={[top100Films[1]]}
237+
options={top100Films}
238+
getOptionLabel={(option) => option.title}
239+
/>
240+
);
241+
}
242+
215243
// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
216244
const top100Films = [
217245
{ title: 'The Shawshank Redemption', year: 1994 },

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import useAutocomplete, {
3-
AutocompleteGetTagProps,
3+
AutocompleteGetItemProps,
4+
UseAutocompleteProps,
45
} from '@mui/material/useAutocomplete';
56
import CheckIcon from '@mui/icons-material/Check';
67
import CloseIcon from '@mui/icons-material/Close';
@@ -65,11 +66,11 @@ const InputWrapper = styled('div')(({ theme }) => ({
6566
},
6667
}));
6768

68-
interface TagProps extends ReturnType<AutocompleteGetTagProps> {
69+
interface ItemProps extends ReturnType<AutocompleteGetItemProps<true>> {
6970
label: string;
7071
}
7172

72-
function Tag(props: TagProps) {
73+
function Item(props: ItemProps) {
7374
const { label, onDelete, ...other } = props;
7475
return (
7576
<div {...other}>
@@ -79,7 +80,7 @@ function Tag(props: TagProps) {
7980
);
8081
}
8182

82-
const StyledTag = styled(Tag)<TagProps>(({ theme }) => ({
83+
const StyledItem = styled(Item)<ItemProps>(({ theme }) => ({
8384
display: 'flex',
8485
alignItems: 'center',
8586
height: '24px',
@@ -163,34 +164,39 @@ const Listbox = styled('ul')(({ theme }) => ({
163164
},
164165
}));
165166

166-
export default function CustomizedHook() {
167+
function CustomAutocomplete<Value>(
168+
props: UseAutocompleteProps<Value, true, false, false>,
169+
) {
167170
const {
168171
getRootProps,
169172
getInputLabelProps,
170173
getInputProps,
171-
getTagProps,
174+
getItemProps,
172175
getListboxProps,
173176
getOptionProps,
174177
groupedOptions,
175178
value,
176179
focused,
177180
setAnchorEl,
178181
} = useAutocomplete({
179-
id: 'customized-hook-demo',
180-
defaultValue: [top100Films[1]],
181182
multiple: true,
182-
options: top100Films,
183-
getOptionLabel: (option) => option.title,
183+
...props,
184184
});
185185

186186
return (
187187
<Root>
188188
<div {...getRootProps()}>
189189
<Label {...getInputLabelProps()}>Customized hook</Label>
190190
<InputWrapper ref={setAnchorEl} className={focused ? 'focused' : ''}>
191-
{value.map((option: FilmOptionType, index: number) => {
192-
const { key, ...tagProps } = getTagProps({ index });
193-
return <StyledTag key={key} {...tagProps} label={option.title} />;
191+
{value.map((option, index) => {
192+
const { key, ...itemProps } = getItemProps({ index });
193+
return (
194+
<StyledItem
195+
key={key}
196+
{...itemProps}
197+
label={props.getOptionLabel!(option)}
198+
/>
199+
);
194200
})}
195201
<input {...getInputProps()} />
196202
</InputWrapper>
@@ -201,7 +207,7 @@ export default function CustomizedHook() {
201207
const { key, ...optionProps } = getOptionProps({ option, index });
202208
return (
203209
<li key={key} {...optionProps}>
204-
<span>{option.title}</span>
210+
<span>{props.getOptionLabel!(option)}</span>
205211
<CheckIcon fontSize="small" />
206212
</li>
207213
);
@@ -212,6 +218,17 @@ export default function CustomizedHook() {
212218
);
213219
}
214220

221+
export default function CustomizedHook() {
222+
return (
223+
<CustomAutocomplete<FilmOptionType>
224+
id="customized-hook-demo"
225+
defaultValue={[top100Films[1]]}
226+
options={top100Films}
227+
getOptionLabel={(option) => option.title}
228+
/>
229+
);
230+
}
231+
215232
interface FilmOptionType {
216233
title: string;
217234
year: number;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<CustomAutocomplete<FilmOptionType>
2+
id="customized-hook-demo"
3+
defaultValue={[top100Films[1]]}
4+
options={top100Films}
5+
getOptionLabel={(option) => option.title}
6+
/>

docs/data/material/components/autocomplete/autocomplete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ import useAutocomplete from '@mui/material/useAutocomplete';
181181

182182
### Customized hook
183183

184-
{{"demo": "CustomizedHook.js"}}
184+
{{"demo": "CustomizedHook.js", "defaultCodeOpen": false}}
185185

186186
Head to the [customization](#customization) section for an example with the `Autocomplete` component instead of the hook.
187187

0 commit comments

Comments
 (0)