Skip to content

Commit 67d50ad

Browse files
committed
✨ provide defaultValues to formState
1 parent 349715e commit 67d50ad

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/core/src/logic/creatFormControl.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
6363
isSubmitting: false,
6464
isSubmitSuccessful: false,
6565
isValid: false,
66+
defaultValues: _options.defaultValues || {},
6667
errors: {} as FieldErrors<TFieldValues>,
6768
}) as FormState<TFieldValues>
6869

@@ -346,11 +347,16 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
346347
_fields[name].inputValue.value = value
347348
}
348349

349-
const setValue: UseFormSetValue<TFieldValues, FieldsKey> = async (name, value, config) => {
350+
const setValue: UseFormSetValue<TFieldValues, FieldsKey> = async (name, value, config = {}) => {
351+
if (isNullOrUndefined(_fields[name])) {
352+
warn(`setValue cannot set not exist field #${name as string}`)
353+
return
354+
}
355+
350356
config = {
351-
shouldValidate: false,
352-
shouldDirty: false,
353-
...(config || {}),
357+
shouldValidate: true,
358+
shouldDirty: true,
359+
...config,
354360
}
355361

356362
_setFieldsValue(name, value)

packages/core/src/types/form.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,6 @@ export interface FormState<TFieldValues> {
153153
isSubmitting: boolean
154154
isValidating: boolean
155155
isValid: boolean
156+
defaultValues: Partial<DefaultValues<TFieldValues>>
156157
errors: FieldErrors<TFieldValues>
157158
}

0 commit comments

Comments
 (0)