fix(edges): add suport for className in baseEdge and remove unecessary render

This commit is contained in:
=
2023-12-13 10:42:04 -03:00
parent 58ad111ec3
commit fe3fe65c10
3 changed files with 26 additions and 18 deletions
@@ -2,6 +2,7 @@ import { isNumeric } from '@xyflow/system';
import type { BaseEdgeProps } from '../../types'; import type { BaseEdgeProps } from '../../types';
import EdgeText from './EdgeText'; import EdgeText from './EdgeText';
import classcat from 'classcat';
const BaseEdge = ({ const BaseEdge = ({
id, id,
@@ -17,6 +18,7 @@ const BaseEdge = ({
style, style,
markerEnd, markerEnd,
markerStart, markerStart,
className,
interactionWidth = 20, interactionWidth = 20,
}: BaseEdgeProps) => { }: BaseEdgeProps) => {
return ( return (
@@ -26,7 +28,7 @@ const BaseEdge = ({
style={style} style={style}
d={path} d={path}
fill="none" fill="none"
className="react-flow__edge-path" className={classcat(['react-flow__edge-path', className])}
markerEnd={markerEnd} markerEnd={markerEnd}
markerStart={markerStart} markerStart={markerStart}
/> />
@@ -1,4 +1,4 @@
import { memo, useRef, useState, useEffect, type FC, type PropsWithChildren } from 'react'; import { memo, useState, type FC, type PropsWithChildren, useCallback } from 'react';
import cc from 'classcat'; import cc from 'classcat';
import type { Rect } from '@xyflow/system'; import type { Rect } from '@xyflow/system';
@@ -17,22 +17,21 @@ const EdgeText: FC<PropsWithChildren<EdgeTextProps>> = ({
className, className,
...rest ...rest
}) => { }) => {
const edgeRef = useRef<SVGTextElement>(null); const [edgeTextBbox, setEdgeTextBbox] = useState<Rect>({ x: 1, y: 0, width: 0, height: 0 });
const [edgeTextBbox, setEdgeTextBbox] = useState<Rect>({ x: 0, y: 0, width: 0, height: 0 });
const edgeTextClasses = cc(['react-flow__edge-textwrapper', className]); const edgeTextClasses = cc(['react-flow__edge-textwrapper', className]);
useEffect(() => { const onEdgeTextRefChange = useCallback((edgeRef: SVGTextElement) => {
if (edgeRef.current) { if (edgeRef === null) return;
const textBbox = edgeRef.current.getBBox();
setEdgeTextBbox({ const textBbox = edgeRef.getBBox();
x: textBbox.x,
y: textBbox.y, setEdgeTextBbox({
width: textBbox.width, x: textBbox.x,
height: textBbox.height, y: textBbox.y,
}); width: textBbox.width,
} height: textBbox.height,
}, [label]); });
}, []);
if (typeof label === 'undefined' || !label) { if (typeof label === 'undefined' || !label) {
return null; return null;
@@ -57,7 +56,13 @@ const EdgeText: FC<PropsWithChildren<EdgeTextProps>> = ({
ry={labelBgBorderRadius} ry={labelBgBorderRadius}
/> />
)} )}
<text className="react-flow__edge-text" y={edgeTextBbox.height / 2} dy="0.3em" ref={edgeRef} style={labelStyle}> <text
className="react-flow__edge-text"
y={edgeTextBbox.height / 2}
dy="0.3em"
ref={onEdgeTextRefChange}
style={labelStyle}
>
{label} {label}
</text> </text>
{children} {children}
+3 -2
View File
@@ -99,8 +99,9 @@ export type EdgeProps<T = any> = Pick<
pathOptions?: any; pathOptions?: any;
}; };
export type BaseEdgeProps = Pick<EdgeProps, 'style' | 'markerStart' | 'markerEnd' | 'interactionWidth'> & export type BaseEdgeProps = Pick<EdgeProps, 'style' | 'markerStart' | 'markerEnd' | 'interactionWidth'> & {
EdgeLabelOptions & { className?: CSSProperties;
} & EdgeLabelOptions & {
id?: string; id?: string;
labelX?: number; labelX?: number;
labelY?: number; labelY?: number;