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

[OuiComboBox] Add docs for icon and revise example for clearOnBlur #806

Merged
merged 1 commit into from
Jun 20, 2023
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
32 changes: 3 additions & 29 deletions src-docs/src/views/combo_box/clear_on_blur.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import React, { useState } from 'react';
import { OuiComboBox } from '../../../../src/components';

export default () => {
const [options, updateOptions] = useState([
const [options] = useState([
{
label: 'Titan',
'data-test-subj': 'titanOption',
Expand Down Expand Up @@ -55,38 +55,12 @@ export default () => {
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"
placeholder="Select one or more options"
options={options}
selectedOptions={selectedOptions}
onChange={onChange}
onCreateOption={onCreateOption}
isClearable={true}
selectedOptions={selectedOptions}
clearOnBlur={true}
/>
);
Expand Down
37 changes: 36 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 @@ -182,6 +182,19 @@ const delimiterSnippet = `<OuiComboBox
isClearable={true}
/>`;

import WithIcon from './combo_box_icon';
const withIconSource = require('!!raw-loader!./combo_box_icon');
const withIconHtml = renderToHtml(WithIcon);
const withIconSnippet = `<OuiComboBox
placeholder="Select or create options"
options={options}
icon={true}
selectedOptions={selectedOptions}
onChange={onChange}
onCreateOption={onCreateOption}
isClearable={true}
/>`;

import StartingWith from './startingWith';
const startingWithSource = require('!!raw-loader!./startingWith');
const startingWithHtml = renderToHtml(StartingWith);
Expand Down Expand Up @@ -214,7 +227,6 @@ const clearOnBlurSnippet = `<OuiComboBox
placeholder="Select one or more options"
options={options}
onChange={onChange}
onSearchChange={onSearchChange}
clearOnBlur={true}
/>`;

Expand Down Expand Up @@ -563,6 +575,29 @@ export const ComboBoxExample = {
snippet: delimiterSnippet,
demo: <Delimiter />,
},
{
title: 'With icon',
source: [
{
type: GuideSectionTypes.JS,
code: withIconSource,
},
{
type: GuideSectionTypes.HTML,
code: withIconHtml,
},
],
text: (
<p>
Pass an <OuiCode>IconType</OuiCode> string to show the icon in the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do we want a link to the icon docs for IconType?

combo box, or set it to <OuiCode>true</OuiCode> to show the search
icon.
</p>
),
props: { OuiComboBox, OuiComboBoxOptionOption },
snippet: withIconSnippet,
demo: <WithIcon />,
},
{
title: 'Sorting matches',
source: [
Expand Down
96 changes: 96 additions & 0 deletions src-docs/src/views/combo_box/combo_box_icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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';

const staticOptions = [
{
label: 'Titan',
'data-test-subj': 'titanOption',
},
{
label: 'Enceladus is disabled',
disabled: true,
},
{
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',
},
];

export default () => {
const [options, setOptions] = useState(staticOptions);
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.
// Use the previousState parameter (prevSelected) from the setState
// instance (setSelected) to ensure looped calls do not override each other
setSelected((prevSelected) => [...prevSelected, newOption]);
};

return (
<OuiComboBox
placeholder="Select or create options"
options={options}
icon={true}
selectedOptions={selectedOptions}
onChange={onChange}
onCreateOption={onCreateOption}
/>
);
};