mirror of
https://github.com/portainer/portainer.git
synced 2025-07-20 13:59:40 +02:00
move debouncing to the component (from the validation). debounce returns undefined when it's not calling the debounced function, and undefined is considered a validation error.
18 lines
449 B
TypeScript
18 lines
449 B
TypeScript
import _ from 'lodash';
|
|
import { useState, useRef } from 'react';
|
|
|
|
export function useDebounce(
|
|
defaultValue: string,
|
|
onChange: (value: string) => void
|
|
) {
|
|
const [searchValue, setSearchValue] = useState(defaultValue);
|
|
|
|
const onChangeDebounces = useRef(_.debounce(onChange, 300));
|
|
|
|
return [searchValue, handleChange] as const;
|
|
|
|
function handleChange(value: string) {
|
|
setSearchValue(value);
|
|
onChangeDebounces.current(value);
|
|
}
|
|
}
|