From b438d2deb4f833ca1c8290a059f91c3c9c8ba096 Mon Sep 17 00:00:00 2001 From: daisy <904492381@qq.com> Date: Mon, 20 May 2024 14:51:57 +0800 Subject: [PATCH] feat: add offsetX to renderFn (#269) * feat: add offsetX to renderFn * test: add case --- src/List.tsx | 1 + src/hooks/useChildren.tsx | 4 +++- src/interface.ts | 2 +- tests/props.test.js | 13 +++++++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/List.tsx b/src/List.tsx index ac4029d4..8a850c87 100644 --- a/src/List.tsx +++ b/src/List.tsx @@ -504,6 +504,7 @@ export function RawList(props: ListProps, ref: React.Ref) { start, end, scrollWidth, + offsetLeft, setInstanceRef, children, sharedConfig, diff --git a/src/hooks/useChildren.tsx b/src/hooks/useChildren.tsx index 8b0bfe61..8c4fdf6d 100644 --- a/src/hooks/useChildren.tsx +++ b/src/hooks/useChildren.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import type { SharedConfig, RenderFunc } from '../interface'; +import type { RenderFunc, SharedConfig } from '../interface'; import { Item } from '../Item'; export default function useChildren( @@ -7,6 +7,7 @@ export default function useChildren( startIndex: number, endIndex: number, scrollWidth: number, + offsetX: number, setNodeRef: (item: T, element: HTMLElement) => void, renderFunc: RenderFunc, { getKey }: SharedConfig, @@ -17,6 +18,7 @@ export default function useChildren( style: { width: scrollWidth, }, + offsetX, }) as React.ReactElement; const key = getKey(item); diff --git a/src/interface.ts b/src/interface.ts index f5431a3e..e0fd765d 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -1,7 +1,7 @@ export type RenderFunc = ( item: T, index: number, - props: { style?: React.CSSProperties }, + props: { style: React.CSSProperties; offsetX: number }, ) => React.ReactNode; export interface SharedConfig { diff --git a/tests/props.test.js b/tests/props.test.js index 45edeff2..bbc41e81 100644 --- a/tests/props.test.js +++ b/tests/props.test.js @@ -40,4 +40,17 @@ describe('Props', () => { expect(wrapper.find('.prefix-holder-inner').length).toBeTruthy(); }); + + it('offsetX in renderFn', () => { + let scrollLeft; + mount( + id} prefixCls="prefix"> + {(id, _, { offsetX }) => { + scrollLeft = offsetX; + return
{id}
}} +
, + ); + + expect(scrollLeft).toEqual(0); + }); });