Skip to content

Formik PoC - #460

Merged
david-crespo merged 10 commits into
mainfrom
formik
Sep 10, 2021
Merged

david-crespo merged 10 commits into
mainfrom
formik

Conversation

@david-crespo

@david-crespo david-crespo commented Sep 3, 2021 •

Copy link
Copy Markdown
Collaborator

Note: because of indentation changes, the diff is much nicer with whitespace changes hidden.

What does Formik do

Formik is a library for reducing form-related boilerplate in React. As the docs helpfully summarize, Formik helps with

  1. Getting values in and out of form state
  2. Validation and error messages
  3. Handling form submission

Rather than explicitly managing a state variable for every field and passing in value and onChange, Formik uses React context internally to let each field manage its own state while also making the full form state available at the form level for validation and submission.

Adding Formik makes vendor.js go from 378 KiB to 410 KiB, which is not too bad, though we'll want to see how much value is added.

Feelings

Good

  • Not manually managing form state
    • Automatically keying field state to field names is great.
    • We had variables for each field and it was bad enough, but it was going to get much worse with validation. Formik automatically tracks value, error, and touched for each field
    • If we don't go with Formik, I still want something to help with this.
  • Field API is very nice, basically a fancy-fied input
    • Converting our existing components was almost trivial: TextField, Radio, and RadioCard were one-liners, simply replacing input with Field
    • Converting RadioGroup was slightly more invasive a change, but the result is a simplification because it no longer needs to handle value and onChange
  • I'm wary of adding complexity to the app by layering in outside APIs, but Formik is really minimal. After using it I feel like I have a good handle on everything it does, and it does exactly what you'd think a form library would do.
  • Using <ErrorMessage name="field_name"> to display field-level validation errors couldn't be easier

Bad

  • It makes Storybook gnarly
    • When you make TextField or Radio use Field instead of input, it's now a "connected" component that needs to be inside a <Formik> in order to work. In the app this is fine, because it is always going to be inside one (I think?) but it's a little annoying to have to do this in Storybook. This sets off my pure component spidey sense.
    • FormikDecorator is how we try to handle this relatively cleanly. It's tolerable but I don't like it very much
  • vendor.js goes from 378 KiB to 410 KiB. This is not much (and we avoid writing some state management code), so this is a very small con
  • TypeScript types are kind of weird even though Formik is written in TS (i.e., doesn't just have types tacked on)
    • They make it pretty hard to define a custom component that has roughly the same props as Field does

To do

  • Convert project create
  • Convert RadioGroup, Radio, and RadioCard
  • Convert instance create
  • Use Formik for form- and field-level validation in project create

Alternatives to Formik

More discussion to be added. For comparison, Formik has 28.2k stars.

  • Nothing/our own helpers
    • Even if we do this we'll want to take inspiration from these other APIs
  • React Hook Form (⭐ 22.9k)
    • At first I didn't like all the {...register('field_name')} business but after messing with Formik, I kind of like that you can use <form> and <input> and <label> directly
    • Only one open issue, gotta love that
    • Docs have a table comparing themselves to Formik but it seems kind of dubious and self-serving
  • React Final Form (⭐ 6.6k)
  • React Form (⭐ 2.2k)
    • Same dev as the wonderful React Query and React Table, but he says it's deprecated and people should use one of the above instead
  • Informed (⭐ 500)

@vercel

vercel Bot commented Sep 3, 2021 •

Copy link
Copy Markdown

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/oxidecomputer/console-ui-storybook/7WQAcKdwbgUqP5DJNgSBCTEajDCW
✅ Preview: https://console-ui-storybook-git-formik-oxidecomputer.vercel.app

@github-actions

github-actions Bot commented Sep 3, 2021

Copy link
Copy Markdown
Contributor

@david-crespo
david-crespo temporarily deployed to Preview VM September 3, 2021 15:40 Inactive
@david-crespo david-crespo changed the title Formik Formik PoC Sep 3, 2021
@david-crespo david-crespo mentioned this pull request Sep 3, 2021
17 tasks done
@just-be-dev

Copy link
Copy Markdown
Contributor

I stumbled across an interesting form library this weekend: https://uniforms.tools/. I'm not sure it'd be the right fit for our needs just based on sheer complexity, but it somewhat handles form generation/validation given a schema. The idea was interesting in that there would be a possibility of shared validation across client/server by using the OpenAPI generated artifacts.

Comment thread app/pages/ProjectCreatePage.tsx Outdated
placeholder="Enter name"
validate={validateName}
/>
<TextFieldError name="project_name" />

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.

can't complain about this API for errors

{...inputProps}
/>
{/* the dot in the middle. hide by default, use peer-checked to show if checked */}
<div className="hidden peer-checked:block absolute w-2 h-2 left-1 top-1 rounded-full bg-green-500" />

@david-crespo david-crespo Sep 9, 2021 •

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.

Switched to using peer-checked because we can't use the checked prop to decide whether to show the dot. Seems like when type="radio", Field is clever enough to set checked on its inner input when the value from the form matches its own value, essentially what we were doing before in RadioGroup with checked: radio.props.value === props.value.

Update: yep, but it's done at top level in the form state, not in Field.

checked: radio.props.value === props.value,
onChange: props.onChange,
required: props.required,
})

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.

kind of nice here. instead of passing checked and onChange down, we can let the child Radios, which contain Fields, pull their state from form context directly

@just-be-dev

Copy link
Copy Markdown
Contributor

Looks promising. I was curious at how they do error announcing (i.e. via aria-live or role=alert) but it looks like that's pushed to user space. https://formik.org/docs/examples/instant-feedback. I suppose that's preferable anyway given that it'll allow us more control over the experience.

I do like the simple error assignment/association and the over all straightforwardness of field definitions. I have no major issues given this PoC.

I think the only other option worth consideration is react-hook-form. Given it's hooks based the concern around defining custom components is non-existent. It has some helpful built-in field level validations that we'd just need to define via functions in formik. Nothing special needed for storybooks and it's a bit smaller. The downsides could be the slightly weird de-structuring API and error validation isn't quite as magical.

Ultimately it feels like a choice between two good options. I'm generally more in favor of hooks based APIs where possible to keep the overall component abstractions low, but I don't feel strongly about that in this case.

@david-crespo

Copy link
Copy Markdown
Collaborator Author

Thanks for checking it out. I also prefer hooks in theory, but don't see any real issues with Formik. Maybe in our case the hooks API could ease the Storybook ugliness, because we could keep our components dumb and then call them like this

<TextField {...register("project-name")} />

And then in the storybook we would display the dumb version and avoid the Formik wrapper pain. This wouldn't work for RadioGroup because it relies on Formik to pass the current selected value. One other possible advantage of RHF is that the TypeScript types might be less janky. I'll give it a very brief test, but my impression is that it wouldn't be that different from Formik.

@david-crespo
david-crespo marked this pull request as ready for review September 10, 2021 15:40

@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.

We discussed react-hook-form a bit more in chat but ultimately decided Formik sufficiently addresses our needs. I'm good to merge whenever you're ready. 👍

@david-crespo
david-crespo temporarily deployed to Preview VM September 10, 2021 16:07 Inactive

This branch was successfully deployed

1 active and 1 inactive deployments
Preview VM — 414b7df7 Deployed Sep 10, 2021 by david-crespo
Preview — 414b7df7 Deployed Sep 10, 2021 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