From e9a30070dbae04a889015be05aff398079f35e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=9D=B1=E6=BE=94?= Date: Sun, 5 Dec 2021 22:59:22 +0800 Subject: [PATCH] fix: fix computed error with MAX/MIN --- src/hooks/useOffsetX.ts | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/hooks/useOffsetX.ts b/src/hooks/useOffsetX.ts index 9cc7b48e..daf4b60b 100644 --- a/src/hooks/useOffsetX.ts +++ b/src/hooks/useOffsetX.ts @@ -35,24 +35,36 @@ export const useOffsetX = (opts: IOpts) => { function getDefaultPos( _type: 'positive' | 'negative', _count: number - ) { - let boundary = null; + ): { + MAX: number; + MIN: number; + startPos: number; + } { + let MAX = null; + let MIN = null; + + let startPos: number = defaultPos; if (_type === 'positive') { - boundary = _count * width; + MAX = _count * width; + MIN = -(VALID_LENGTH - _count) * width; } else { - boundary = (VALID_LENGTH - _count) * width; + MAX = (VALID_LENGTH - _count) * width; + MIN = -_count * width; } - if (defaultPos > boundary) { - return boundary - defaultPos; + if (defaultPos > MAX) { + startPos = MAX - defaultPos; } - return defaultPos; + + return { + startPos, + MAX, + MIN, + }; } - const startPos = getDefaultPos(type, viewCount); - const MAX = viewCount * width; - const MIN = -((VALID_LENGTH - viewCount) * width); + const { startPos, MAX, MIN } = getDefaultPos(type, viewCount); const inputRange = [ -TOTAL_WIDTH,