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
createFormHookthat 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 theEncodedwill show up the field as string which is not true, so I'd rather separate these things for type safety.Decodedtype. 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.useAppFormdefaultValuesvalidationLogicandvalidators.onDynamicto the schemaonSubmitwithonSubmitWithTransitionthat gives you theDecodedand 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.withFieldGroupkeyof Encodedto get the nested object out of the schema - the group.defaultValuesStandardSchemaV1Issue[]inrenderfor the errors related to the group object.groupthat is provided inrenderis actually typed now. I provided generics and returned as-is muting typescript errors. Your typing has a bunch ofunknownthere for some reason forAppFieldExtendedReactFieldGroupApi(but maybe I should be usingAppFieldExtendedReactFormApiinstead? anyway, it works, idk). I think without my wrapper the defaultwithFieldGroupreturnsanyfor 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