mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
timeout-based form sending
This commit is contained in:
parent
770a256ab8
commit
033b5ca45d
1 changed files with 13 additions and 5 deletions
|
@ -35,7 +35,9 @@ export const Tags: FC<IProps> = ({ tags, is_editable, onTagsChange, ...props })
|
|||
const onInput = useCallback(
|
||||
({ target: { value } }: ChangeEvent<HTMLInputElement>) => {
|
||||
setInput(value);
|
||||
if (timer) clearTimeout(timer.current);
|
||||
if (timer) {
|
||||
clearTimeout(timer.current);
|
||||
}
|
||||
},
|
||||
[setInput, timer]
|
||||
);
|
||||
|
@ -69,8 +71,10 @@ export const Tags: FC<IProps> = ({ tags, is_editable, onTagsChange, ...props })
|
|||
const onSubmit = useCallback(() => {
|
||||
if (length(tags) === length(data) && isEmpty(symmetricDifference(tags, data))) return;
|
||||
|
||||
if (timer.current) clearTimeout(timer.current);
|
||||
|
||||
onTagsChange(data.map(tag => tag.title));
|
||||
}, [tags, data, onTagsChange]);
|
||||
}, [tags, data, onTagsChange, timer]);
|
||||
|
||||
const onBlur = useCallback(() => {
|
||||
clearTimeout(timer.current);
|
||||
|
@ -79,10 +83,14 @@ export const Tags: FC<IProps> = ({ tags, is_editable, onTagsChange, ...props })
|
|||
|
||||
useEffect(() => setData(tags), [tags]);
|
||||
useEffect(() => {
|
||||
timer.current = setTimeout(onSubmit, 3000);
|
||||
timer.current = setTimeout(() => {
|
||||
onSubmit();
|
||||
}, 3000);
|
||||
|
||||
return () => clearTimeout(timer.current);
|
||||
}, [data]);
|
||||
return () => {
|
||||
clearTimeout(timer.current);
|
||||
};
|
||||
}, [data, tags]);
|
||||
|
||||
return (
|
||||
<TagField {...props}>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue