useSearchValue.ts 586 字节
Newer Older
P
Peter Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import useDebounce from '~/hooks/useDebounce';

const isEmptyValue = (value: unknown): boolean =>
    (Array.isArray(value) && !value.length) || ('string' === typeof value && value === '');

const useSearchValue = <T>(value: T, delay = 275): T => {
    const debounced = useDebounce(value, delay);
    // return empty value immediately
    if (isEmptyValue(value)) {
        return value;
    }
    // if debounced value is empty, return non-empty value immediately
    if (isEmptyValue(debounced)) {
        return value;
    }
    return debounced;
};

export default useSearchValue;