Skip to content
Merged
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
43 changes: 23 additions & 20 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 {
composeRef,
getNodeRef,
supportNodeRef,
} from '@rc-component/util/lib/ref';
import { clsx } from 'clsx';
import * as React from 'react';
import { useRef } from 'react';
Expand Down Expand Up @@ -106,6 +110,10 @@ export interface CSSMotionState {
prevProps?: CSSMotionProps;
}

export function isRefNotConsumed(children?: CSSMotionProps['children']) {
return children?.length < 2;
}

/**
* `transitionSupport` is used for none transition test case.
* Default we use browser transition event support check.
Expand Down Expand Up @@ -189,12 +197,12 @@ export function genCSSMotion(config: CSSMotionConfig) {
}

// We should render children when motionStyle is sync with stepStatus
return React.useMemo(() => {
const returnNode = React.useMemo<React.ReactElement | null>(() => {
if (styleReady === 'NONE') {
return null;
}

let motionChildren: React.ReactNode;
let motionChildren: React.ReactElement | null;
const mergedProps = { ...eventProps, visible };

if (!children) {
Expand Down Expand Up @@ -246,25 +254,20 @@ 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]);

if (isRefNotConsumed(children) && supportNodeRef(returnNode)) {
const originNodeRef = getNodeRef(returnNode);

if (originNodeRef !== nodeRef) {
return React.cloneElement(returnNode as any, {
ref: composeRef(originNodeRef, nodeRef),
});
}
}

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

Expand Down
14 changes: 12 additions & 2 deletions src/CSSMotionList.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint react/prop-types: 0 */
import * as React from 'react';
import type { CSSMotionProps } from './CSSMotion';
import OriginCSSMotion from './CSSMotion';
import OriginCSSMotion, { isRefNotConsumed } from './CSSMotion';
import type { KeyObject } from './util/diff';
import {
diffKeys,
Expand Down Expand Up @@ -59,6 +59,10 @@ export interface CSSMotionListProps
) => React.ReactElement;
}

type ChildrenWithoutRef = (
props: Parameters<CSSMotionListProps['children']>[0],
) => ReturnType<CSSMotionListProps['children']>;

export interface CSSMotionListState {
keyEntities: KeyObject[];
}
Expand Down Expand Up @@ -174,7 +178,13 @@ export function genCSSMotionList(
}
}}
>
{(props, ref) => children({ ...props, index }, ref)}
{isRefNotConsumed(children)
? props =>
(children as ChildrenWithoutRef)({
...props,
index,
})
: (props, ref) => children({ ...props, index }, ref)}
</CSSMotion>
);
})}
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