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

Add props and examples for the combobox #660

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
93 changes: 93 additions & 0 deletions src-docs/src/views/combo_box/combo_box_custom_icon.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]]);
Copy link
Member

@bandinib-amzn bandinib-amzn Apr 8, 2023

Choose a reason for hiding this comment

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

Why we choose option 2 and option 4? If they are default values, can we store them in constant and add comment so that in future if want to decide change the default values it will be easier for other developers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These examples are just to demo icon prop usage, I think moving to common won't be the right thing because consumers when use the example will also need to be aware and refer to common code stored which would be else where.


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}
icon="menu"
/>
);
};
93 changes: 93 additions & 0 deletions src-docs/src/views/combo_box/combo_box_default_icon.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]);
};
Copy link
Member

Choose a reason for hiding this comment

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

Code from Line 16 to Line 80 is same in both combo_box_custome_icon.js and combo_box_default_icon.js. Is it possible to reuse the code?

Copy link
Contributor Author

@AbhishekReddy1127 AbhishekReddy1127 Apr 12, 2023

Choose a reason for hiding this comment

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

These changes are just for demonstration purpose, examples on how to combo box with custom icon and and default search icon.
It not basically part of actual implementation. If we re-use then i think the example snippet will be disturbed...

Copy link
Collaborator

Choose a reason for hiding this comment

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

I remember seeing some of these sample codes shared between examples. We could look at combining them after merging this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Let's create an issue to refactor this code and avoid duplication and reuse examples. Approving this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks. Created: #830


return (
<OuiComboBox
placeholder="Select or create options"
options={options}
selectedOptions={selectedOptions}
onChange={onChange}
onCreateOption={onCreateOption}
isClearable={true}
icon
/>
);
};
66 changes: 66 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 @@ -218,6 +218,28 @@ const clearOnBlurSnippet = `<OuiComboBox
clearOnBlur={true}
/>`;

import ComboBoxDefaultIcon from './combo_box_default_icon';
const comboBoxDefaultIconSource = require('!!raw-loader!./combo_box_default_icon');
const comboBoxDefaultIconSourceOptionsHtml = renderToHtml(ComboBoxDefaultIcon);
const comboBoxDefaultIconSnippet = `<OuiComboBox
placeholder="Select one or more options"
options={options}
onChange={onChange}
onSearchChange={onSearchChange}
icon={true}
/>`;

import ComboBoxCustomIcon from './combo_box_custom_icon';
const comboBoxCustomIconSource = require('!!raw-loader!./combo_box_custom_icon');
const comboBoxCustomIconSourceOptionsHtml = renderToHtml(ComboBoxCustomIcon);
const comboBoxCustomIconSnippet = `<OuiComboBox
placeholder="Select one or more options"
options={options}
onChange={onChange}
onSearchChange={onSearchChange}
icon="menu"
/>`;

export const ComboBoxExample = {
title: 'Combo box',
intro: (
Expand Down Expand Up @@ -634,5 +656,49 @@ export const ComboBoxExample = {
snippet: clearOnBlurSnippet,
demo: <ClearOnBlur />,
},
{
title: 'Combox box default icon',
source: [
{
type: GuideSectionTypes.JS,
code: comboBoxDefaultIconSource,
},
{
type: GuideSectionTypes.HTML,
code: comboBoxDefaultIconSourceOptionsHtml,
},
],
text: (
<p>
Set the prop <OuiCode>icon</OuiCode> to make the combo box input text
appear with a default search icon.
</p>
),
props: { OuiComboBox, OuiComboBoxOptionOption },
snippet: comboBoxDefaultIconSnippet,
demo: <ComboBoxDefaultIcon />,
},
{
title: 'Combox box custom icon',
source: [
{
type: GuideSectionTypes.JS,
code: comboBoxCustomIconSource,
},
{
type: GuideSectionTypes.HTML,
code: comboBoxCustomIconSourceOptionsHtml,
},
],
text: (
<p>
Set the prop <OuiCode>icon</OuiCode> with a valid IconType to make the
combo box input text appear with an given icon type.
</p>
),
props: { OuiComboBox, OuiComboBoxOptionOption },
snippet: comboBoxCustomIconSnippet,
demo: <ComboBoxCustomIcon />,
},
],
};
Loading