Skip to content

Commit 43c13f1

Browse files
authored
fix(line_annotation): keep the spec in state after chart rerender (#605)
The `LineAnnotation` component doesn't use the `specComponentFactory` because it render the marker if available. We have to always keep the spec updating the state on every lifecycle thus removing the deepEqual check on the shouldComponentUpdate did the trick. fix #604
1 parent 433d2cd commit 43c13f1

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License. */
18+
19+
import React from 'react';
20+
import { createStore, Store } from 'redux';
21+
import { Provider } from 'react-redux';
22+
import { mount } from 'enzyme';
23+
24+
import { chartStoreReducer, GlobalChartState } from '../../../state/chart_state';
25+
import { SpecsParser } from '../../../specs/specs_parser';
26+
import { LineSeries } from './line_series';
27+
import { LineAnnotation, AnnotationDomainTypes } from '../../../specs';
28+
29+
function LineAnnotationChart(props: { chartStore: Store<GlobalChartState> }) {
30+
return (
31+
<Provider store={props.chartStore}>
32+
<SpecsParser>
33+
<LineSeries
34+
id="line"
35+
data={[
36+
[1585126720460, 0],
37+
[1585126780460, 22],
38+
[1585126840460, 100],
39+
[1585126900460, 333],
40+
[1585126960460, 444],
41+
]}
42+
xAccessor={0}
43+
yAccessors={[1]}
44+
/>
45+
<LineAnnotation
46+
id="threshold"
47+
domainType={AnnotationDomainTypes.YDomain}
48+
dataValues={[{ dataValue: 120, details: 'threshold' }]}
49+
/>
50+
</SpecsParser>
51+
</Provider>
52+
);
53+
}
54+
55+
describe('Line annotation', () => {
56+
it('Should always be available on the on every render', () => {
57+
const storeReducer = chartStoreReducer('chart_id');
58+
const chartStore = createStore(storeReducer);
59+
const wrapper = mount(<LineAnnotationChart chartStore={chartStore} />);
60+
expect(chartStore.getState().specs['threshold']).toBeDefined();
61+
wrapper.setProps({});
62+
expect(chartStore.getState().specs['threshold']).toBeDefined();
63+
});
64+
});

src/chart_types/xy_chart/specs/line_annotation.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* under the License. */
1818

1919
import React, { createRef, CSSProperties, Component } from 'react';
20-
import { deepEqual } from '../../../utils/fast_deep_equal';
2120
import { LineAnnotationSpec, DEFAULT_GLOBAL_ID, AnnotationTypes } from '../utils/specs';
2221
import { DEFAULT_ANNOTATION_LINE_STYLE, mergeWithDefaultAnnotationLine } from '../../../utils/themes/theme';
2322
import { bindActionCreators, Dispatch } from 'redux';
@@ -62,10 +61,6 @@ export class LineAnnotationSpecComponent extends Component<LineAnnotationSpec> {
6261
upsertSpec(spec);
6362
}
6463

65-
shouldComponentUpdate(nextProps: LineAnnotationSpec) {
66-
return !deepEqual(this.props, nextProps);
67-
}
68-
6964
componentDidUpdate() {
7065
const { upsertSpec, removeSpec, children, ...config } = this.props as InjectedProps;
7166
if (this.markerRef.current) {

0 commit comments

Comments
 (0)