-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(EuiComboBox): add EuiDocs example
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { EuiComboBox } from '../../../../src/components'; | ||
import { DisplayToggles } from '../form_controls/display_toggles'; | ||
|
||
const optionsStatic = [ | ||
{ | ||
label: 'Titan', | ||
'data-test-subj': 'titanOption', | ||
toolTipContent: 'Lorem ipsum', | ||
}, | ||
{ | ||
label: 'Enceladus is disabled', | ||
disabled: true, | ||
toolTipContent: 'Lorem ipsum', | ||
}, | ||
{ | ||
label: 'Mimas', | ||
toolTipContent: 'Lorem ipsum', | ||
}, | ||
{ | ||
label: 'Dione', | ||
toolTipContent: 'Lorem ipsum', | ||
}, | ||
{ | ||
label: 'Iapetus', | ||
toolTipContent: 'Lorem ipsum', | ||
}, | ||
{ | ||
label: 'Phoebe', | ||
toolTipContent: 'Lorem ipsum', | ||
}, | ||
{ | ||
label: 'Rhea', | ||
toolTipContent: 'Lorem ipsum', | ||
}, | ||
{ | ||
label: | ||
"Pandora is one of Saturn's moons, named for a Titaness of Greek mythology", | ||
toolTipContent: 'Lorem ipsum', | ||
}, | ||
{ | ||
label: 'Tethys', | ||
toolTipContent: 'Lorem ipsum', | ||
}, | ||
{ | ||
label: 'Hyperion', | ||
toolTipContent: 'Lorem ipsum', | ||
}, | ||
]; | ||
export default () => { | ||
const [options, setOptions] = useState(optionsStatic); | ||
const [selectedOptions, setSelected] = useState([options[2], options[4]]); | ||
|
||
const onChange = (selectedOptions) => { | ||
setSelected(selectedOptions); | ||
}; | ||
|
||
const onCreateOption = (searchValue, flattenedOptions = []) => { | ||
const normalizedSearchValue = searchValue.trim().toLowerCase(); | ||
|
||
if (!normalizedSearchValue) { | ||
return; | ||
} | ||
|
||
const newOption = { | ||
label: searchValue, | ||
}; | ||
|
||
// Create the option if it doesn't exist. | ||
if ( | ||
flattenedOptions.findIndex( | ||
(option) => option.label.trim().toLowerCase() === normalizedSearchValue | ||
) === -1 | ||
) { | ||
setOptions([...options, newOption]); | ||
} | ||
|
||
// Select the option. | ||
setSelected([...selectedOptions, newOption]); | ||
}; | ||
|
||
return ( | ||
/* DisplayToggles wrapper for Docs only */ | ||
<DisplayToggles canDisabled={false} canReadOnly={false} canIsDisabled> | ||
<EuiComboBox | ||
aria-label="Accessible screen reader label" | ||
placeholder="Select or create options" | ||
options={options} | ||
selectedOptions={selectedOptions} | ||
onChange={onChange} | ||
onCreateOption={onCreateOption} | ||
isClearable={true} | ||
data-test-subj="demoComboBox" | ||
autoFocus | ||
/> | ||
</DisplayToggles> | ||
); | ||
}; |