diff --git a/src/CSSMotionList.tsx b/src/CSSMotionList.tsx index 99ceb1d..2b2f03a 100644 --- a/src/CSSMotionList.tsx +++ b/src/CSSMotionList.tsx @@ -38,7 +38,7 @@ const MOTION_PROP_NAMES = [ ]; export interface CSSMotionListProps - extends Omit, + extends Omit, Omit, 'children'> { keys: (React.Key | { key: React.Key; [name: string]: any })[]; component?: string | React.ComponentType | false; @@ -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 { @@ -138,7 +148,7 @@ export function genCSSMotionList( return ( - {keyEntities.map(({ status, ...eventProps }) => { + {keyEntities.map(({ status, ...eventProps }, index) => { const visible = status === STATUS_ADD || status === STATUS_KEEP; return ( - {children} + {(props, ref) => children({ ...props, index }, ref)} ); })} diff --git a/tests/CSSMotionList.spec.tsx b/tests/CSSMotionList.spec.tsx index 85dfce0..10b0b9d 100644 --- a/tests/CSSMotionList.spec.tsx +++ b/tests/CSSMotionList.spec.tsx @@ -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 }) => ( + + {({ key, style, className, index }) => ( +
+ {index} +
+ )} +
+ ); + + const { container } = render(); + expect(container.querySelectorAll('.motion-box')[0].textContent).toEqual( + '0', + ); + expect(container.querySelectorAll('.motion-box')[1].textContent).toEqual( + '1', + ); + }); });