Skip to content

React Hook Form: The Hookening - #1254

Merged
david-crespo merged 32 commits into
mainfrom
react-hook-form
Oct 26, 2022
Merged

david-crespo merged 32 commits into
mainfrom
react-hook-form

Conversation

@david-crespo

@david-crespo david-crespo commented Oct 25, 2022 •

Copy link
Copy Markdown
Collaborator

Starts the work on #804, incrementally switching to RHF. RHF is quite small, only 9kb, so that's pretty cool.

What's in here

  • Org create form uses RHF
  • New RHF SideModalForm, NameField, and DescriptionField
  • Validation works the same as before
  • Fix errors in console due to us spreading a ref into TextInput, 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

@vercel

vercel Bot commented Oct 25, 2022 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
console-ui-storybook ✅ Ready (Inspect) Visit Preview Oct 26, 2022 at 6:49PM (UTC)

}

export interface SideModalProps extends Omit<DialogProps, 'isOpen'>, ChildrenProp {
export type SideModalProps = {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the DialogProps noisy and we're not using them

export type EditSideModalFormProps<TFieldValues, Data> = SetRequired<
CreateSideModalFormProps<TFieldValues, Data>,
'defaultValues'
>

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare to the originals, which I always have a hard time wrapping my head around:

export type CreateFormProps<Values, Data> = Partial<
Pick<FormProps<Values>, 'id' | 'initialValues' | 'onSubmit'>
> & {
onSuccess?: (data: Data) => void
onError?: (err: ErrorResult) => void
onDismiss?: () => void
}
export type CreateSideModalFormProps<Values, Data> = CreateFormProps<Values, Data> &
Omit<SideModalProps, 'id'>
export type EditSideModalFormProps<Values, Data> = SetRequired<
CreateFormProps<Values, Data>,
'initialValues'
> &
CreateSideModalFormProps<Values, Data>

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree. I think I would prefer to see them composed at the call site.

title: ReactNode
onSubmit: SubmitHandler<TFieldValues>
form: UseFormReturn<TFieldValues>
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare to the original, which extends FormProps:

interface SideModalFormProps<Values> extends Omit<FormProps<Values>, 'setSubmitState'> {
isOpen: boolean
onDismiss: () => void
submitDisabled?: boolean
error?: Error
title: ReactNode
}

which itself extends FormikConfig:

export interface FormProps<Values> extends FormikConfig<Values> {
id: string
className?: string
children: ReactNode
/** true if submission can happen, false otherwise */
setSubmitState?: (state: boolean) => void
}

I never have any idea what the props are on these things!

id?: string
children: React.ReactNode
title: string
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used

@david-crespo

Copy link
Copy Markdown
Collaborator Author

love it

image

})}
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')}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forwarded ref in 523702f

Comment on lines +27 to +29
// 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}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you figured the casting issue out?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Got rid of the comment. name is a prop with type Path<TFieldValues> so it's good.

Comment thread app/forms/org-create.tsx Outdated
Comment on lines +48 to +66
// 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])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, exactly what I was thinking.

@david-crespo david-crespo Oct 26, 2022 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

https://github.com/oxidecomputer/console/pull/1259/files

Screen Shot 2022-10-26 at 1 34 31 PM

Comment on lines +38 to +41
export const TextInput = React.forwardRef<
HTMLInputElement,
TextInputBaseProps & TextAreaProps
>(({ type = 'text', error, className, fieldClassName, ...fieldProps }, ref) => (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, yeah. We'll likely need to do that to the other base input components too.

@just-be-dev just-be-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

* move useForm call inside SideModalForm, use render prop to pass control

* let SideModalForm take all the useForm props, not just defaultValues

This branch was successfully deployed

1 active deployment
Preview — 015329f9 Deployed Oct 26, 2022 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants