-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.tsx
67 lines (57 loc) · 1.37 KB
/
index.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
import React, { useEffect, useRef, forwardRef } from 'react';
export type ReactG2PlotProps<O> = {
readonly className?: string;
readonly Ctor: any;
readonly options: O;
};
/**
* 一个业务可用的 G2Plot React 组件
*/
export default forwardRef(function<O = any>(props: ReactG2PlotProps<O>, ref: any) {
const { className, Ctor, options } = props;
const $dom = useRef(undefined);
const plotRef = useRef(undefined);
/**
* 同步 forward ref
* @param source
* @param target
*/
function syncRef(source, target) {
if (typeof target === 'function') {
target(source.current);
} else if (target) {
target.current = source.current;
}
}
/**
* 如果存在则更新,否则就创建
*/
function renderPlot() {
if (plotRef.current) {
// update
plotRef.current.update(options);
} else {
// new
plotRef.current = new Ctor($dom.current, options);
plotRef.current.render();
}
syncRef(plotRef, ref);
}
/**
* 销毁
*/
function destoryPlot() {
if (plotRef.current) {
plotRef.current.destroy();
plotRef.current = undefined;
}
}
useEffect(() => {
renderPlot();
return () => {
destoryPlot();
};
}, [options, Ctor]);
// 默认添加 g2plot-for-react 的 class
return <div className={`g2plot-for-react ${className || ''}`} ref={$dom} />;
});