forked from opensearch-project/oui
-
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.
OUI combo box refine (opensearch-project#183)
* Added a prop clearOnBlur Signed-off-by: AbhishekReddy1127 <[email protected]> * Added example for prop clearOnBlur Signed-off-by: AbhishekReddy1127 <[email protected]> --------- Signed-off-by: AbhishekReddy1127 <[email protected]>
- Loading branch information
1 parent
9c8248d
commit 17f257f
Showing
3 changed files
with
142 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
|
||
import { OuiComboBox } from '../../../../src/components'; | ||
|
||
export default () => { | ||
const [options, updateOptions] = useState([ | ||
{ | ||
label: 'Titan', | ||
'data-test-subj': 'titanOption', | ||
}, | ||
{ | ||
label: 'Enceladus is disabled', | ||
}, | ||
{ | ||
label: 'Mimas', | ||
}, | ||
{ | ||
label: 'Dione', | ||
}, | ||
{ | ||
label: 'Iapetus', | ||
}, | ||
{ | ||
label: 'Phoebe', | ||
}, | ||
{ | ||
label: 'Rhea', | ||
}, | ||
{ | ||
label: | ||
"Pandora is one of Saturn's moons, named for a Titaness of Greek mythology", | ||
}, | ||
{ | ||
label: 'Tethys', | ||
}, | ||
{ | ||
label: 'Hyperion', | ||
}, | ||
]); | ||
|
||
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 | ||
) { | ||
updateOptions([...options, newOption]); | ||
} | ||
|
||
// Select the option. | ||
setSelected((prevSelected) => [...prevSelected, newOption]); | ||
}; | ||
|
||
return ( | ||
<OuiComboBox | ||
placeholder="Select or create options" | ||
options={options} | ||
selectedOptions={selectedOptions} | ||
onChange={onChange} | ||
onCreateOption={onCreateOption} | ||
isClearable={true} | ||
clearOnBlur={true} | ||
/> | ||
); | ||
}; |
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