Repository navigation
Formik PoC - #460
Formik PoC#460
Conversation
|
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/oxidecomputer/console-ui-storybook/7WQAcKdwbgUqP5DJNgSBCTEajDCW |
|
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. |
| placeholder="Enter name" | ||
| validate={validateName} | ||
| /> | ||
| <TextFieldError name="project_name" /> |
There was a problem hiding this comment.
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" /> |
There was a problem hiding this comment.
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, | ||
| }) |
There was a problem hiding this comment.
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
|
Looks promising. I was curious at how they do error announcing (i.e. via 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. |
|
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 And then in the storybook we would display the dumb version and avoid the Formik wrapper pain. This wouldn't work for |
63f1989 to
414b7df
Compare
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
Rather than explicitly managing a state variable for every field and passing in
valueandonChange, 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.jsgo from 378 KiB to 410 KiB, which is not too bad, though we'll want to see how much value is added.Feelings
Good
FieldAPI is very nice, basically a fancy-fiedinputTextField,Radio, andRadioCardwere one-liners, simply replacinginputwithFieldRadioGroupwas slightly more invasive a change, but the result is a simplification because it no longer needs to handlevalueandonChange<ErrorMessage name="field_name">to display field-level validation errors couldn't be easierBad
TextFieldorRadiouseFieldinstead ofinput, 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.FormikDecoratoris how we try to handle this relatively cleanly. It's tolerable but I don't like it very muchvendor.jsgoes from 378 KiB to 410 KiB. This is not much (and we avoid writing some state management code), so this is a very small conFielddoesTo do
RadioGroup,Radio, andRadioCardAlternatives to Formik
More discussion to be added. For comparison, Formik has 28.2k stars.
{...register('field_name')}business but after messing with Formik, I kind of like that you can use<form>and<input>and<label>directly