import React, { useRef, useEffect } from 'react';
import G6, { TreeGraph } from '@antv/g6';
const data = {
id: 'root',
label: 'Root',
children: [
{
id: 'node-1',
label: 'Node 1',
children: [
{ id: 'node-1-1', label: 'Node 1-1' },
{ id: 'node-1-2', label: 'Node 1-2' },
],
},
{
id: 'node-2',
label: 'Node 2',
children: [
{ id: 'node-2-1', label: 'Node 2-1' },
{ id: 'node-2-2', label: 'Node 2-2' },
],
},
],
};
const TreeCard: React.FC = () => {
const graphContainer = useRef<HTMLDivElement>(null);
const graph = useRef<TreeGraph | null>(null);
useEffect(() => {
if (!graph.current && graphContainer.current) {
graph.current = new G6.TreeGraph({
container: graphContainer.current,
width: 800,
height: 500,
modes: {
default: ['collapse-expand'],
},
layout: {
type: 'dendrogram',
direction: 'LR',
nodeSep: 30,
rankSep: 100,
radial: false,
},
});
graph.current.data(data);
graph.current.render();
graph.current.on('node:click', (evt: G6.NodeEvent) => {
graph.current!.updateItem(evt.item, {
collapsed: !evt.item.getModel().collapsed,
});
graph.current!.refreshLayout();
});
}
}, []);
return <div ref={graphContainer} />;
};
export default TreeCard;