-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraph.tsx
50 lines (44 loc) · 1.33 KB
/
Graph.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
import * as X6 from '@antv/x6';
import React, { useRef, createContext, useContext, forwardRef, useEffect, useState } from 'react';
import type { ReactNode } from 'react'
const GraphContext = createContext<X6.Graph | null>(null);
interface Props {
className?: string;
container?: HTMLDivElement;
children?: ReactNode;
};
export const Graph = forwardRef<X6.Graph, X6.Graph.Options & Props>((props, ref) => {
const [graph, setGraph] = useState<X6.Graph | null>(null);
const { container, children, className = 'react-x6-graph', ...other } = props;
const containerRef = useRef<HTMLDivElement>(container || null);
useEffect(() => {
if (containerRef.current && !graph) {
const graph = new X6.Graph({
container: containerRef.current,
...other,
});
setGraph(graph);
if (typeof ref === 'function') {
ref(graph);
} else if (ref) {
ref.current = graph;
}
}
}, [graph, other, ref]);
return (
<div
className={className}
style={{
width: '100%',
height: '100%',
position: 'relative',
}}
>
<GraphContext.Provider value={graph}>
{!container && <div ref={containerRef} />}
{!!graph && children}
</GraphContext.Provider>
</div>
);
});
export const useGraphInstance = () => useContext(GraphContext);