Question: Is it possible to create reusable form sections?
See original GitHub issueQuestion
Is it possible to create reusable form sections w/ Formik? Or nest Formik components?
In redux-form
, there is the concept of a FormSection.
For example, something like:
class MyFormSection extends Component {
return (
<Formik
initialValues={{
a: '',
b: ''
}}
validationSchema={() => yup.object()
.shape({
a: yup.string(),
b: yup.string()
})
}
render={({
values,
errors,
touched,
handleChange,
handleBlur
}) => (
<fieldset>
<input name="a" onChange={handleChange} onBlur={handleBlur} value={values.a} />
{touched.a && errors.a && <div>{errors.a}</div>}
<input name="b" onChange={handleChange} onBlur={handleBlur} value={values.b} />
{touched.b && errors.b && <div>{errors.b}</div>}
</fieldset>
)}
/>
)
}
class MyEntireForm extends Component {
return (
<Formik
initialValues={{
c: ''
}}
validationSchema={() => yup.object()
.shape({
c: yup.string()
})
}
onSubmit={(values) => {
console.log(values) // { c: '' }
}}
render={({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit
}) => (
<form onSubmit={handleSubmit}>
<MyFormSection />
<input name="c" onChange={handleChange} onBlur={handleBlur} value={values.c} />
{touched.c && errors.c && <div>{errors.c}</div>}
<button type="submit">Submit</button>
</form>
)}
/>
)
}
I started going down this route, but I’m unsure how MyFormSection
would know how to be submitted or how MyEntireForm
would receive the submitted values from MyFormSection
.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:11 (3 by maintainers)
Top Results From Across the Web
How to create reusable form components with React Hook ...
The first step is to create an input component. Creating an isolated component can be a good way to provide consumers of your...
Read more >vue.js - How to create reusable form and input components in ...
You should create empty form with button (Cancel/Submit e.t.c) and add events for it. Then you should just pass inputs to slot and...
Read more >Demonstrating Reusable React Components in a Form
To make this input element reusable in other places and projects, we'll have to extract it into its own component. Let's call it...
Read more >Reducing the forms boilerplate — make your Angular forms ...
Reduce the forms boilerplate by creating reusable custom forms controls with the ControlValueAccessor interface. Learn how to create reusable forms, ...
Read more >How to Make Fillable Forms Reusable in Word - YouTube
HOW TO MAKE FILLABLE FORMS REUSABLE IN WORD // For forms that need to be reused frequently, a best practice is to save...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Had to use quasi-inheritance rather than composition. Isn’t as flexible as self-validating fieldsets but works.
For any future Internet travelers, this was my solution:
The difficulty right now is in being able to reuse validation logic in fieldsets which appear in multiple forms (but each form has additional other fields).
Formik
appears to have the requirement that there needs to be<form onSubmit={handleSubmit}>
somewhere inside itsrender
prop which means you can’t properly render aFormik
component inside anotherFormik
component because you can’t nest forms.What would be neat to have is something like a Formik
Fieldset
, essentially aFormik
component that doesn’t need a<form>
inside itsrender
prop but expects to be used in aFormik
component that does:where
FieldsetOne
andFieldsetTwo
can be moved around, added / removed as needed, used in other Formik forms, and theFormik
component combines theirinitialValues
andvalidationSchema
with its own, andonSubmit
combines all the sections’ values together.I don’t know if this is even possible, but my mental model thinks that this could be useful. Perhaps it could be achieved another way?
Thoughts?
+1