Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 9x 6x 6x 6x 6x 3x 3x 3x 13x 13x 13x | import { useState } from 'react'
import { isEmpty, isNumber } from 'asura-eye'
export type Options = {
/**
* @default 0
*/
min?: number,
max?: number
}
function getTargetValue(val: number, options: Options = {}) {
const { min = 0, max = Infinity } = options;
let target = val;
if (isNumber(max)) {
target = Math.min(max, target)
}
if (isNumber(min)) {
target = Math.max(min, target)
}
return target;
}
/**
* @title useCount
* @description 计数
* @param initialState {number=0}
* @param options {min?:number,max?:number}
* @returns [number, (value?:number)=>void]
*/
export function useCount(initialState = 0, options: Options = {})
: readonly [number, (value?: number) => void] {
const [state, setStateTemp] = useState<number>(getTargetValue(initialState, options))
const setState = (value: number) => {
if (isEmpty(value)) {
const result = getTargetValue(state + 1, options)
if (result !== state) setStateTemp(result)
return;
}
setStateTemp(getTargetValue(value, options))
return;
}
return [state, setState] as const
} |