This commit is contained in:
2026-05-25 17:08:18 +08:00
parent df501f6151
commit f1259b71b5
9178 changed files with 1626125 additions and 0 deletions
@@ -0,0 +1,12 @@
import type { InputProps } from '..';
import type { CountConfig, ShowCountFormatter } from '../interface';
type ForcedCountConfig = Omit<CountConfig, 'show'> & Pick<Required<CountConfig>, 'strategy'> & {
show: boolean;
showFormatter?: ShowCountFormatter;
};
/**
* Cut `value` by the `count.max` prop.
*/
export declare function inCountRange(value: string, countConfig: ForcedCountConfig): boolean;
export default function useCount(count?: CountConfig, showCount?: InputProps['showCount']): ForcedCountConfig;
export {};
+33
View File
@@ -0,0 +1,33 @@
import * as React from 'react';
/**
* Cut `value` by the `count.max` prop.
*/
export function inCountRange(value, countConfig) {
if (!countConfig.max) {
return true;
}
const count = countConfig.strategy(value);
return count <= countConfig.max;
}
export default function useCount(count, showCount) {
return React.useMemo(() => {
let mergedConfig = {};
if (showCount) {
mergedConfig.show = typeof showCount === 'object' && showCount.formatter ? showCount.formatter : !!showCount;
}
mergedConfig = {
...mergedConfig,
...count
};
const {
show,
...rest
} = mergedConfig;
return {
...rest,
show: !!show,
showFormatter: typeof show === 'function' ? show : undefined,
strategy: rest.strategy || (value => value.length)
};
}, [count, showCount]);
}