Repository navigation
React Hook Form: The Hookening - #1254
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
| } | ||
|
|
||
| export interface SideModalProps extends Omit<DialogProps, 'isOpen'>, ChildrenProp { | ||
| export type SideModalProps = { |
There was a problem hiding this comment.
I found the DialogProps noisy and we're not using them
| export type EditSideModalFormProps<TFieldValues, Data> = SetRequired< | ||
| CreateSideModalFormProps<TFieldValues, Data>, | ||
| 'defaultValues' | ||
| > |
There was a problem hiding this comment.
Compare to the originals, which I always have a hard time wrapping my head around:
Lines 8 to 23 in 7815e95
There's no analogue to CreateFormProps yet because that's only used for instance create, which is the one form that's not in a side modal.
There was a problem hiding this comment.
I'm not really super happy with this abstraction all around anyway. I think the relationship between the sidemodal and the form could likely be revisited.
There was a problem hiding this comment.
Totally agree. I think I would prefer to see them composed at the call site.
| title: ReactNode | ||
| onSubmit: SubmitHandler<TFieldValues> | ||
| form: UseFormReturn<TFieldValues> | ||
| } |
There was a problem hiding this comment.
Compare to the original, which extends FormProps:
console/app/components/form/SideModalForm.tsx
Lines 10 to 16 in 7815e95
which itself extends FormikConfig:
console/app/components/form/Form.tsx
Lines 17 to 23 in 7815e95
I never have any idea what the props are on these things!
| id?: string | ||
| children: React.ReactNode | ||
| title: string | ||
| } |
| })} | ||
| aria-describedby={description ? `${id}-label-tip` : undefined} | ||
| // leave ref out because UITextField doesn't take it and we don't need it | ||
| {...exclude(field, 'ref')} |
There was a problem hiding this comment.
Unsure about this. It seems to work perfectly well without the ref, so I can't think of anything it would need it for. The alternative here would be to wrap TextInput with React.forwardRef, which would be a pretty reasonable thing to do in general.
| // TODO: I don't get why we have to cast. It should know `name` is | ||
| // valid because we require that TFieldValues has that key | ||
| name={name} |
There was a problem hiding this comment.
Looks like you figured the casting issue out?
There was a problem hiding this comment.
Yep. Got rid of the comment. name is a prop with type Path<TFieldValues> so it's good.
| // TODO: we don't want to have manually specify this every time we make a | ||
| // form. might have to make a wrapper hook with different defaults | ||
| // | ||
| // TODO: the docs warn against the performance implications of validating on | ||
| // every change | ||
| const form = useForm({ defaultValues, mode: 'all' }) | ||
| const canSubmit = form.formState.isDirty && form.formState.isValid | ||
|
|
||
| // TODO: calling useForm all the way up here means it's always mounted whether | ||
| // the side modal is open or not, which means form state hangs around even | ||
| // when the modal is closed. Using useEffect like this is a code smell, (and | ||
| // error-prone, and would have to be wrapped up in a custom useForm in order | ||
| // to get consistent correct behavior everywhere) so I would like this to work | ||
| // differently. Ideally useForm would be called one level lower, e.g., in a | ||
| // component that wraps name and description in the children of SideModalForm | ||
| // so it only gets rendered when the form is open. | ||
| useEffect(() => { | ||
| if (!isOpen) form.reset() | ||
| }, [form, isOpen]) |
There was a problem hiding this comment.
Seems to me that if this bit lived inside the SideModalForm we could nix more of the props. The one annoying bit is getting control to the children while having the types right. There's a few ways we could do that though.
There was a problem hiding this comment.
Yep, exactly what I was thinking.
There was a problem hiding this comment.
Here's what that looks like. Render props are always goofy but it's really not bad. I think it's better. The useForm, the reset, and the canSubmit stuff are all bundled up in SideModalForm.
| export const TextInput = React.forwardRef< | ||
| HTMLInputElement, | ||
| TextInputBaseProps & TextAreaProps | ||
| >(({ type = 'text', error, className, fieldClassName, ...fieldProps }, ref) => ( |
There was a problem hiding this comment.
Nice, yeah. We'll likely need to do that to the other base input components too.
just-be-dev
left a comment
There was a problem hiding this comment.
Awesome, great work. I think we can keep chipping away at the form structure to get it a bit simpler, but this is already feeling a lot cleaner.
Co-authored-by: Justin Bennett <[email protected]>


Starts the work on #804, incrementally switching to RHF. RHF is quite small, only 9kb, so that's pretty cool.
What's in here
SideModalForm,NameField, andDescriptionFieldrefintoTextInput, which doesn't take one (oddly this doesn't appear to affect behavior)Before/After
Hard to spot any difference.
2022-10-25-rhf-before-after.mp4