forked from elastic/eui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EuiComboBox] Allow options to have a unique key (elastic#4048)
* Allow options in combo box to have an id * Change warning in documentation * Fix eslint error * Added changelog entry * Address review * Update src-docs/src/views/combo_box/combo_box_example.js Co-authored-by: Caroline Horn <549577+cchaos@users.noreply.github.com> * Address review feedback * Rename id to key * Address recently merged PRs changes Co-authored-by: Caroline Horn <549577+cchaos@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
162 additions
and
23 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,69 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { EuiComboBox } from '../../../../src/components'; | ||
|
||
const optionsStatic = [ | ||
{ | ||
label: 'Titan', | ||
key: 'titan1', | ||
}, | ||
{ | ||
label: 'Titan', | ||
key: 'titan2', | ||
}, | ||
{ | ||
label: 'Enceladus is disabled', | ||
disabled: true, | ||
}, | ||
{ | ||
label: 'Titan', | ||
key: 'titan3', | ||
}, | ||
{ | ||
label: 'Dione', | ||
}, | ||
]; | ||
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 ( | ||
<EuiComboBox | ||
placeholder="Select or create options" | ||
options={options} | ||
selectedOptions={selectedOptions} | ||
onChange={onChange} | ||
onCreateOption={onCreateOption} | ||
isClearable={true} | ||
data-test-subj="demoComboBox" | ||
/> | ||
); | ||
}; |
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
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
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
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