Skip to content

Commit f3d4e57

Browse files
committed
Fix groups example
- remove a ton of now-unnecessary lowercase normalization checks that EuiComboBox handles automatically - had a `custom` undefined error on every new custom creation that was due to too many `setAllOptions` calls - use a callback and an `=` destructure fallback to fix all those things
1 parent 30921d1 commit f3d4e57

1 file changed

Lines changed: 9 additions & 34 deletions

File tree

src-docs/src/views/combo_box/groups.js

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ const soundGroup = {
3838
],
3939
};
4040

41-
const allOptionsStatic = [colorGroup, soundGroup];
42-
4341
export default () => {
44-
const [allOptions, setAllOptions] = useState(allOptionsStatic);
42+
const [allOptions, setAllOptions] = useState([colorGroup, soundGroup]);
4543
const [selectedOptions, setSelected] = useState([
4644
colorGroup.options[2],
4745
soundGroup.options[3],
@@ -51,54 +49,31 @@ export default () => {
5149
setSelected(selectedOptions);
5250
};
5351

54-
const onCreateOption = (searchValue, flattenedOptions = []) => {
55-
if (!searchValue) {
56-
return;
57-
}
58-
59-
const normalizedSearchValue = searchValue.trim().toLowerCase();
60-
61-
if (!normalizedSearchValue) {
62-
return;
63-
}
64-
52+
const onCreateOption = (searchValue) => {
6553
const newOption = {
6654
label: searchValue,
6755
};
6856

69-
// Create the option if it doesn't exist.
70-
if (
71-
flattenedOptions.findIndex(
72-
(option) => option.label.trim().toLowerCase() === normalizedSearchValue
73-
) === -1
74-
) {
75-
if (allOptions[allOptions.length - 1].label !== 'Custom') {
76-
setAllOptions([
77-
...allOptions,
78-
{
79-
label: 'Custom',
80-
options: [],
81-
},
82-
]);
83-
}
84-
const [colors, sounds, custom] = allOptions;
85-
setAllOptions([
57+
setAllOptions((allOptions) => {
58+
const [colors, sounds, custom = { label: 'Custom', options: [] }] =
59+
allOptions;
60+
return [
8661
colors,
8762
sounds,
8863
{
8964
...custom,
9065
options: [...custom.options, newOption],
9166
},
92-
]);
93-
}
67+
];
68+
});
9469

9570
// Select the option.
9671
setSelected([...selectedOptions, newOption]);
9772
};
9873

9974
return (
10075
<EuiComboBox
101-
aria-label="Accessible screen reader label"
76+
aria-label="EuiComboBox example with groups"
10277
placeholder="These options are grouped"
10378
options={allOptions}
10479
selectedOptions={selectedOptions}

0 commit comments

Comments
 (0)