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

adjusted files with combo_box & icon.js to avoid duplication #1165

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 4 additions & 23 deletions src-docs/src/views/combo_box/combo_box_custom_icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
*/

import React, { useState } from 'react';

import { OnCreateOption as comboBoxOnCreateOption } from './combo_box_icon'; // Rename the import to avoid naming conflicts
import { OuiComboBox } from '../../../../src/components';

export default () => {
const [options, updateOptions] = useState([
const [options] = useState([
{
label: 'Titan',
'data-test-subj': 'titanOption',
Expand Down Expand Up @@ -56,27 +56,8 @@ export default () => {
};

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]);
// Call the OnCreateOption function from combo_box_icon.js
comboBoxOnCreateOption(searchValue, flattenedOptions, options, setSelected);
};

return (
Expand Down
27 changes: 4 additions & 23 deletions src-docs/src/views/combo_box/combo_box_default_icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
*/

import React, { useState } from 'react';

import { OnCreateOption as comboBoxOnCreateOption } from './combo_box_icon'; // Rename the import to avoid naming conflicts
import { OuiComboBox } from '../../../../src/components';

export default () => {
const [options, updateOptions] = useState([
const [options] = useState([
{
label: 'Titan',
'data-test-subj': 'titanOption',
Expand Down Expand Up @@ -56,27 +56,8 @@ export default () => {
};

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]);
// Pass options and setSelected as parameters to OnCreateOption
comboBoxOnCreateOption(searchValue, flattenedOptions, options, setSelected);
};

return (
Expand Down
57 changes: 30 additions & 27 deletions src-docs/src/views/combo_box/combo_box_icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

import React, { useState } from 'react';

import { OuiComboBox } from '../../../../src/components';

const staticOptions = [
Expand Down Expand Up @@ -49,38 +48,40 @@ const staticOptions = [
},
];

export default () => {
const OnCreateOption = (searchValue, flattenedOptions = []) => {
const [options, setOptions] = useState(staticOptions);
const [selectedOptions, setSelected] = useState([options[2], options[4]]);
const normalizedSearchValue = searchValue.trim().toLowerCase();
const [setSelected] = useState([options[2], options[4]]);

const onChange = (selectedOptions) => {
setSelected(selectedOptions);
};
if (!normalizedSearchValue) {
return;
}

const onCreateOption = (searchValue, flattenedOptions = []) => {
const normalizedSearchValue = searchValue.trim().toLowerCase();
const newOption = {
label: searchValue,
};

if (!normalizedSearchValue) {
return;
}
// Create the option if it doesn't exist.
if (
flattenedOptions.findIndex(
(option) => option.label.trim().toLowerCase() === normalizedSearchValue
) === -1
) {
setOptions([...options, newOption]);
}

const newOption = {
label: searchValue,
};
// 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]);
};

// Create the option if it doesn't exist.
if (
flattenedOptions.findIndex(
(option) => option.label.trim().toLowerCase() === normalizedSearchValue
) === -1
) {
setOptions([...options, newOption]);
}
const ComboBoxWithOption = () => {
const [options] = useState(staticOptions);
const [selectedOptions, setSelected] = useState([options[2], options[4]]);

// 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]);
const onChange = (selectedOptions) => {
setSelected(selectedOptions);
};

return (
Expand All @@ -90,7 +91,9 @@ export default () => {
icon={true}
selectedOptions={selectedOptions}
onChange={onChange}
onCreateOption={onCreateOption}
onCreateOption={OnCreateOption}
/>
);
};

export { ComboBoxWithOption, OnCreateOption };
Loading