From 7f94027352debed9e7cb494bb6fa8049cbb9375c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=9D=B1=E6=BE=94?= Date: Fri, 4 Nov 2022 11:31:26 +0800 Subject: [PATCH] fix: getCurrentIndex returns wrong value when data count is < 3 fix #299 --- src/hooks/useCarouselController.tsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/hooks/useCarouselController.tsx b/src/hooks/useCarouselController.tsx index 424c5477..6acde29f 100644 --- a/src/hooks/useCarouselController.tsx +++ b/src/hooks/useCarouselController.tsx @@ -12,7 +12,7 @@ import type { TCarouselProps, WithTimingAnimation, } from "../types"; -import { convertToSharedIndex } from "../utils/computedWithAutoFillData"; +import { computedRealIndexWithAutoFillData, convertToSharedIndex } from "../utils/computedWithAutoFillData"; import { dealWithAnimation } from "../utils/dealWithAnimation"; import { handlerOffsetDirection } from "../utils/handlerOffsetDirection"; import { round } from "../utils/log"; @@ -118,8 +118,15 @@ export function useCarouselController(options: IOpts): ICarouselController { ); const getCurrentIndex = React.useCallback(() => { - return index.value; - }, [index]); + const realIndex = computedRealIndexWithAutoFillData({ + index: index.value, + dataLength: dataInfo.originalLength, + loop, + autoFillData: autoFillData!, + }); + + return realIndex; + }, [index, autoFillData, dataInfo, loop]); const canSliding = React.useCallback(() => { return !dataInfo.disable;