@@ -14,12 +14,41 @@ import { MetricSelect, METRIC_AGGREGATION_VALUES } from './metric_select';
1414import { SingleFieldSelect } from './single_field_select' ;
1515import { METRIC_TYPE } from '../../common/constants' ;
1616
17+ function filterFieldsForAgg ( fields , aggType ) {
18+ if ( ! fields ) {
19+ return [ ] ;
20+ }
21+
22+ if ( aggType === METRIC_TYPE . UNIQUE_COUNT ) {
23+ return fields . filter ( field => {
24+ return field . aggregatable ;
25+ } ) ;
26+ }
27+
28+ return fields . filter ( field => {
29+ return field . aggregatable && field . type === 'number' ;
30+ } ) ;
31+ }
32+
1733export function MetricEditor ( { fields, metricsFilter, metric, onChange, removeButton } ) {
1834 const onAggChange = metricAggregationType => {
19- onChange ( {
35+ const newMetricProps = {
2036 ...metric ,
2137 type : metricAggregationType ,
22- } ) ;
38+ } ;
39+
40+ // unset field when new agg type does not support currently selected field.
41+ if ( metric . field && metricAggregationType !== METRIC_TYPE . COUNT ) {
42+ const fieldsForNewAggType = filterFieldsForAgg ( fields , metricAggregationType ) ;
43+ const found = fieldsForNewAggType . find ( field => {
44+ return field . name === metric . field ;
45+ } ) ;
46+ if ( ! found ) {
47+ newMetricProps . field = undefined ;
48+ }
49+ }
50+
51+ onChange ( newMetricProps ) ;
2352 } ;
2453 const onFieldChange = fieldName => {
2554 onChange ( {
@@ -36,12 +65,6 @@ export function MetricEditor({ fields, metricsFilter, metric, onChange, removeBu
3665
3766 let fieldSelect ;
3867 if ( metric . type && metric . type !== METRIC_TYPE . COUNT ) {
39- const filterField =
40- metric . type !== METRIC_TYPE . UNIQUE_COUNT
41- ? field => {
42- return field . type === 'number' ;
43- }
44- : undefined ;
4568 fieldSelect = (
4669 < EuiFormRow
4770 label = { i18n . translate ( 'xpack.maps.metricsEditor.selectFieldLabel' , {
@@ -55,8 +78,7 @@ export function MetricEditor({ fields, metricsFilter, metric, onChange, removeBu
5578 } ) }
5679 value = { metric . field }
5780 onChange = { onFieldChange }
58- filterField = { filterField }
59- fields = { fields }
81+ fields = { filterFieldsForAgg ( fields , metric . type ) }
6082 isClearable = { false }
6183 compressed
6284 />
0 commit comments