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
@@ -2,7 +2,6 @@ import ReactFlow, { EdgeProps } from 'reactflow';
import ControlledFlow from '../../support/ControlledFlow';
import * as simpleflow from '../../fixtures/simpleflow';
import { RefReactFlow } from '../../support/RefReactFlow';
describe('<ReactFlow />: Basic Props', () => {
describe('uses defaultNodes and defaultEdges', () => {
@@ -195,32 +194,4 @@ describe('<ReactFlow />: Basic Props', () => {
cy.mount(<ControlledFlow className="custom" />);
cy.get('.react-flow').should('have.class', 'custom');
});
it('uses function ref', () => {
let pResolve: ((element: HTMLDivElement | PromiseLike<HTMLDivElement> | null) => void) | null = null;
const promise: Promise<HTMLDivElement | null> = new Promise((resolve) => {
pResolve = resolve;
});
function ref(root: HTMLDivElement | null) {
pResolve && pResolve(root);
}
cy.mount(<ReactFlow ref={ref} />);
cy.wrap(promise).should('be.instanceOf', HTMLDivElement);
});
it('use mutable ref', () => {
let pResolve: ((element: HTMLDivElement | PromiseLike<HTMLDivElement> | null) => void) | null = null;
const promise: Promise<HTMLDivElement | null> = new Promise((resolve) => {
pResolve = resolve;
});
function onGetRef(root: HTMLDivElement | null) {
pResolve && pResolve(root);
}
cy.mount(<RefReactFlow onGetRef={onGetRef} />);
cy.wrap(promise).should('be.instanceOf', HTMLDivElement);
});
});
@@ -1,12 +0,0 @@
import { useEffect, useRef } from 'react';
import ReactFlow from 'reactflow';
export function RefReactFlow({ onGetRef }: { onGetRef: (element: HTMLDivElement | null) => void }) {
const ref = useRef<HTMLDivElement | null>(null);
useEffect(() => {
onGetRef(ref.current);
}, []);
return <ReactFlow ref={ref} />;
}