Skip to content
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

Updated EuiComboBox to allow the options list to open for single selection custom options #3706

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Updated `EuiComboBox` to allow the options list to open for single selection custom options ([#3706](https://github.com/elastic/eui/pull/3706))
- Added `useEuiI18n` hook for localization ([#3749](https://github.com/elastic/eui/pull/3749))

**Bug fixes**
Expand Down
44 changes: 43 additions & 1 deletion src-docs/src/views/combo_box/combo_box_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,19 @@ const singleSelectionSnippet = `<EuiComboBox
options={options}
selectedOptions={selectedOptions}
onChange={onChange}
isClearable={false}
/>`;

import SingleSelectionCustomOptions from './single_selection_custom_options';
const singleSelectionCustomOptionsSource = require('!!raw-loader!./single_selection_custom_options');
const singleSelectionCustomOptionsHtml = renderToHtml(
SingleSelectionCustomOptions
);
const singleSelectionCustomOptionsSnippet = `<EuiComboBox
placeholder="Select a single option"
singleSelection={{ asPlainText: true }}
options={options}
selectedOptions={selectedOptions}
onChange={onChange}
miukimiu marked this conversation as resolved.
Show resolved Hide resolved
/>`;

import DisallowCustomOptions from './disallow_custom_options';
Expand Down Expand Up @@ -405,6 +417,36 @@ export const ComboBoxExample = {
snippet: singleSelectionSnippet,
demo: <SingleSelection />,
},
{
title: 'Single selection with custom options',
source: [
{
type: GuideSectionTypes.JS,
code: singleSelectionCustomOptionsSource,
},
{
type: GuideSectionTypes.HTML,
code: singleSelectionCustomOptionsHtml,
},
],
text: (
<Fragment>
<p>
You can allow the user to select a single option and also allow the
creation of custom options. To do that, use the{' '}
<EuiCode>singleSelection</EuiCode> in conjunction with the{' '}
<EuiCode>onCreateOption</EuiCode> prop.
</p>
<p>
<strong>Note:</strong> Creating custom options might not be obvious,
miukimiu marked this conversation as resolved.
Show resolved Hide resolved
so provide a help text explaining that this option is available.
miukimiu marked this conversation as resolved.
Show resolved Hide resolved
</p>
</Fragment>
),
props: { EuiComboBox },
snippet: singleSelectionCustomOptionsSnippet,
demo: <SingleSelectionCustomOptions />,
},
{
title: 'Disallowing custom options',
source: [
Expand Down
2 changes: 1 addition & 1 deletion src-docs/src/views/combo_box/single_selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default () => {
options={options}
selectedOptions={selectedOptions}
onChange={onChange}
isClearable={false}
isClearable={true}
miukimiu marked this conversation as resolved.
Show resolved Hide resolved
/>
</DisplayToggles>
);
Expand Down
68 changes: 68 additions & 0 deletions src-docs/src/views/combo_box/single_selection_custom_options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React, { useState } from 'react';

import { EuiComboBox, EuiFormRow } from '../../../../src/components';

const options = [
{
label: 'Software Developer',
'data-test-subj': 'softDevOption',
},
{
label: 'Mobile Developer',
},
{
label: 'Javascript Engineer',
},
{
label: 'UX Designer',
},
{
label: 'UI Designer',
},
{
label: 'Product Designer',
},
{
label: 'QA Engineer',
},
];

export default () => {
const [selectedOptions, setSelected] = useState([options[2]]);

const onChange = selectedOptions => {
// We should only get back either 0 or 1 options.
setSelected(selectedOptions);
};

const onCreateOption = (searchValue = []) => {
const normalizedSearchValue = searchValue.trim().toLowerCase();

if (!normalizedSearchValue) {
return;
}

const newOption = {
label: searchValue,
};

// Select the option.
setSelected([newOption]);
};

return (
<EuiFormRow
label="Your occupation"
helpText="Select an occupation from the list. If your occupation isn’t available, create a custom one.">
<EuiComboBox
placeholder="Select a single occupation"
singleSelection={{ asPlainText: true }}
options={options}
selectedOptions={selectedOptions}
onChange={onChange}
onCreateOption={onCreateOption}
isClearable={true}
/>
</EuiFormRow>
);
};
27 changes: 4 additions & 23 deletions src/components/combo_box/combo_box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,7 @@ export class EuiComboBox<T> extends Component<

this.clearSearchValue();

if (
this.isSingleSelectionCustomOption() ||
miukimiu marked this conversation as resolved.
Show resolved Hide resolved
(Boolean(singleSelection) && matchingOptions.length < 1)
) {
if (Boolean(singleSelection)) {
// Adding a custom option to a single select that does not appear in the list of options
this.closeList();
}
Expand Down Expand Up @@ -516,29 +513,13 @@ export class EuiComboBox<T> extends Component<
return flattenOptions.length === numberOfSelectedOptions;
};

isSingleSelectionCustomOption = () => {
const {
onCreateOption,
options,
selectedOptions,
singleSelection,
} = this.props;
// The selected option of a single select is custom and does not appear in the list of options
return (
Boolean(singleSelection) &&
onCreateOption &&
selectedOptions.length > 0 &&
!options.includes(selectedOptions[0])
);
};

onComboBoxFocus: FocusEventHandler<HTMLInputElement> = event => {
if (this.props.onFocus) {
this.props.onFocus(event);
}
if (!this.isSingleSelectionCustomOption()) {
this.openList();
}

this.openList();

this.setState({ hasFocus: true });
};

Expand Down