/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { forwardRef, useRef } from "react"; import { EuiFieldNumber, EuiFieldText, EuiSwitch, EuiSelect, EuiText, EuiCheckbox, EuiComboBoxOptionOption } from "@elastic/eui"; import EuiToolTipWrapper, { IEuiToolTipWrapperProps } from "../../EuiToolTipWrapper"; import EuiComboBox from "../../ComboBoxWithoutWarning"; export type ComponentMapEnum = "Input" | "Number" | "Switch" | "Select" | "Text" | "ComboBoxSingle" | "CheckBox" | "ComboBoxMultiple"; export interface IFieldComponentProps extends IEuiToolTipWrapperProps { onChange: (val: IFieldComponentProps["value"], ...args: any) => void; value?: any; [key: string]: any; } let globalId = 0; const componentMap: Record> = { Input: EuiToolTipWrapper( forwardRef(({ onChange, value, removeWhenEmpty, ...others }, ref: React.Ref) => ( onChange(e.target.value ? e.target.value : removeWhenEmpty ? undefined : e.target.value)} {...others} /> )) as React.ComponentType ), Number: EuiToolTipWrapper( forwardRef(({ onChange, value, removeWhenEmpty, ...others }, ref: React.Ref) => ( onChange(e.target.value ? e.target.value : removeWhenEmpty ? undefined : e.target.value)} value={value === undefined ? "" : value} {...others} /> )) as React.ComponentType ), Switch: EuiToolTipWrapper( forwardRef(({ value, onChange, ...others }, ref: React.Ref) => (
onChange(e.target.checked)} {...others} />
)) as React.ComponentType ), Text: forwardRef(({ value }, ref: React.Ref) => (
{value || "-"}
)) as React.ComponentType, Select: EuiToolTipWrapper( forwardRef(({ onChange, value, ...others }, ref: React.Ref) => ( onChange(e.target.value)} value={value || ""} {...others} /> )) as React.ComponentType ), CheckBox: EuiToolTipWrapper( forwardRef(({ onChange, value, ...others }, ref: React.Ref) => { const idRef = useRef(globalId++); return ( onChange(e.target.checked)} {...others} /> ); }) as React.ComponentType ), ComboBoxSingle: EuiToolTipWrapper( forwardRef(({ onChange, value, options, ...others }, ref: React.Ref) => { return ( { const allOptions = (options as { label: string; options?: { label: string }[] }[]).reduce((total, current) => { if (current.options) { return [...total, ...current.options]; } else { return [...total, current]; } }, [] as { label: string }[]); const findItem = allOptions.find((item: { label: string }) => item.label === searchValue); if (findItem) { onChange(searchValue); } }} {...others} options={options} singleSelection={{ asPlainText: true }} ref={ref} onChange={(selectedOptions) => { if (selectedOptions && selectedOptions[0]) { onChange(selectedOptions[0].label, selectedOptions[0]); } else { onChange(undefined); } }} selectedOptions={[value].filter((item) => item !== undefined).map((label) => ({ label: `${label}` }))} /> ); }) ) as React.ComponentType, ComboBoxMultiple: EuiToolTipWrapper( forwardRef( ( { onChange, value, ...others }: { value?: string[]; options: EuiComboBoxOptionOption[]; onChange: (val: string[], values: EuiComboBoxOptionOption[], ...args: any) => void; }, ref: React.Ref ) => { return ( { const allOptions = others.options.reduce((total, current) => { if (current.options) { return [...total, ...current.options]; } else { return [...total, current]; } }, [] as EuiComboBoxOptionOption[]); const findItem = allOptions.find((item: { label: string }) => item.label === searchValue); if (findItem) { onChange( [...(value || []), searchValue], [ ...allOptions.filter((item) => value?.includes(item.label)), { label: searchValue, }, ] ); } }} {...others} ref={ref} onChange={(selectedOptions) => { onChange(selectedOptions.map((item) => item.value) as string[], selectedOptions); }} selectedOptions={ (value || []) .map((item: string) => others.options.find((option) => option.value === item) || { label: item, value: item }) .filter((item) => item !== undefined) as EuiComboBoxOptionOption[] } /> ); } ) ) as React.ComponentType, }; export default componentMap;