1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 05:19:39 +02:00

fix(wizard): debounce name state [EE-4177] (#8042)

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.
This commit is contained in:
Chaim Lev-Ari 2022-11-21 19:33:08 +02:00 committed by GitHub
parent 253a3a2b40
commit 7006c17ce4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 74 additions and 58 deletions

View file

@ -1,15 +1,18 @@
import { useEffect, useState } from 'react';
import _ from 'lodash';
import { useState, useRef } from 'react';
export function useDebounce<T>(value: T, delay = 500): T {
const [debouncedValue, setDebouncedValue] = useState<T>(value);
export function useDebounce(
defaultValue: string,
onChange: (value: string) => void
) {
const [searchValue, setSearchValue] = useState(defaultValue);
useEffect(() => {
const timer = setTimeout(() => setDebouncedValue(value), delay);
const onChangeDebounces = useRef(_.debounce(onChange, 300));
return () => {
clearTimeout(timer);
};
}, [value, delay]);
return [searchValue, handleChange] as const;
return debouncedValue;
function handleChange(value: string) {
setSearchValue(value);
onChangeDebounces.current(value);
}
}