import React from "react" import { FieldValues, Path, UseFormRegister, RegisterOptions, } from "react-hook-form" export type WithReactHookFormProps = { name: Path register?: UseFormRegister rules?: Omit< RegisterOptions, "valueAsNumber" | "valueAsDate" | "setValueAs" > disabled?: boolean } function getDisplayName(WrappedComponent: React.ComponentType): string { return WrappedComponent.displayName || WrappedComponent.name || 'Component' } export function withReactHookForm< P extends { onChange?: (...args: any[]) => void onBlur?: (...args: any[]) => void ref?: any }, T extends FieldValues = FieldValues >(WrappedComponent: React.ComponentType

) { const WithReactHookFormComponent = React.forwardRef>( (props, ref) => { const { register, name, rules, disabled, ...rest } = props if (!register) { return () } const fieldRegistration = register(name, rules) const fieldProps = { ...rest, ...fieldRegistration, disabled: disabled || (rest as any).disabled, ref: ref || fieldRegistration.ref, onChange: (...args: any[]) => { fieldRegistration.onChange?.(...args) ;(rest as P).onChange?.(...args) }, onBlur: (...args: any[]) => { fieldRegistration.onBlur?.(...args) ;(rest as P).onBlur?.(...args) }, } return } ) WithReactHookFormComponent.displayName = `WithReactHookForm(${getDisplayName(WrappedComponent)})` return WithReactHookFormComponent }