import { UseFormReturn } from 'react-hook-form'; import { FormDataPath } from '../../types'; import { FormGroup } from './form-groups'; export const useFormState = >( methods: UseFormReturn<{ groups: FormGroup[] }> ) => { return { fieldState: { isDirty: (index: number, fieldName: keyof T) => methods.getFieldState( `groups.${index}.data.${String(fieldName)}` as FormDataPath ).isDirty, isTouched: (index: number, fieldName: keyof T) => methods.getFieldState( `groups.${index}.data.${String(fieldName)}` as FormDataPath ).isTouched, get: (index: number, fieldName: keyof T) => methods.getFieldState( `groups.${index}.data.${String(fieldName)}` as FormDataPath ), }, formState: { isDirty: methods.formState.isDirty, isValid: methods.formState.isValid, touched: Object.keys(methods.formState.touchedFields).length > 0, isSubmitting: methods.formState.isSubmitting, submitCount: methods.formState.submitCount, isValidating: methods.formState.isValidating, }, reset: methods.reset, trigger: methods.trigger, watch: methods.watch, }; };