| | import React from 'react'; |
| |
|
| | import getProxyFormState from './logic/getProxyFormState'; |
| | import type { |
| | FieldValues, |
| | UseFormStateProps, |
| | UseFormStateReturn, |
| | } from './types'; |
| | import { useFormContext } from './useFormContext'; |
| | import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export function useFormState< |
| | TFieldValues extends FieldValues = FieldValues, |
| | TTransformedValues = TFieldValues, |
| | >( |
| | props?: UseFormStateProps<TFieldValues, TTransformedValues>, |
| | ): UseFormStateReturn<TFieldValues> { |
| | const methods = useFormContext<TFieldValues, any, TTransformedValues>(); |
| | const { control = methods.control, disabled, name, exact } = props || {}; |
| | const [formState, updateFormState] = React.useState(control._formState); |
| | const _localProxyFormState = React.useRef({ |
| | isDirty: false, |
| | isLoading: false, |
| | dirtyFields: false, |
| | touchedFields: false, |
| | validatingFields: false, |
| | isValidating: false, |
| | isValid: false, |
| | errors: false, |
| | }); |
| |
|
| | useIsomorphicLayoutEffect( |
| | () => |
| | control._subscribe({ |
| | name, |
| | formState: _localProxyFormState.current, |
| | exact, |
| | callback: (formState) => { |
| | !disabled && |
| | updateFormState({ |
| | ...control._formState, |
| | ...formState, |
| | }); |
| | }, |
| | }), |
| | [name, disabled, exact], |
| | ); |
| |
|
| | React.useEffect(() => { |
| | _localProxyFormState.current.isValid && control._setValid(true); |
| | }, [control]); |
| |
|
| | return React.useMemo( |
| | () => |
| | getProxyFormState( |
| | formState, |
| | control, |
| | _localProxyFormState.current, |
| | false, |
| | ), |
| | [formState, control], |
| | ); |
| | } |
| |
|