Skip to content

Commit 87a9b6b

Browse files
[Maps] Move legend rendering to style property (#53173)
1 parent 3ee0683 commit 87a9b6b

File tree

10 files changed

+156
-106
lines changed

10 files changed

+156
-106
lines changed

x-pack/legacy/plugins/maps/public/layers/styles/components/style_legend_row.js renamed to x-pack/legacy/plugins/maps/public/layers/styles/components/ranged_style_legend_row.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import PropTypes from 'prop-types';
99

1010
import { EuiFlexGroup, EuiFlexItem, EuiText, EuiSpacer, EuiToolTip } from '@elastic/eui';
1111

12-
export function StyleLegendRow({ header, minLabel, maxLabel, propertyLabel, fieldLabel }) {
12+
export function RangedStyleLegendRow({ header, minLabel, maxLabel, propertyLabel, fieldLabel }) {
1313
return (
1414
<div>
1515
<EuiSpacer size="xs" />
@@ -39,7 +39,7 @@ export function StyleLegendRow({ header, minLabel, maxLabel, propertyLabel, fiel
3939
);
4040
}
4141

42-
StyleLegendRow.propTypes = {
42+
RangedStyleLegendRow.propTypes = {
4343
header: PropTypes.node.isRequired,
4444
minLabel: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
4545
maxLabel: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,

x-pack/legacy/plugins/maps/public/layers/styles/heatmap/components/legend/heatmap_legend.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React from 'react';
88

99
import { i18n } from '@kbn/i18n';
1010
import { ColorGradient } from '../../../components/color_gradient';
11-
import { StyleLegendRow } from '../../../components/style_legend_row';
11+
import { RangedStyleLegendRow } from '../../../components/ranged_style_legend_row';
1212
import {
1313
DEFAULT_RGB_HEATMAP_COLOR_RAMP,
1414
DEFAULT_HEATMAP_COLOR_RAMP_NAME,
@@ -50,7 +50,7 @@ export class HeatmapLegend extends React.Component {
5050
);
5151

5252
return (
53-
<StyleLegendRow
53+
<RangedStyleLegendRow
5454
header={header}
5555
minLabel={i18n.translate('xpack.maps.heatmapLegend.coldLabel', {
5656
defaultMessage: 'cold',

x-pack/legacy/plugins/maps/public/layers/styles/vector/components/legend/style_property_legend_row.js

Lines changed: 0 additions & 57 deletions
This file was deleted.

x-pack/legacy/plugins/maps/public/layers/styles/vector/components/legend/vector_style_legend.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55
*/
66

77
import _ from 'lodash';
8-
import React, { Component } from 'react';
9-
import PropTypes from 'prop-types';
10-
11-
import { StylePropertyLegendRow } from './style_property_legend_row';
8+
import React, { Component, Fragment } from 'react';
129

1310
export class VectorStyleLegend extends Component {
1411
state = {
15-
rows: [],
12+
styles: [],
1613
};
1714

1815
componentDidMount() {
1916
this._isMounted = true;
20-
this._prevRowDescriptors = undefined;
17+
this._prevStyleDescriptors = undefined;
2118
this._loadRows();
2219
}
2320

@@ -30,27 +27,26 @@ export class VectorStyleLegend extends Component {
3027
}
3128

3229
_loadRows = _.debounce(async () => {
33-
const rows = await this.props.loadRows();
34-
const rowDescriptors = rows.map(row => {
30+
const styles = await this.props.getLegendDetailStyleProperties();
31+
const styleDescriptorPromises = styles.map(async style => {
3532
return {
36-
label: row.label,
37-
range: row.meta,
38-
styleOptions: row.style.getOptions(),
33+
type: style.getStyleName(),
34+
options: style.getOptions(),
35+
fieldMeta: style.getFieldMeta(),
36+
label: await style.getField().getLabel(),
3937
};
4038
});
41-
if (this._isMounted && !_.isEqual(rowDescriptors, this._prevRowDescriptors)) {
42-
this._prevRowDescriptors = rowDescriptors;
43-
this.setState({ rows });
39+
40+
const styleDescriptors = await Promise.all(styleDescriptorPromises);
41+
if (this._isMounted && !_.isEqual(styleDescriptors, this._prevStyleDescriptors)) {
42+
this._prevStyleDescriptors = styleDescriptors;
43+
this.setState({ styles: styles });
4444
}
4545
}, 100);
4646

4747
render() {
48-
return this.state.rows.map(rowProps => {
49-
return <StylePropertyLegendRow key={rowProps.style.getStyleName()} {...rowProps} />;
48+
return this.state.styles.map(style => {
49+
return <Fragment key={style.getStyleName()}>{style.renderLegendDetailRow()}</Fragment>;
5050
});
5151
}
5252
}
53-
54-
VectorStyleLegend.propTypes = {
55-
loadRows: PropTypes.func.isRequired,
56-
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import React from 'react';
8+
import _ from 'lodash';
9+
import { RangedStyleLegendRow } from '../../../components/ranged_style_legend_row';
10+
import { getVectorStyleLabel } from '../../components/get_vector_style_label';
11+
12+
const EMPTY_VALUE = '';
13+
14+
export class DynamicLegendRow extends React.Component {
15+
constructor() {
16+
super();
17+
this._isMounted = false;
18+
this.state = {
19+
label: EMPTY_VALUE,
20+
};
21+
}
22+
23+
async _loadParams() {
24+
const label = await this.props.style.getField().getLabel();
25+
const newState = { label };
26+
if (this._isMounted && !_.isEqual(this.state, newState)) {
27+
this.setState(newState);
28+
}
29+
}
30+
31+
componentDidUpdate() {
32+
this._loadParams();
33+
}
34+
35+
componentWillUnmount() {
36+
this._isMounted = false;
37+
}
38+
39+
componentDidMount() {
40+
this._isMounted = true;
41+
this._loadParams();
42+
}
43+
44+
_formatValue(value) {
45+
if (value === EMPTY_VALUE) {
46+
return value;
47+
}
48+
return this.props.style.formatField(value);
49+
}
50+
51+
render() {
52+
const fieldMeta = this.props.style.getFieldMeta();
53+
54+
let minLabel = EMPTY_VALUE;
55+
let maxLabel = EMPTY_VALUE;
56+
if (fieldMeta) {
57+
const range = { min: fieldMeta.min, max: fieldMeta.max };
58+
const min = this._formatValue(_.get(range, 'min', EMPTY_VALUE));
59+
minLabel =
60+
this.props.style.isFieldMetaEnabled() && range && range.isMinOutsideStdRange
61+
? `< ${min}`
62+
: min;
63+
64+
const max = this._formatValue(_.get(range, 'max', EMPTY_VALUE));
65+
maxLabel =
66+
this.props.style.isFieldMetaEnabled() && range && range.isMaxOutsideStdRange
67+
? `> ${max}`
68+
: max;
69+
}
70+
71+
return (
72+
<RangedStyleLegendRow
73+
header={this.props.style.renderLegendHeader()}
74+
minLabel={minLabel}
75+
maxLabel={maxLabel}
76+
propertyLabel={getVectorStyleLabel(this.props.style.getStyleName())}
77+
fieldLabel={this.state.label}
78+
/>
79+
);
80+
}
81+
}

x-pack/legacy/plugins/maps/public/layers/styles/vector/properties/dynamic_color_property.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class DynamicColorProperty extends DynamicStyleProperty {
111111
return getColorRampStops(this._options.color);
112112
}
113113

114-
renderHeader() {
114+
renderLegendHeader() {
115115
if (this._options.color) {
116116
return <ColorGradient colorRampName={this._options.color} />;
117117
} else {

x-pack/legacy/plugins/maps/public/layers/styles/vector/properties/dynamic_size_property.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class DynamicSizeProperty extends DynamicStyleProperty {
121121
);
122122
}
123123

124-
renderHeader() {
124+
renderLegendHeader() {
125125
let icons;
126126
if (this.getStyleName() === VECTOR_STYLES.LINE_WIDTH) {
127127
icons = getLineWidthIcons();

x-pack/legacy/plugins/maps/public/layers/styles/vector/properties/dynamic_style_property.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ import _ from 'lodash';
88
import { AbstractStyleProperty } from './style_property';
99
import { DEFAULT_SIGMA } from '../vector_style_defaults';
1010
import { STYLE_TYPE } from '../../../../../common/constants';
11+
import { DynamicLegendRow } from './components/dynamic_legend_row';
12+
import React from 'react';
1113

1214
export class DynamicStyleProperty extends AbstractStyleProperty {
1315
static type = STYLE_TYPE.DYNAMIC;
1416

15-
constructor(options, styleName, field) {
17+
constructor(options, styleName, field, getFieldMeta, getFieldFormatter) {
1618
super(options, styleName);
1719
this._field = field;
20+
this._getFieldMeta = getFieldMeta;
21+
this._getFieldFormatter = getFieldFormatter;
22+
}
23+
24+
getFieldMeta() {
25+
return this._getFieldMeta && this._field ? this._getFieldMeta(this._field.getName()) : null;
1826
}
1927

2028
getField() {
@@ -105,4 +113,18 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
105113
isMaxOutsideStdRange: stats.max > stdUpperBounds,
106114
};
107115
}
116+
117+
formatField(value) {
118+
if (this.getField()) {
119+
const fieldName = this.getField().getName();
120+
const fieldFormatter = this._getFieldFormatter(fieldName);
121+
return fieldFormatter ? fieldFormatter(value) : value;
122+
} else {
123+
return value;
124+
}
125+
}
126+
127+
renderLegendDetailRow() {
128+
return <DynamicLegendRow style={this} />;
129+
}
108130
}

x-pack/legacy/plugins/maps/public/layers/styles/vector/properties/style_property.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export class AbstractStyleProperty {
2424
return true;
2525
}
2626

27+
formatField(value) {
28+
return value;
29+
}
30+
2731
getStyleName() {
2832
return this._styleName;
2933
}
@@ -32,7 +36,11 @@ export class AbstractStyleProperty {
3236
return this._options || {};
3337
}
3438

35-
renderHeader() {
39+
renderLegendHeader() {
40+
return null;
41+
}
42+
43+
renderLegendDetailRow() {
3644
return null;
3745
}
3846
}

0 commit comments

Comments
 (0)