import React, { memo, useRef, useState, useEffect, FC } from 'react'; import { EdgeTextProps, Rect } from '../../types'; const EdgeText: FC = ({ x, y, label, labelStyle = {}, labelShowBg = true, labelBgStyle = {}, labelBgPadding = [2, 4], labelBgBorderRadius = 2, children, ...rest }) => { const edgeRef = useRef(null); const [edgeTextBbox, setEdgeTextBbox] = useState({ x: 0, y: 0, width: 0, height: 0 }); useEffect(() => { if (edgeRef.current) { const textBbox = edgeRef.current.getBBox(); setEdgeTextBbox({ x: textBbox.x, y: textBbox.y, width: textBbox.width, height: textBbox.height, }); } }, [label]); if (typeof label === 'undefined' || !label) { return null; } return ( {labelShowBg && ( )} {label} {children} ); }; export default memo(EdgeText);