Skip to content

Commit f51272a

Browse files
committed
Refactoring
1 parent 95d1759 commit f51272a

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

src/plugins/chart_expressions/expression_metric/public/components/auto_scale.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('AutoScale', () => {
4444
describe('withAutoScale', () => {
4545
it('renders', () => {
4646
const Component = () => <h1>Hoi!</h1>;
47-
const WrappedComponent = withAutoScale()(Component);
47+
const WrappedComponent = withAutoScale(Component);
4848
expect(mount(<WrappedComponent />)).toMatchInlineSnapshot(`
4949
<Component>
5050
<div

src/plugins/chart_expressions/expression_metric/public/components/auto_scale.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
import React, { useRef, useEffect, useState, ComponentType } from 'react';
9+
import React, { useRef, useEffect, useState, ComponentType, useMemo } from 'react';
1010
import { throttle } from 'lodash';
1111
import { useResizeObserver } from '@elastic/eui';
1212
import { autoScaleWrapperStyle } from './auto_scale.styles';
@@ -41,29 +41,36 @@ export function computeScale(
4141
return Math.max(Math.min(MAX_SCALE, Math.min(scaleX, scaleY)), minScale);
4242
}
4343

44-
export const withAutoScale =
45-
(autoScaleParams?: AutoScaleParams) => (WrappedComponent: ComponentType<any>) => (props: any) => {
44+
export function withAutoScale<T>(
45+
WrappedComponent: ComponentType<T>,
46+
autoScaleParams?: AutoScaleParams
47+
) {
48+
return (props: T) => {
4649
const [scale, setScale] = useState(0);
4750
const parentRef = useRef<HTMLDivElement>(null);
4851
const childrenRef = useRef<HTMLDivElement>(null);
4952
const parentDimensions = useResizeObserver(parentRef.current);
5053

51-
const scaleFn = throttle(() => {
52-
const newScale = computeScale(
53-
{ clientHeight: parentDimensions.height, clientWidth: parentDimensions.width },
54-
childrenRef.current,
55-
autoScaleParams?.minScale
56-
);
54+
const scaleFn = useMemo(
55+
() =>
56+
throttle(() => {
57+
const newScale = computeScale(
58+
{ clientHeight: parentDimensions.height, clientWidth: parentDimensions.width },
59+
childrenRef.current,
60+
autoScaleParams?.minScale
61+
);
5762

58-
// Prevent an infinite render loop
59-
if (scale !== newScale) {
60-
setScale(newScale);
61-
}
62-
});
63+
// Prevent an infinite render loop
64+
if (scale !== newScale) {
65+
setScale(newScale);
66+
}
67+
}),
68+
[parentDimensions, setScale, scale, childrenRef]
69+
);
6370

6471
useEffect(() => {
6572
scaleFn();
66-
}, [parentDimensions]);
73+
}, [scaleFn]);
6774

6875
return (
6976
<div ref={parentRef} css={autoScaleWrapperStyle}>
@@ -78,3 +85,4 @@ export const withAutoScale =
7885
</div>
7986
);
8087
};
88+
}

src/plugins/chart_expressions/expression_metric/public/components/metric_component.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export interface MetricVisComponentProps {
2828
renderComplete: () => void;
2929
}
3030

31+
const AutoScaleMetricVisValue = withAutoScale(MetricVisValue);
32+
3133
class MetricVisComponent extends Component<MetricVisComponentProps> {
3234
private getColor(value: number, paletteParams: CustomPaletteState) {
3335
return getPaletteService().get('custom')?.getColorForValue?.(value, paletteParams, {
@@ -110,7 +112,7 @@ class MetricVisComponent extends Component<MetricVisComponentProps> {
110112

111113
private renderMetric = (metric: MetricOptions, index: number) => {
112114
const MetricComponent = this.props.visParams.metric.autoScale
113-
? withAutoScale()(MetricVisValue)
115+
? AutoScaleMetricVisValue
114116
: MetricVisValue;
115117

116118
return (

0 commit comments

Comments
 (0)