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

OUI combo box refine #183

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
93 changes: 93 additions & 0 deletions src-docs/src/views/combo_box/clear_on_blur.js
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}
/>
);
};
34 changes: 34 additions & 0 deletions src-docs/src/views/combo_box/combo_box_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const comboBoxSnippet = `<OuiComboBox
onChange={onChange}
onCreateOption={onCreateOption}
isClearable={true}
clearOnBlur={true}
/>`;

import Containers from './containers';
Expand Down Expand Up @@ -206,6 +207,17 @@ const duplicateOptionsSnippet = `const options = [{
key: 'Label2',
}]`;

import ClearOnBlur from './clear_on_blur';
const clearOnBlurSource = require('!!raw-loader!./clear_on_blur');
const clearOnBlurSourceOptionsHtml = renderToHtml(ClearOnBlur);
const clearOnBlurSnippet = `<OuiComboBox
placeholder="Select one or more options"
options={options}
onChange={onChange}
onSearchChange={onSearchChange}
clearOnBlur={true}
/>`;

export const ComboBoxExample = {
title: 'Combo box',
intro: (
Expand Down Expand Up @@ -600,5 +612,27 @@ export const ComboBoxExample = {
demo: <DuplicateOptions />,
snippet: duplicateOptionsSnippet,
},
{
title: 'Clear on blur',
source: [
{
type: GuideSectionTypes.JS,
code: clearOnBlurSource,
},
{
type: GuideSectionTypes.HTML,
code: clearOnBlurSourceOptionsHtml,
},
],
text: (
<p>
Set the prop <OuiCode>clearOnBlur</OuiCode> to make the combo box
input text clear when user focuses out of text box.
</p>
),
props: { OuiComboBox, OuiComboBoxOptionOption },
snippet: clearOnBlurSnippet,
demo: <ClearOnBlur />,
},
],
};
16 changes: 15 additions & 1 deletion src/components/combo_box/combo_box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ export interface _OuiComboBoxProps<T>
* Specifies that the input should have focus when the component loads
*/
autoFocus?: boolean;
/**
* When `true` clears the input text when user focus out of the input box
*/
clearOnBlur?: boolean;
}

/**
Expand Down Expand Up @@ -464,10 +468,20 @@ export class OuiComboBox<T> extends Component<
options,
selectedOptions,
singleSelection,
clearOnBlur,
} = this.props;

const { matchingOptions } = this.state;

const { hasFocus, isListOpen } = this.state;
if (
clearOnBlur &&
searchValue &&
(hasFocus === false || isListOpen === false)
) {
this.clearSearchValue();
return;
}

if (this.doesSearchMatchOnlyOption()) {
this.onAddOption(matchingOptions[0], isContainerBlur);
return;
Expand Down