Skip to content

Commit

Permalink
[Vis: Default editor] EUIficate top_aggregate and size param editors (e…
Browse files Browse the repository at this point in the history
…lastic#36567)

* EUIficate top_aggregate_and_size param editor

* Remove template

* Change typescript interfaces

* Fix browser tests

* Add an icon alert

* Changes due to comments

* Move error to a help text

* Move error to a field

* Change validation logic

* Fix discarding changes action

* Remove changed translation
  • Loading branch information
sulemanof authored and jkakavas committed May 30, 2019
1 parent b983eb1 commit 59d3e0d
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Top hit metric', function () {
value: sortOrder
};
params.aggregate = {
val: aggregate
value: aggregate
};
params.size = size;
const vis = new Vis(indexPattern, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import React from 'react';

const wrapWithInlineComp = Component => props => (
<div className={`visEditorAggParam--half visEditorAggParam--half-${props.aggParam.name}`}>
<Component {...props} />
<Component {...props} wrappedWithInlineComp={true}/>
</div>);

export { wrapWithInlineComp };
8 changes: 7 additions & 1 deletion src/legacy/ui/public/agg_types/controls/field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ import { FieldParamType } from '../param_types';
const label = i18n.translate('common.ui.aggTypes.field.fieldLabel', { defaultMessage: 'Field' });

interface FieldParamEditorProps extends AggParamEditorProps<FieldParamType> {
customError?: string;
customLabel?: string;
}

function FieldParamEditor({
agg,
aggParam,
customError,
customLabel,
indexedFields = [],
showValidation,
Expand All @@ -61,6 +63,10 @@ function FieldParamEditor({
};
const errors = [];

if (customError) {
errors.push(customError);
}

if (!indexedFields.length) {
errors.push(
i18n.translate('common.ui.aggTypes.field.noCompatibleFieldsDescription', {
Expand All @@ -75,7 +81,7 @@ function FieldParamEditor({
setTouched();
}

const isValid = !!value && !!indexedFields.length;
const isValid = !!value && !errors.length;

useEffect(
() => {
Expand Down
3 changes: 2 additions & 1 deletion src/legacy/ui/public/agg_types/controls/order.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function OrderParamEditor({
setValue,
setValidity,
setTouched,
wrappedWithInlineComp,
}: AggParamEditorProps<SelectValueProp> & SelectParamEditorProps) {
const label = i18n.translate('common.ui.aggTypes.orderLabel', {
defaultMessage: 'Order',
Expand All @@ -48,7 +49,7 @@ function OrderParamEditor({
label={label}
fullWidth={true}
isInvalid={showValidation ? !isValid : false}
className="visEditorSidebar__aggParamFormRow"
className={wrappedWithInlineComp ? undefined : 'visEditorSidebar__aggParamFormRow'}
>
<EuiSelect
options={aggParam.options.raw}
Expand Down
26 changes: 19 additions & 7 deletions src/legacy/ui/public/agg_types/controls/size.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,30 @@ import React, { useEffect } from 'react';
import { isUndefined } from 'lodash';
import { AggParamEditorProps } from 'ui/vis/editors/default';
import { EuiFormRow, EuiFieldNumber } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';

interface SizeParamEditorProps extends AggParamEditorProps<number | ''> {
iconTip?: React.ReactNode;
disabled?: boolean;
}

function SizeParamEditor({
disabled,
iconTip,
value,
setValue,
showValidation,
setValidity,
setTouched,
}: AggParamEditorProps<number | ''>) {
const label = i18n.translate('common.ui.aggTypes.sizeLabel', {
defaultMessage: 'Size',
});
const isValid = Number(value) > 0;
wrappedWithInlineComp,
}: SizeParamEditorProps) {
const label = (
<>
<FormattedMessage id="common.ui.aggTypes.sizeLabel" defaultMessage="Size" />
{iconTip}
</>
);
const isValid = disabled || Number(value) > 0;

useEffect(
() => {
Expand All @@ -47,7 +58,7 @@ function SizeParamEditor({
label={label}
fullWidth={true}
isInvalid={showValidation ? !isValid : false}
className="visEditorSidebar__aggParamFormRow"
className={wrappedWithInlineComp ? undefined : 'visEditorSidebar__aggParamFormRow'}
>
<EuiFieldNumber
value={isUndefined(value) ? '' : value}
Expand All @@ -56,6 +67,7 @@ function SizeParamEditor({
isInvalid={showValidation ? !isValid : false}
onBlur={setTouched}
min={1}
disabled={disabled}
data-test-subj="sizeParamEditor"
/>
</EuiFormRow>
Expand Down
138 changes: 138 additions & 0 deletions src/legacy/ui/public/agg_types/controls/top_aggregate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React, { useEffect, useRef } from 'react';
import { EuiFormRow, EuiIconTip, EuiSelect } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { AggParamEditorProps } from 'ui/vis/editors/default';
import { AggConfig } from 'ui/vis';
import { AggParam } from '../agg_param';
import { SelectValueProp, SelectParamEditorProps } from '../param_types/select';

interface AggregateValueProp extends SelectValueProp {
isCompatibleType(filedType: string): boolean;
isCompatibleVis(visName: string): boolean;
}

function getCompatibleAggs(agg: AggConfig, visName: string): AggregateValueProp[] {
const fieldType = agg.params.field && agg.params.field.type;
const { options = [] } = agg.getAggParams().find(({ name }: AggParam) => name === 'aggregate');
return options.filter(
(option: AggregateValueProp) =>
fieldType && option.isCompatibleType(fieldType) && option.isCompatibleVis(visName)
);
}

function TopAggregateParamEditor({
agg,
aggParam,
value,
visName,
showValidation,
setValue,
setValidity,
setTouched,
wrappedWithInlineComp,
}: AggParamEditorProps<AggregateValueProp> & SelectParamEditorProps<AggregateValueProp>) {
const isFirstRun = useRef(true);
const fieldType = agg.params.field && agg.params.field.type;
const emptyValue = { text: '', value: 'EMPTY_VALUE', disabled: true, hidden: true };
const filteredOptions = getCompatibleAggs(agg, visName)
.map(({ text, value: val }) => ({ text, value: val }))
.sort((a, b) => a.text.toLowerCase().localeCompare(b.text.toLowerCase()));
const options = [emptyValue, ...filteredOptions];
const disabled = fieldType && !filteredOptions.length;
const isValid = disabled || !!value;

const label = (
<>
<FormattedMessage
id="common.ui.aggTypes.aggregateWithLabel"
defaultMessage="Aggregate with"
/>{' '}
<EuiIconTip
position="right"
type="questionInCircle"
content={i18n.translate('common.ui.aggTypes.aggregateWithTooltip', {
defaultMessage:
'Choose a strategy for combining multiple hits or a multi-valued field into a single metric.',
})}
/>
</>
);

useEffect(
() => {
setValidity(isValid);
},
[isValid]
);

useEffect(
() => {
if (isFirstRun.current) {
isFirstRun.current = false;
return;
}

if (value) {
if (aggParam.options.byValue[value.value]) {
return;
}

setValue();
}

if (filteredOptions.length === 1) {
setValue(aggParam.options.byValue[filteredOptions[0].value]);
}
},
[fieldType, visName]
);

const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
if (event.target.value === emptyValue.value) {
setValue();
} else {
setValue(aggParam.options.byValue[event.target.value]);
}
};

return (
<EuiFormRow
label={label}
fullWidth={true}
isInvalid={showValidation ? !isValid : false}
className={wrappedWithInlineComp ? undefined : 'visEditorSidebar__aggParamFormRow'}
>
<EuiSelect
options={options}
value={value ? value.value : emptyValue.value}
onChange={handleChange}
fullWidth={true}
isInvalid={showValidation ? !isValid : false}
disabled={disabled}
onBlur={setTouched}
/>
</EuiFormRow>
);
}

export { TopAggregateParamEditor, getCompatibleAggs };

This file was deleted.

40 changes: 40 additions & 0 deletions src/legacy/ui/public/agg_types/controls/top_field.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React from 'react';
import { i18n } from '@kbn/i18n';
import { AggParamEditorProps } from '../../vis/editors/default';
import { FieldParamType } from '../param_types';
import { FieldParamEditor } from './field';
import { getCompatibleAggs } from './top_aggregate';

function TopFieldParamEditor(props: AggParamEditorProps<FieldParamType>) {
const compatibleAggs = getCompatibleAggs(props.agg, props.visName);
let customError;

if (!compatibleAggs.length) {
customError = i18n.translate('common.ui.aggTypes.aggregateWith.noAggsErrorTooltip', {
defaultMessage: 'The chosen field has no compatible aggregations.',
});
}

return <FieldParamEditor {...props} customError={customError} />;
}

export { TopFieldParamEditor };
47 changes: 47 additions & 0 deletions src/legacy/ui/public/agg_types/controls/top_size.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React from 'react';
import { AggParamEditorProps } from 'ui/vis/editors/default';
import { EuiIconTip } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { SizeParamEditor } from './size';
import { getCompatibleAggs } from './top_aggregate';

function TopSizeParamEditor(props: AggParamEditorProps<number | ''>) {
const iconTip = (
<>
{' '}
<EuiIconTip
position="right"
content={i18n.translate('common.ui.aggTypes.sizeTooltip', {
defaultMessage:
"Request top-K hits. Multiple hits will be combined via 'aggregate with'.",
})}
type="questionInCircle"
/>
</>
);
const fieldType = props.agg.params.field && props.agg.params.field.type;
const disabled = fieldType && !getCompatibleAggs(props.agg, props.visName).length;

return <SizeParamEditor {...props} iconTip={iconTip} disabled={disabled} />;
}

export { TopSizeParamEditor };
Loading

0 comments on commit 59d3e0d

Please sign in to comment.