From 254003919cbb7b91e8c2e38925580df83f5706ba Mon Sep 17 00:00:00 2001 From: moklick Date: Mon, 1 Mar 2021 15:42:17 +0100 Subject: [PATCH] refactor(edge-text): allow html attributes as props --- src/components/Edges/EdgeText.tsx | 96 +++++++++++++++---------------- src/types/index.ts | 4 +- 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/components/Edges/EdgeText.tsx b/src/components/Edges/EdgeText.tsx index 55cc369b..28c1888b 100644 --- a/src/components/Edges/EdgeText.tsx +++ b/src/components/Edges/EdgeText.tsx @@ -1,56 +1,56 @@ -import React, { memo, useRef, useState, useEffect } from 'react'; +import React, { memo, useRef, useState, useEffect, FC } from 'react'; import { EdgeTextProps, Rect } from '../../types'; -export default memo( - ({ - x, - y, - label, - labelStyle = {}, - labelShowBg = true, - labelBgStyle = {}, - labelBgPadding = [2, 4], - labelBgBorderRadius = 2, - }: EdgeTextProps) => { - const edgeRef = useRef(null); - const [edgeTextBbox, setEdgeTextBbox] = useState({ x: 0, y: 0, width: 0, height: 0 }); +const EdgeText: FC = ({ + x, + y, + label, + labelStyle = {}, + labelShowBg = true, + labelBgStyle = {}, + labelBgPadding = [2, 4], + labelBgBorderRadius = 2, + ...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(); + 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; + setEdgeTextBbox({ + x: textBbox.x, + y: textBbox.y, + width: textBbox.width, + height: textBbox.height, + }); } + }, [label]); - return ( - - {labelShowBg && ( - - )} - - {label} - - - ); + if (typeof label === 'undefined' || !label) { + return null; } -); + + return ( + + {labelShowBg && ( + + )} + + {label} + + + ); +}; +export default memo(EdgeText); diff --git a/src/types/index.ts b/src/types/index.ts index a3a94c86..7b10a466 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,4 @@ -import { CSSProperties, MouseEvent as ReactMouseEvent } from 'react'; +import { CSSProperties, MouseEvent as ReactMouseEvent, HTMLAttributes } from 'react'; import { Selection as D3Selection, ZoomBehavior } from 'd3'; export type ElementId = string; @@ -157,7 +157,7 @@ export interface EdgeSmoothStepProps extends EdgeProps { borderRadius?: number; } -export interface EdgeTextProps { +export interface EdgeTextProps extends HTMLAttributes { x: number; y: number; label?: string;