-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathprogress_component.tsx
134 lines (115 loc) · 4.21 KB
/
progress_component.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, { CSSProperties, RefCallback, useCallback, useEffect, useRef, useState } from 'react';
import { useResizeObserver } from '@elastic/eui';
import { IInterpreterRenderHandlers } from '../../../../expressions';
import { NodeDimensions, ProgressRendererConfig } from '../../../common/types';
import { ShapeRef, SvgConfig, SvgTextAttributes } from '../reusable/types';
import { getShapeContentElement } from '../reusable/shape_factory';
import { getId, withSuspense } from '../../../../presentation_util/public';
import { getTextAttributes, getViewBox } from './utils';
import { getDefaultShapeData } from '../reusable';
import { LazyProgressDrawer } from '../..';
const ProgressDrawer = withSuspense(LazyProgressDrawer);
interface ProgressComponentProps extends ProgressRendererConfig {
onLoaded: IInterpreterRenderHandlers['done'];
parentNode: HTMLElement;
}
function ProgressComponent({
onLoaded,
parentNode,
shape: shapeType,
value,
max,
valueColor,
barColor,
valueWeight,
barWeight,
label,
font,
}: ProgressComponentProps) {
const parentNodeDimensions = useResizeObserver(parentNode);
const [dimensions, setDimensions] = useState<NodeDimensions>({
width: parentNode.offsetWidth,
height: parentNode.offsetHeight,
});
const [shapeData, setShapeData] = useState<SvgConfig>(getDefaultShapeData());
const shapeRef = useCallback<RefCallback<ShapeRef>>((node) => {
if (node !== null) {
setShapeData(node.getData());
}
}, []);
const [totalLength, setTotalLength] = useState<number>(0);
useEffect(() => {
setDimensions({
width: parentNode.offsetWidth,
height: parentNode.offsetHeight,
});
onLoaded();
}, [onLoaded, parentNode, parentNodeDimensions]);
const progressRef = useRef<
SVGCircleElement & SVGPathElement & SVGPolygonElement & SVGRectElement
>(null);
const textRef = useRef<SVGTextElement>(null);
useEffect(() => {
setTotalLength(progressRef.current ? progressRef.current.getTotalLength() : 0);
}, [shapeType, shapeData, progressRef]);
const BarProgress = shapeData.shapeType ? getShapeContentElement(shapeData.shapeType) : null;
const shapeContentAttributes = {
className: 'canvasProgress__background',
fill: 'none',
stroke: barColor,
strokeWidth: `${barWeight}px`,
};
const percent = value / max;
const to = totalLength * (1 - percent);
const barProgressAttributes = {
...shapeData.shapeProps,
className: 'canvasProgress__value',
fill: 'none',
stroke: valueColor,
strokeWidth: `${valueWeight}px`,
strokeDasharray: totalLength,
strokeDashoffset: Math.max(0, to),
};
const { width: labelWidth, height: labelHeight } = textRef.current
? textRef.current.getBBox()
: { width: 0, height: 0 };
const offset = Math.max(valueWeight, barWeight);
const updatedTextAttributes = shapeData.textAttributes
? getTextAttributes(shapeType, shapeData.textAttributes, offset, label)
: {};
const textAttributes: SvgTextAttributes = {
className: 'canvasProgress__label',
style: font.spec as CSSProperties,
...updatedTextAttributes,
};
const updatedViewBox = getViewBox(shapeType, shapeData.viewBox, offset, labelWidth, labelHeight);
const shapeAttributes = {
className: 'canvasProgress',
id: getId('svg'),
...(dimensions || {}),
viewBox: updatedViewBox,
};
return (
<div className="shapeAligner">
<ProgressDrawer
shapeType={shapeType}
shapeContentAttributes={{ ...shapeContentAttributes, ref: progressRef }}
shapeAttributes={shapeAttributes}
textAttributes={{ ...textAttributes, ref: textRef }}
ref={shapeRef}
>
{BarProgress && <BarProgress {...barProgressAttributes} />}
</ProgressDrawer>
</div>
);
}
// default export required for React.Lazy
// eslint-disable-next-line import/no-default-export
export { ProgressComponent as default };