refactor: use existing d3Selection to get the element ref

This commit is contained in:
GeoffreyLiu
2022-11-14 08:30:09 +08:00
parent 1993734a99
commit 23aa8bb270
9 changed files with 15 additions and 92 deletions
@@ -6,7 +6,6 @@ import EdgeRenderer from '../EdgeRenderer';
import ViewportWrapper from '../Viewport';
import useOnInitHandler from '../../hooks/useOnInitHandler';
import type { EdgeTypesWrapped, NodeTypesWrapped, ReactFlowProps } from '../../types';
import { EDGE_LABEL_RENDERER_MAIN_CLASS } from '../../constants/component';
export type GraphViewProps = Omit<ReactFlowProps, 'onSelectionChange' | 'nodes' | 'edges' | 'nodeTypes' | 'edgeTypes'> &
Required<
@@ -159,7 +158,7 @@ const GraphView = ({
disableKeyboardA11y={disableKeyboardA11y}
rfId={rfId}
/>
<div className={EDGE_LABEL_RENDERER_MAIN_CLASS} />
<div className="react-flow__edgelabel-renderer" />
<NodeRenderer
nodeTypes={nodeTypes}
@@ -1,4 +1,4 @@
import { forwardRef, useRef } from 'react';
import { forwardRef } from 'react';
import type { CSSProperties } from 'react';
import cc from 'classcat';
@@ -164,26 +164,11 @@ const ReactFlow = forwardRef<ReactFlowRefType, ReactFlowProps>(
const edgeTypesWrapped = useNodeOrEdgeTypes(edgeTypes, createEdgeTypes) as EdgeTypesWrapped;
const rfId = id || '1';
const rootElementRef = useRef<HTMLDivElement | null>(null);
function setRootRef(element: HTMLDivElement | null) {
rootElementRef.current = element;
if (typeof ref === 'function') {
ref(element);
return;
}
if (ref) {
ref.current = element;
}
}
return (
<div
{...rest}
style={{ ...style, ...wrapperStyle }}
ref={setRootRef}
ref={ref}
className={cc(['react-flow', className])}
data-testid="rf__wrapper"
id={id}
@@ -290,7 +275,6 @@ const ReactFlow = forwardRef<ReactFlowRefType, ReactFlowProps>(
noPanClassName={noPanClassName}
nodeOrigin={nodeOrigin}
rfId={rfId}
rootElementRef={rootElementRef}
/>
<SelectionListener onSelectionChange={onSelectionChange} />
{children}