Skip to content

Commit 39063ab

Browse files
authored
[Autocomplete] Fix "fixed tags" demo (#20687)
1 parent b9ddd6b commit 39063ab

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

docs/src/pages/components/autocomplete/FixedTags.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,29 @@ import TextField from '@material-ui/core/TextField';
55
import Autocomplete from '@material-ui/lab/Autocomplete';
66

77
export default function FixedTags() {
8+
const fixedOptions = [top100Films[6]];
9+
const [value, setValue] = React.useState([...fixedOptions, top100Films[13]]);
10+
811
return (
912
<Autocomplete
1013
multiple
1114
id="fixed-tags-demo"
15+
value={value}
16+
onChange={(event, newValue) => {
17+
setValue([
18+
...fixedOptions,
19+
...newValue.filter((option) => fixedOptions.indexOf(option) === -1),
20+
]);
21+
}}
1222
options={top100Films}
1323
getOptionLabel={(option) => option.title}
14-
defaultValue={[top100Films[6], top100Films[13]]}
15-
renderTags={(value, getTagProps) =>
16-
value.map((option, index) => (
17-
<Chip label={option.title} {...getTagProps({ index })} disabled={index === 0} />
24+
renderTags={(tagValue, getTagProps) =>
25+
tagValue.map((option, index) => (
26+
<Chip
27+
label={option.title}
28+
{...getTagProps({ index })}
29+
disabled={fixedOptions.indexOf(option) !== -1}
30+
/>
1831
))
1932
}
2033
style={{ width: 500 }}

docs/src/pages/components/autocomplete/FixedTags.tsx

+17-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,29 @@ import TextField from '@material-ui/core/TextField';
55
import Autocomplete from '@material-ui/lab/Autocomplete';
66

77
export default function FixedTags() {
8+
const fixedOptions = [top100Films[6]];
9+
const [value, setValue] = React.useState([...fixedOptions, top100Films[13]]);
10+
811
return (
912
<Autocomplete
1013
multiple
1114
id="fixed-tags-demo"
15+
value={value}
16+
onChange={(event, newValue) => {
17+
setValue([
18+
...fixedOptions,
19+
...newValue.filter((option) => fixedOptions.indexOf(option) === -1),
20+
]);
21+
}}
1222
options={top100Films}
1323
getOptionLabel={(option) => option.title}
14-
defaultValue={[top100Films[6], top100Films[13]]}
15-
renderTags={(value, getTagProps) =>
16-
value.map((option, index) => (
17-
<Chip label={option.title} {...getTagProps({ index })} disabled={index === 0} />
24+
renderTags={(tagValue, getTagProps) =>
25+
tagValue.map((option, index) => (
26+
<Chip
27+
label={option.title}
28+
{...getTagProps({ index })}
29+
disabled={fixedOptions.indexOf(option) !== -1}
30+
/>
1831
))
1932
}
2033
style={{ width: 500 }}

0 commit comments

Comments
 (0)