-
Notifications
You must be signed in to change notification settings - Fork 841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[EuiComboBox] Allow options to have a unique key #4048
Changes from 6 commits
edd5d4e
71636b6
02f2de7
48d0d63
af26b6f
094b5bf
0d1801c
af0d0a8
070aeca
33041b6
d7e8665
4f208bc
c866a0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { EuiComboBox } from '../../../../src/components'; | ||
|
||
const optionsStatic = [ | ||
{ | ||
label: 'Titan', | ||
id: 'titan1', | ||
}, | ||
{ | ||
label: 'Titan', | ||
id: 'titan2', | ||
}, | ||
{ | ||
label: 'Enceladus is disabled', | ||
disabled: true, | ||
}, | ||
{ | ||
label: 'Titan', | ||
id: '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 ( | ||
/* DisplayToggles wrapper for Docs only */ | ||
<EuiComboBox | ||
placeholder="Select or create options" | ||
options={options} | ||
selectedOptions={selectedOptions} | ||
onChange={onChange} | ||
onCreateOption={onCreateOption} | ||
isClearable={true} | ||
data-test-subj="demoComboBox" | ||
/> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,6 +183,10 @@ const startingWithSnippet = `<EuiComboBox | |
isClearable={true} | ||
/>`; | ||
|
||
import DuplicateOptions from './combo_box_duplicates'; | ||
const duplicateOptionsSource = require('!!raw-loader!./combo_box_duplicates'); | ||
const duplicateOptionsHtml = renderToHtml(DuplicateOptions); | ||
|
||
This comment was marked as outdated.
Sorry, something went wrong. |
||
export const ComboBoxExample = { | ||
title: 'Combo box', | ||
intro: ( | ||
|
@@ -199,13 +203,15 @@ export const ComboBoxExample = { | |
|
||
<EuiSpacer /> | ||
|
||
<EuiCallOut title="No duplicate option labels allowed" color="warning"> | ||
<EuiCallOut title="Duplicate labels require an id" color="warning"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's actually remove this entire callout now that we have a specific example section that explains this more succinctly. |
||
<p> | ||
The combo box will have errors if any of the options you pass to it | ||
share the same label property. It’s OK if options have duplicate | ||
values, though. This is because the label is the only thing the combo | ||
box is concerned about, since this is what the user sees and what is | ||
matched against when the user searches. | ||
The combo box will have errors by default if any of the options you | ||
pass to it share the same label property. It’s OK if options | ||
have duplicate values, though. This is because the label is the only | ||
thing the combo box is concerned about, since this is what the user | ||
sees and what is matched against when the user searches. You can pass | ||
an unique <EuiCode>id</EuiCode> to an option if you require duplicate | ||
labels. See the example below. | ||
</p> | ||
</EuiCallOut> | ||
|
||
|
@@ -567,5 +573,28 @@ export const ComboBoxExample = { | |
snippet: startingWithSnippet, | ||
demo: <StartingWith />, | ||
}, | ||
{ | ||
title: 'Duplicate labels', | ||
source: [ | ||
{ | ||
type: GuideSectionTypes.JS, | ||
code: duplicateOptionsSource, | ||
}, | ||
{ | ||
type: GuideSectionTypes.HTML, | ||
code: duplicateOptionsHtml, | ||
}, | ||
], | ||
text: ( | ||
<p> | ||
If you want to use options with a duplicate label (which in general is | ||
chandlerprall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
not recommended since the user has no way to distinguish those | ||
options), you need to set a unique <EuiCode language="js">id</EuiCode>{' '} | ||
for each option. | ||
timroes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</p> | ||
), | ||
props: { EuiComboBox }, | ||
demo: <DuplicateOptions />, | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
}, | ||
], | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this comment as it doesn't apply here.