[RFC] add @hookform/adapter to customize register
return value
#5250
Replies: 5 comments 11 replies
-
We created something like the following internally, that did work for us, if anyone is looking for something like this import {
FieldValues,
FieldPath,
RegisterOptions,
UseFormRegisterReturn,
UseFormReturn,
} from 'react-hook-form'
export type UseMuiFormRegister<TFieldValues extends FieldValues> = <
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
>(
name: TFieldName,
options?: RegisterOptions<TFieldValues, TFieldName>,
) => Omit<UseFormRegisterReturn, 'ref'> & { inputRef: UseFormRegisterReturn['ref'] }
export function useFormMuiRegister<V>({ register }: Pick<UseFormReturn<V>, 'register'>) {
const muiRegister: UseMuiFormRegister<V> = (name, opts) => {
const { ref: inputRef, ...fields } = register(name, opts)
return { ...fields, inputRef }
}
return muiRegister
} |
Beta Was this translation helpful? Give feedback.
-
For a better DX, I think this one could be under |
Beta Was this translation helpful? Give feedback.
-
I think it would be better for DX to provide <TextField
{...register("filedName", {
adapter: ({ ref, ...rest }) => ({ inputRef: ref, ...rest }),
...options
})}
/> |
Beta Was this translation helpful? Give feedback.
-
Personally, I prefer this syntax that @kotarella1110 suggested: <TextField
{...register("filedName", {
adapter: ({ ref, ...rest }) => ({ inputRef: ref, ...rest }),
// adapter: ({ ref, ...rest }) => ({ ...rest, inputRef: ref }), works as well
required: true,
min: 3,
})}
/> especially you don't have to worry about the |
Beta Was this translation helpful? Give feedback.
-
For long forms with a lot of inputs this becomes a bit annoying, why not specify your const { register } = useForm({ registerAdapter: ({ ref, ...rest }) => ({
inputRef: ref,
...rest
})
}); |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
It takes some effort to customize
register
return value.ref: #4629
Describe the solution you'd like
Use
adapter
to allow customizeregister
return value.Pros
register
doesn't changeCons
Comments
It's just a simple function, users can implement themselves
Describe alternatives you've considered
It is also possible to change the API of register, but I personally do not like it.
https://twitter.com/HookForm/status/1381918850753568773
Beta Was this translation helpful? Give feedback.
All reactions