Skip to content

Commit ec369c1

Browse files
committed
feat: useForm - add options in reset, setValue methods
1 parent 873fdae commit ec369c1

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/Shared/Hooks/useForm/useForm.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,7 @@ export const useForm = <T extends Record<keyof T, any> = {}>(options?: {
263263
const validation = validations[name]
264264
const error = checkValidation(data[name], validation)
265265

266-
if (error) {
267-
setErrors({ ...errors, [name]: error })
268-
}
266+
setErrors({ ...errors, [name]: error })
269267

270268
return error
271269
}
@@ -288,10 +286,19 @@ export const useForm = <T extends Record<keyof T, any> = {}>(options?: {
288286
shouldDirty?: boolean
289287
/** A boolean indicating whether to mark the field as touched after setting the value. */
290288
shouldTouch?: boolean
289+
/** A boolean indicating whether to trigger validation after setting the value. */
290+
triggerError?: boolean
291291
},
292292
) => {
293293
// Update the form data with the new value.
294294
setData((prev) => ({ ...prev, [name]: value }))
295+
if (valueOptions?.triggerError) {
296+
const validations = getValidations({ ...data, [name]: value })
297+
const validation = validations[name]
298+
const error = checkValidation(value, validation)
299+
300+
setErrors((prev) => ({ ...prev, [name]: error }))
301+
}
295302
if (valueOptions?.shouldDirty) {
296303
const initialValues = initialValuesRef.current
297304
// Mark the field as dirty if the new value differs from the initial value.
@@ -320,25 +327,38 @@ export const useForm = <T extends Record<keyof T, any> = {}>(options?: {
320327
keepErrors?: boolean
321328
/** A boolean indicating whether the form should check for dirty state upon reset. */
322329
triggerDirty?: boolean
330+
/** A boolean indicating whether the form should check for errors upon reset. */
331+
triggerError?: boolean
323332
/** A boolean indicating whether the initial values of the form should be retained after reset. If false, provided formData will become initial data. */
324333
keepInitialValues?: boolean
334+
/** Partial initial values to override the current initial values upon reset.
335+
* @note `keepInitialValues` will have no effect when this is provided.
336+
*/
337+
formInitialValues?: Partial<T>
325338
},
326339
) => {
327340
const {
328341
keepDirty = false,
329342
keepTouched = false,
330343
keepErrors = false,
331344
triggerDirty = false,
345+
triggerError = false,
332346
keepInitialValues = false,
347+
formInitialValues = {},
333348
} = resetOptions ?? {} // Destructure reset options with defaults.
334349

335350
setData(formData)
336351

337-
if (!keepInitialValues) {
352+
if (Object.keys(formInitialValues).length) {
353+
initialValuesRef.current = formInitialValues
354+
} else if (!keepInitialValues) {
338355
initialValuesRef.current = formData
339356
}
340357

341-
if (!keepErrors) {
358+
if (triggerError) {
359+
const newErrors = getErrorsFromFormData(formData)
360+
setErrors(newErrors)
361+
} else if (!keepErrors) {
342362
setErrors({})
343363
}
344364

0 commit comments

Comments
 (0)