/* * 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, { Fragment } from 'react'; import { useSelector } from 'react-redux'; import { get } from 'lodash'; import { EuiFormRow, EuiSelect, EuiComboBox } from '@elastic/eui'; import { getAllFields } from '../../../../redux/selectors/opensearch'; import { getNumberFieldOptions, getCountableFieldOptions, } from '../../utils/helpers'; import { Field, FieldProps } from 'formik'; import { AGGREGATION_TYPES } from '../../utils/constants'; import { requiredSelectField, requiredNonEmptyFieldSelected, isInvalid, getError, } from '../../../../utils/utils'; interface AggregationSelectorProps { index?: number; } export const AggregationSelector = (props: AggregationSelectorProps) => { const numberFields = getNumberFieldOptions(useSelector(getAllFields)); const countableFields = getCountableFieldOptions(useSelector(getAllFields)); return ( {({ field, form }: FieldProps) => ( { const currentValue = field.value; const aggregationOf = get( form, `values.featureList.${props.index}.aggregationOf.0.type` ); if ( currentValue === 'value_count' && aggregationOf !== 'number' ) { form.setFieldValue( `featureList.${props.index}.aggregationOf`, undefined ); } field.onChange(e); }} data-test-subj="aggregationType" /> )} {({ field, form }: FieldProps) => ( { const normalizedOptions = createdOption.trim(); if (!normalizedOptions) return; const customOption = [{ label: normalizedOptions }]; form.setFieldValue( `featureList.${props.index}.aggregationOf`, customOption ); }} //@ts-ignore options={ get(form, `values.featureList.${props.index}.aggregationBy`) === 'value_count' ? countableFields : numberFields } {...field} onClick={() => { form.setFieldTouched( `featureList.${props.index}.aggregationOf`, true ); }} onChange={(options: any) => { form.setFieldValue( `featureList.${props.index}.aggregationOf`, options ); }} /> )} ); };