-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Vis: Default editor] EUIficate top_aggregate and size param editors (#…
…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
1 parent
b0c0165
commit 3665ba0
Showing
16 changed files
with
290 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/legacy/ui/public/agg_types/controls/top_aggregate.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
43 changes: 0 additions & 43 deletions
43
src/legacy/ui/public/agg_types/controls/top_aggregate_and_size.html
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.