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;