Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
44 changes: 25 additions & 19 deletions src/CSSMotion.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* eslint-disable react/default-props-match-prop-types, react/no-multi-comp, react/prop-types */
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';
import { getNodeRef, supportRef } from '@rc-component/util/lib/ref';
import {
getNodeRef,
supportRef,
useComposeRef,
} from '@rc-component/util/lib/ref';
import { clsx } from 'clsx';
import * as React from 'react';
import { useRef } from 'react';
Expand Down Expand Up @@ -189,7 +193,7 @@ export function genCSSMotion(config: CSSMotionConfig) {
}

// We should render children when motionStyle is sync with stepStatus
return React.useMemo(() => {
const motionChildren = React.useMemo(() => {
if (styleReady === 'NONE') {
return null;
}
Expand Down Expand Up @@ -246,25 +250,27 @@ export function genCSSMotion(config: CSSMotionConfig) {
);
}

// Auto inject ref if child node not have `ref` props
if (
React.isValidElement(motionChildren) &&
supportRef(motionChildren)
) {
const originNodeRef = getNodeRef(motionChildren);

if (!originNodeRef) {
motionChildren = React.cloneElement(
motionChildren as React.ReactElement,
{
ref: nodeRef,
},
);
}
}

return motionChildren;
}, [idRef.current]) as React.ReactElement;

const canHoldRef =
React.isValidElement(motionChildren) && supportRef(motionChildren);
const originNodeRef = canHoldRef ? getNodeRef(motionChildren) : null;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我微调一下

const shouldInjectRef = canHoldRef && originNodeRef !== nodeRef;
const mergedNodeRef = useComposeRef(
shouldInjectRef ? originNodeRef : null,
nodeRef,
);

// Preserve original behavior when child already uses motion's ref directly.
// Only compose refs when child owns another ref or misses the motion ref.
if (shouldInjectRef) {
return React.cloneElement(motionChildren, {
ref: mergedNodeRef,
});
}

return motionChildren;
},
);

Expand Down
45 changes: 45 additions & 0 deletions tests/CSSMotion.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,51 @@ describe('CSSMotion', () => {

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

it('supports existing child refs for motion end', () => {
const motionRef = React.createRef<CSSMotionRef>();
const childRef = React.createRef<HTMLDivElement>();

const Demo = ({ visible }: { visible: boolean }) => (
<CSSMotion
motionName="transition"
motionAppear={false}
visible={visible}
ref={motionRef}
>
{({ style, className }) => (
<div
ref={childRef}
style={style}
className={clsx('motion-box', className)}
/>
)}
</CSSMotion>
);

const { container, rerender } = render(<Demo visible />);

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

expect(motionRef.current.nativeElement).toBe(childRef.current);

rerender(<Demo visible={false} />);

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

fireEvent.transitionEnd(childRef.current!);

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

expect(container.querySelector('.motion-box')).toBeFalsy();
expect(ReactDOM.findDOMNode).not.toHaveBeenCalled();
});
});

describe('onVisibleChanged', () => {
Expand Down
Loading