Schema requires too much boilerplate, here's my wrapper for createFormHook #1695
Unanswered
Sam-Kruglov
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I don't have time to submit a PR right now but I made a generic wrapper over
createFormHook
that integrates Effect/Schema and it works great for me. I made sure to satisfy all linting (and I configured it to the max with opinionated tsconfig flags, tseslint.strictTypeChecked|stylisticTypeChecked). It does need some unit tests though which I don't have. At some point I'm hoping you'll take it or I'll finally submit a PR myself (I very doubt I'd ever get to it).Usage
Improvements Made
<input value={typeof field.state.value === 'number' && Number.isNaN(field.state.value) ? '' : field.state.value}
itself. Otherwise theEncoded
will show up the field as string which is not true, so I'd rather separate these things for type safety.Decoded
type. Since I'm making a form, I'm about to submit my change and I already have the type for the change (generated graphql mutation type in my case), so might as well get auto-complete for it when filling out the schema.useAppForm
defaultValues
validationLogic
andvalidators.onDynamic
to the schemaonSubmit
withonSubmitWithTransition
that gives you theDecoded
and wraps your code in astartTransition
. I always do this, so I though might as well wrap it. Idk how applicable the transition part is but the decoded is the main piece here.withFieldGroup
keyof Encoded
to get the nested object out of the schema - the group.defaultValues
StandardSchemaV1Issue[]
inrender
for the errors related to the group object.group
that is provided inrender
is actually typed now. I provided generics and returned as-is muting typescript errors. Your typing has a bunch ofunknown
there for some reason forAppFieldExtendedReactFieldGroupApi
(but maybe I should be usingAppFieldExtendedReactFormApi
instead? anyway, it works, idk). I think without my wrapper the defaultwithFieldGroup
returnsany
for me when I try to access state. Tbh I forgot what the typing problem was but you can try it.@/components/ui/form.tsx
'@/components/ui/form-schema.ts'
I really hope y'all at least get some inspiration from it and remove the boilerplate in any way you see fit. Please also pay attention to the comments, there's some extra feedback.
Beta Was this translation helpful? Give feedback.
All reactions