From af0883cb0b87fe93fb3f6c6bd5aaafc8014880c0 Mon Sep 17 00:00:00 2001 From: Abhishek Reddy <62020972+AbhishekReddy1127@users.noreply.github.com> Date: Mon, 19 Jun 2023 22:10:06 -0700 Subject: [PATCH] Add props and examples for the combobox (#660) Signed-off-by: AbhishekReddy1127 --- .../views/combo_box/combo_box_custom_icon.js | 93 +++++++++++++++++++ .../views/combo_box/combo_box_default_icon.js | 93 +++++++++++++++++++ .../src/views/combo_box/combo_box_example.js | 66 +++++++++++++ 3 files changed, 252 insertions(+) create mode 100644 src-docs/src/views/combo_box/combo_box_custom_icon.js create mode 100644 src-docs/src/views/combo_box/combo_box_default_icon.js diff --git a/src-docs/src/views/combo_box/combo_box_custom_icon.js b/src-docs/src/views/combo_box/combo_box_custom_icon.js new file mode 100644 index 0000000000..d67e28fbdf --- /dev/null +++ b/src-docs/src/views/combo_box/combo_box_custom_icon.js @@ -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 ( + + ); +}; diff --git a/src-docs/src/views/combo_box/combo_box_default_icon.js b/src-docs/src/views/combo_box/combo_box_default_icon.js new file mode 100644 index 0000000000..8bf84a0aee --- /dev/null +++ b/src-docs/src/views/combo_box/combo_box_default_icon.js @@ -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 ( + + ); +}; diff --git a/src-docs/src/views/combo_box/combo_box_example.js b/src-docs/src/views/combo_box/combo_box_example.js index 4b94785685..4212e8638f 100644 --- a/src-docs/src/views/combo_box/combo_box_example.js +++ b/src-docs/src/views/combo_box/combo_box_example.js @@ -218,6 +218,28 @@ const clearOnBlurSnippet = ``; +import ComboBoxDefaultIcon from './combo_box_default_icon'; +const comboBoxDefaultIconSource = require('!!raw-loader!./combo_box_default_icon'); +const comboBoxDefaultIconSourceOptionsHtml = renderToHtml(ComboBoxDefaultIcon); +const comboBoxDefaultIconSnippet = ``; + +import ComboBoxCustomIcon from './combo_box_custom_icon'; +const comboBoxCustomIconSource = require('!!raw-loader!./combo_box_custom_icon'); +const comboBoxCustomIconSourceOptionsHtml = renderToHtml(ComboBoxCustomIcon); +const comboBoxCustomIconSnippet = ``; + export const ComboBoxExample = { title: 'Combo box', intro: ( @@ -634,5 +656,49 @@ export const ComboBoxExample = { snippet: clearOnBlurSnippet, demo: , }, + { + title: 'Combox box default icon', + source: [ + { + type: GuideSectionTypes.JS, + code: comboBoxDefaultIconSource, + }, + { + type: GuideSectionTypes.HTML, + code: comboBoxDefaultIconSourceOptionsHtml, + }, + ], + text: ( +

+ Set the prop icon to make the combo box input text + appear with a default search icon. +

+ ), + props: { OuiComboBox, OuiComboBoxOptionOption }, + snippet: comboBoxDefaultIconSnippet, + demo: , + }, + { + title: 'Combox box custom icon', + source: [ + { + type: GuideSectionTypes.JS, + code: comboBoxCustomIconSource, + }, + { + type: GuideSectionTypes.HTML, + code: comboBoxCustomIconSourceOptionsHtml, + }, + ], + text: ( +

+ Set the prop icon with a valid IconType to make the + combo box input text appear with an given icon type. +

+ ), + props: { OuiComboBox, OuiComboBoxOptionOption }, + snippet: comboBoxCustomIconSnippet, + demo: , + }, ], };