From 7c4f99cdbc99c3e775b9d5688e0549ab0ac7ee43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Sun, 31 Dec 2023 05:17:37 +0800 Subject: [PATCH] fix: fix --- src/hooks/useMaxCount.ts | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/hooks/useMaxCount.ts b/src/hooks/useMaxCount.ts index 870c6be6..1d369e25 100644 --- a/src/hooks/useMaxCount.ts +++ b/src/hooks/useMaxCount.ts @@ -3,23 +3,21 @@ import SelectContext from '../SelectContext'; import type { SelectContextProps } from '../SelectContext'; const useMaxCount = (multiple: boolean) => { - const { maxCount, rawValues } = React.useContext(SelectContext); - return { - shouldTruncate: React.useCallback( - (overCount = true) => { - if (!multiple || typeof maxCount === 'undefined') { - return false; - } - const overMaxCount = rawValues?.size >= maxCount; - return overCount ? overMaxCount : !overMaxCount; - }, - [multiple, maxCount, rawValues?.size], - ), - truncateLength: React.useMemo( - () => maxCount - rawValues?.size, - [maxCount, rawValues?.size], - ), - }; + const { maxCount, rawValues } = React.useContext(SelectContext) || {}; + const truncateLength = React.useMemo( + () => maxCount - rawValues?.size, + [maxCount, rawValues?.size], + ); + const shouldTruncate = React.useCallback( + (overCount = true) => { + if (!multiple || typeof maxCount === 'undefined') { + return false; + } + return overCount ? truncateLength < 0 : truncateLength >= 0; + }, + [multiple, maxCount, truncateLength], + ); + return { shouldTruncate, truncateLength }; }; export default useMaxCount;