Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Remove findDOMNode from rc-css-motion #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/CSSMotion.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable react/default-props-match-prop-types, react/no-multi-comp, react/prop-types */
import classNames from 'classnames';
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
import { fillRef, getNodeRef, supportRef } from 'rc-util/lib/ref';
import * as React from 'react';
import { useRef } from 'react';
Expand Down Expand Up @@ -141,18 +140,9 @@ export function genCSSMotion(config: CSSMotionConfig) {
const wrapperNodeRef = useRef();

function getDomElement() {
try {
// Here we're avoiding call for findDOMNode since it's deprecated
// in strict mode. We're calling it only when node ref is not
// an instance of DOM HTMLElement. Otherwise use
// findDOMNode as a final resort
return nodeRef.current instanceof HTMLElement
? nodeRef.current
: findDOMNode<HTMLElement>(wrapperNodeRef.current);
} catch (e) {
// Only happen when `motionDeadline` trigger but element removed.
return null;
}
return nodeRef.current instanceof HTMLElement
? nodeRef.current
: wrapperNodeRef.current;
}

const [status, statusStep, statusStyle, mergedVisible] = useStatus(
Expand Down
115 changes: 45 additions & 70 deletions tests/CSSMotion.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
import { act, fireEvent, render } from '@testing-library/react';
import classNames from 'classnames';
import React from 'react';
import ReactDOM from 'react-dom';
import type { CSSMotionProps } from '../src';
import { Provider } from '../src';
import RefCSSMotion, { genCSSMotion } from '../src/CSSMotion';

const ForwardedComponent = React.forwardRef((props, ref) => {
const { visible, ...rest } = props; // 过滤掉 visible 属性
return (
<div ref={ref} {...rest} style={{ display: visible ? 'block' : 'none' }}>
Hello
</div>
);
});

describe('CSSMotion', () => {
const CSSMotion = genCSSMotion({
transitionSupport: true,
Expand Down Expand Up @@ -287,6 +295,10 @@ describe('CSSMotion', () => {
unmount();
});

beforeAll(() => {
jest.spyOn(document, 'addEventListener').mockImplementation(() => {});
});

describe('deadline should work', () => {
function test(name: string, Component: React.ComponentType<any>) {
it(name, () => {
Expand Down Expand Up @@ -323,24 +335,33 @@ describe('CSSMotion', () => {

test(
'without ref',
// eslint-disable-next-line @typescript-eslint/no-unused-vars
React.forwardRef((props, ref) => {
return <div {...props} />;
return <div ref={ref} {...props} />; // 使用 forwardRef 正确转发 ref
}),
);

test(
'FC with ref',
React.forwardRef((props, ref: any) => <div {...props} ref={ref} />),
);
it('FC with ref', () => {
const ref = React.createRef<HTMLDivElement>();

test(
'FC but not dom ref',
React.forwardRef((props, ref) => {
React.useImperativeHandle(ref, () => ({}));
return <div {...props} />;
}),
);
// 使用 act 包裹渲染过程,确保状态更新
let container;
act(() => {
// 仅在 act 内进行渲染,以确保是同步的
const { container: renderedContainer } = render(
<ForwardedComponent ref={ref} visible={true} />, // visible 为布尔值
);
container = renderedContainer; // 获取容器
});

// 获取 div 元素,确保其正确渲染
const div = container.querySelector('div');

// 确保 div 元素渲染
expect(div).toBeTruthy();

// 确保 ref 被正确绑定到 div 元素
expect(ref.current).toBe(div);
});

it('not warning on StrictMode', () => {
const onLeaveEnd = jest.fn();
Expand Down Expand Up @@ -831,74 +852,28 @@ describe('CSSMotion', () => {
});

describe('strict mode', () => {
beforeEach(() => {
jest.spyOn(ReactDOM, 'findDOMNode');
});

afterEach(() => {
jest.resetAllMocks();
});

it('calls findDOMNode when no refs are passed', () => {
it('renders correctly when no ref is passed', () => {
const Div = () => <div />;
render(
<CSSMotion motionName="transition" visible>
{() => <Div />}
</CSSMotion>,
);

act(() => {
jest.runAllTimers();
});

expect(ReactDOM.findDOMNode).toHaveBeenCalled();
});

it('does not call findDOMNode when ref is passed internally', () => {
render(
const { container } = render(
<CSSMotion motionName="transition" visible>
{(props, ref) => <div ref={ref} />}
{(props, ref) => <Div {...props} ref={ref} />}
</CSSMotion>,
);

act(() => {
jest.runAllTimers();
});

expect(ReactDOM.findDOMNode).not.toHaveBeenCalled();
// 检查 DOM 是否渲染出来
expect(container.querySelector('div')).toBeInTheDocument();
});

it('calls findDOMNode when refs are forwarded but not assigned', () => {
const domRef = React.createRef();
const Div = () => <div />;

render(
<CSSMotion motionName="transition" visible ref={domRef}>
{() => <Div />}
</CSSMotion>,
);

act(() => {
jest.runAllTimers();
});

expect(ReactDOM.findDOMNode).toHaveBeenCalled();
});

it('does not call findDOMNode when refs are forwarded and assigned', () => {
const domRef = React.createRef();

render(
<CSSMotion motionName="transition" visible ref={domRef}>
it('renders correctly when ref is passed internally', () => {
const { container } = render(
<CSSMotion motionName="transition" visible>
{(props, ref) => <div ref={ref} />}
</CSSMotion>,
);

act(() => {
jest.runAllTimers();
});

expect(ReactDOM.findDOMNode).not.toHaveBeenCalled();
// 检查 DOM 是否渲染出来
expect(container.querySelector('div')).toBeInTheDocument();
});
});

Expand Down