Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
[ML] Fix detector modal combo box selection (elastic#48741)
Browse files Browse the repository at this point in the history
* [ML] Fix detector modal combo box selection

* removing old work around
  • Loading branch information
jgowdyelastic authored Oct 21, 2019
1 parent 3cfbfaf commit 28c374a
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export const AdvancedDetectorModal: FC<Props> = ({
<EuiComboBox
singleSelection={{ asPlainText: true }}
options={currentFieldOptions}
selectedOptions={[fieldOption]}
selectedOptions={createSelectedOptions(fieldOption, currentFieldOptions)}
onChange={onOptionChange(setFieldOption)}
isClearable={true}
isDisabled={fieldsEnabled === false || fieldOptionEnabled === false}
Expand All @@ -233,7 +233,7 @@ export const AdvancedDetectorModal: FC<Props> = ({
<EuiComboBox
singleSelection={{ asPlainText: true }}
options={splitFieldOptions}
selectedOptions={[byFieldOption]}
selectedOptions={createSelectedOptions(byFieldOption, splitFieldOptions)}
onChange={onOptionChange(setByFieldOption)}
isClearable={true}
isDisabled={fieldsEnabled === false}
Expand All @@ -245,7 +245,7 @@ export const AdvancedDetectorModal: FC<Props> = ({
<EuiComboBox
singleSelection={{ asPlainText: true }}
options={splitFieldOptions}
selectedOptions={[overFieldOption]}
selectedOptions={createSelectedOptions(overFieldOption, splitFieldOptions)}
onChange={onOptionChange(setOverFieldOption)}
isClearable={true}
isDisabled={fieldsEnabled === false}
Expand All @@ -257,7 +257,7 @@ export const AdvancedDetectorModal: FC<Props> = ({
<EuiComboBox
singleSelection={{ asPlainText: true }}
options={splitFieldOptions}
selectedOptions={[partitionFieldOption]}
selectedOptions={createSelectedOptions(partitionFieldOption, splitFieldOptions)}
onChange={onOptionChange(setPartitionFieldOption)}
isClearable={true}
isDisabled={fieldsEnabled === false}
Expand Down Expand Up @@ -355,12 +355,9 @@ function useDetectorPlaceholder(detector: RichDetector) {

// creates list of combobox options based on an aggregation's field list
function createFieldOptionList(agg: Aggregation | null) {
const options = (agg !== null && agg.fields !== undefined ? agg.fields : [])
return (agg !== null && agg.fields !== undefined ? agg.fields : [])
.filter(f => f.id !== EVENT_RATE_FIELD_ID)
.map(createFieldOption);

// working round EuiComboBox's odd behavior when the options list contains only one item
return options.length === 1 ? [emptyOption, ...options] : options;
}

// custom hook for storing combobox options based on an aggregation field list
Expand All @@ -387,3 +384,13 @@ function createDefaultDescription(dtr: RichDetector) {
basicDetector.exclude_frequent = dtr.excludeFrequent ? dtr.excludeFrequent : undefined;
return detectorToString(basicDetector);
}

// fixes issue with EuiComboBox.
// if the options list only contains one option and nothing has been selected, set
// selectedOptions list to be an empty array
function createSelectedOptions(
option: EuiComboBoxOptionProps,
options: EuiComboBoxOptionProps[]
): EuiComboBoxOptionProps[] {
return options.length === 1 && options[0].label !== option.label ? [] : [option];
}

0 comments on commit 28c374a

Please sign in to comment.