From 4ed9d69cd8d69c5165772f5b30ec0de364cb7eae Mon Sep 17 00:00:00 2001 From: Miki Date: Mon, 12 Jun 2023 11:53:27 -0700 Subject: [PATCH] [OuiComboBox] Add docs for icon and revise example for clearOnBlur Signed-off-by: Miki --- src-docs/src/views/combo_box/clear_on_blur.js | 32 +------ .../src/views/combo_box/combo_box_example.js | 37 ++++++- .../src/views/combo_box/combo_box_icon.js | 96 +++++++++++++++++++ 3 files changed, 135 insertions(+), 30 deletions(-) create mode 100644 src-docs/src/views/combo_box/combo_box_icon.js diff --git a/src-docs/src/views/combo_box/clear_on_blur.js b/src-docs/src/views/combo_box/clear_on_blur.js index 01c3585240..8bf6ba09b5 100644 --- a/src-docs/src/views/combo_box/clear_on_blur.js +++ b/src-docs/src/views/combo_box/clear_on_blur.js @@ -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', @@ -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 ( ); 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..452c25798a 100644 --- a/src-docs/src/views/combo_box/combo_box_example.js +++ b/src-docs/src/views/combo_box/combo_box_example.js @@ -182,6 +182,19 @@ const delimiterSnippet = ``; +import WithIcon from './combo_box_icon'; +const withIconSource = require('!!raw-loader!./combo_box_icon'); +const withIconHtml = renderToHtml(WithIcon); +const withIconSnippet = ``; + import StartingWith from './startingWith'; const startingWithSource = require('!!raw-loader!./startingWith'); const startingWithHtml = renderToHtml(StartingWith); @@ -214,7 +227,6 @@ const clearOnBlurSnippet = ``; @@ -563,6 +575,29 @@ export const ComboBoxExample = { snippet: delimiterSnippet, demo: , }, + { + title: 'With icon', + source: [ + { + type: GuideSectionTypes.JS, + code: withIconSource, + }, + { + type: GuideSectionTypes.HTML, + code: withIconHtml, + }, + ], + text: ( +

+ Pass an IconType string to show the icon in the + combo box, or set it to true to show the search + icon. +

+ ), + props: { OuiComboBox, OuiComboBoxOptionOption }, + snippet: withIconSnippet, + demo: , + }, { title: 'Sorting matches', source: [ diff --git a/src-docs/src/views/combo_box/combo_box_icon.js b/src-docs/src/views/combo_box/combo_box_icon.js new file mode 100644 index 0000000000..2108486293 --- /dev/null +++ b/src-docs/src/views/combo_box/combo_box_icon.js @@ -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 ( + + ); +};