1
This commit is contained in:
+12
@@ -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
@@ -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]);
|
||||
}
|
||||
Reference in New Issue
Block a user