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: CSSMotionList support index in children #49

Merged
merged 2 commits into from
Sep 5, 2023
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
16 changes: 13 additions & 3 deletions src/CSSMotionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const MOTION_PROP_NAMES = [
];

export interface CSSMotionListProps
extends Omit<CSSMotionProps, 'onVisibleChanged'>,
extends Omit<CSSMotionProps, 'onVisibleChanged' | 'children'>,
Omit<React.HTMLAttributes<any>, 'children'> {
keys: (React.Key | { key: React.Key; [name: string]: any })[];
component?: string | React.ComponentType | false;
Expand All @@ -47,6 +47,16 @@ export interface CSSMotionListProps
onVisibleChanged?: (visible: boolean, info: { key: React.Key }) => void;
/** All motion leaves in the screen */
onAllRemoved?: () => void;
children?: (
props: {
visible?: boolean;
className?: string;
style?: React.CSSProperties;
index?: number;
[key: string]: any;
},
ref: (node: any) => void,
) => React.ReactElement;
}

export interface CSSMotionListState {
Expand Down Expand Up @@ -138,7 +148,7 @@ export function genCSSMotionList(

return (
<Component {...restProps}>
{keyEntities.map(({ status, ...eventProps }) => {
{keyEntities.map(({ status, ...eventProps }, index) => {
const visible = status === STATUS_ADD || status === STATUS_KEEP;
return (
<CSSMotion
Expand All @@ -158,7 +168,7 @@ export function genCSSMotionList(
}
}}
>
{children}
{(props, ref) => children({ ...props, index }, ref)}
</CSSMotion>
);
})}
Expand Down
26 changes: 26 additions & 0 deletions tests/CSSMotionList.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,30 @@ describe('CSSMotionList', () => {
expect(onVisibleChanged).toHaveBeenCalledWith(false, { key: 'a' });
expect(onAllRemoved).toHaveBeenCalled();
});

it('should support index', () => {
const CSSMotionList = genCSSMotionList(false);

const Demo = ({ keys }) => (
<CSSMotionList motionName="transition" keys={keys}>
{({ key, style, className, index }) => (
<div
key={key}
style={style}
className={classNames('motion-box', className)}
>
{index}
</div>
)}
</CSSMotionList>
);

const { container } = render(<Demo keys={['a', 'b', 'c']} />);
expect(container.querySelectorAll('.motion-box')[0].textContent).toEqual(
'0',
);
expect(container.querySelectorAll('.motion-box')[1].textContent).toEqual(
'1',
);
});
});
Loading