Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit e818cea

Browse files
committed
feat: Improve field validation to avoid state updates on multiple validations
1 parent c400e74 commit e818cea

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/useFormHandler.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,21 @@ export const useFormHandler = <
124124
if (_refs[name]._disabled) {
125125
return
126126
}
127+
const timeout = setTimeout(
128+
() => setValidating(name as keyof TForm, true),
129+
20
130+
)
127131
for (const validation of Object.values(_refs[name]._validations)) {
128-
const timeout = setTimeout(
129-
() => setValidating(name as keyof TForm, true),
130-
20
131-
)
132132
const result = await (validation as any)(values[name])
133133

134-
clearTimeout(timeout)
135-
136-
setValidating(name as keyof TForm, false)
137134
if (result !== true) {
138135
formState.errors[name] = result
139136
break
140137
}
141138
delete formState.errors[name]
142139
}
140+
clearTimeout(timeout)
141+
setValidating(name as keyof TForm, false)
143142
}
144143

145144
const _validateForm = async <T extends Record<string, any>>(

0 commit comments

Comments
 (0)