Skip to content

Commit

Permalink
Updated EuiComboBox to allow the options list to open for single sele…
Browse files Browse the repository at this point in the history
…ction custom options (#3706)

* Fixing includes to return true when object exists in array

* changelog

* Allowing list to open for single selection custom options

* Updated changelog

* PR review

* Improving example

* Improving example

* Addind isClearable

* Improving examples

* Improving explanation text

* Adding note

* Addressing PR issues

* Update src-docs/src/views/combo_box/combo_box_example.js

Co-authored-by: Caroline Horn <549577+cchaos@users.noreply.github.com>

* Update src-docs/src/views/combo_box/combo_box_example.js

Co-authored-by: Caroline Horn <549577+cchaos@users.noreply.github.com>

* Snippet

Co-authored-by: Caroline Horn <549577+cchaos@users.noreply.github.com>
  • Loading branch information
miukimiu and cchaos authored Jul 17, 2020
1 parent 0c69cad commit 518419c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 25 deletions.
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
46 changes: 45 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,20 @@ 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}
onCreateOption={onCreateOption}
onChange={onChange}
/>`;

import DisallowCustomOptions from './disallow_custom_options';
Expand Down Expand Up @@ -405,6 +418,37 @@ 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
to the user, so provide help text explaining that this option is
available.
</p>
</Fragment>
),
props: { EuiComboBox },
snippet: singleSelectionCustomOptionsSnippet,
demo: <SingleSelectionCustomOptions />,
},
{
title: 'Disallowing custom options',
source: [
Expand Down
1 change: 0 additions & 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,6 @@ export default () => {
options={options}
selectedOptions={selectedOptions}
onChange={onChange}
isClearable={false}
/>
</DisplayToggles>
);
Expand Down
67 changes: 67 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,67 @@
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}
/>
</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() ||
(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

0 comments on commit 518419c

Please sign in to comment.