Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jia-nan committed Dec 30, 2023
1 parent faa4b41 commit 7c4f99c
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/hooks/useMaxCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ import SelectContext from '../SelectContext';
import type { SelectContextProps } from '../SelectContext';

const useMaxCount = (multiple: boolean) => {
const { maxCount, rawValues } = React.useContext<SelectContextProps>(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<number>(
() => maxCount - rawValues?.size,
[maxCount, rawValues?.size],
),
};
const { maxCount, rawValues } = React.useContext<SelectContextProps>(SelectContext) || {};
const truncateLength = React.useMemo<number>(
() => 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;

0 comments on commit 7c4f99c

Please sign in to comment.