(
props: UseFormProps<TFieldValues, TContext, TTransformedValues> = {},
)
| 44 | * ``` |
| 45 | */ |
| 46 | export function useForm< |
| 47 | TFieldValues extends FieldValues = FieldValues, |
| 48 | TContext = any, |
| 49 | TTransformedValues = TFieldValues, |
| 50 | >( |
| 51 | props: UseFormProps<TFieldValues, TContext, TTransformedValues> = {}, |
| 52 | ): UseFormReturn<TFieldValues, TContext, TTransformedValues> { |
| 53 | const _formControl = React.useRef< |
| 54 | UseFormReturn<TFieldValues, TContext, TTransformedValues> | undefined |
| 55 | >(undefined); |
| 56 | const _values = React.useRef<typeof props.values>(undefined); |
| 57 | const _formControlProp = React.useRef(props.formControl); |
| 58 | const [formState, updateFormState] = React.useState<FormState<TFieldValues>>( |
| 59 | () => ({ |
| 60 | ...cloneObject(DEFAULT_FORM_STATE), |
| 61 | isLoading: isFunction(props.defaultValues), |
| 62 | errors: props.errors || {}, |
| 63 | disabled: props.disabled || false, |
| 64 | defaultValues: isFunction(props.defaultValues) |
| 65 | ? undefined |
| 66 | : props.defaultValues, |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | if ( |
| 71 | !_formControl.current || |
| 72 | (props.formControl && _formControlProp.current !== props.formControl) |
| 73 | ) { |
| 74 | _formControlProp.current = props.formControl; |
| 75 | if (props.formControl) { |
| 76 | _formControl.current = { |
| 77 | ...props.formControl, |
| 78 | formState, |
| 79 | }; |
| 80 | |
| 81 | if (props.defaultValues && !isFunction(props.defaultValues)) { |
| 82 | props.formControl.reset(props.defaultValues, props.resetOptions); |
| 83 | } |
| 84 | } else { |
| 85 | const { formControl, ...rest } = createFormControl(props); |
| 86 | |
| 87 | _formControl.current = { |
| 88 | ...rest, |
| 89 | formState, |
| 90 | }; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | const control = _formControl.current.control; |
| 95 | control._options = props; |
| 96 | |
| 97 | useIsomorphicLayoutEffect(() => { |
| 98 | const sub = control._subscribe({ |
| 99 | formState: control._proxyFormState, |
| 100 | callback: () => |
| 101 | updateFormState({ |
| 102 | ...control._formState, |
| 103 | defaultValues: |
searching dependent graphs…