Build forms in React, without the tears 😭
APACHE-2.0 License
Bot releases are hidden (Show)
#396 Modifies behavior of FieldArray's props.pop()
and FieldArray props.remove()
to remove relevant nested partials of from touched
and errors
objects in addition to values
so that validation works as expected with array manipulations.
Thank you @a-tarasyuk @davidhenley @rovansteen @timc13
<Field name="social.facebook" />
and <Field name="friends[1]" />
<Field validate={value => ...} />
onReset
prop for <Formik />
.<FieldArray>
componentimport React from 'react';
import { Formik, Form, Field, FieldArray } from 'formik'
export const FriendList = () => (
<div>
<h1>Friend List</h1>
<Formik
initialValues={{ friends: ['jared', 'ian', 'brent'] }}
onSubmit={...}
render={formikProps => (
<FieldArray
name="friends"
render={({ move, swap, push, insert, unshift, pop }) => (
<Form>
{/*... use these however you want */}
</Form>
)}
/>
/>
</div>
);
dirty
(and thus isValid
) has changed due to popular demand/consensus.
Old meaning: "has any field been touched?"
New meaning: "has any field value changed?"
handleChangeValue
has been removed entirely. It's been deprecated since 0.8
next
branch of how to do a multi-step form wizard.dirty
(and thus isValid
) has changed due to popular demand/consensus.Old meaning: "has any field been touched?"
New meaning: "has any field value changed?"
onReset: (values: Values, bag: FormikBag<Values>)
config / prop that runs prior to a resetFormunsafe_setFormikState: (whateveryouwant: any) => void
.submitCount
: Will track number of submit attempts<Field>
that does not rerendermeta
property on <Field>
.validateForm
, isValidating
Published by jaredpalmer almost 7 years ago
#290 Fix missing argument in Field's setFieldError
Published by jaredpalmer almost 7 years ago
<Field>
so that <Field component="select"><option /><option /></Field>
worksPublished by jaredpalmer almost 7 years ago
Fully backwards compatible. No breaking changes.
<Field>
now also accepts a render prop.
<Field name="firstName" placeholder="First Name" />
<Field name="firstName" placeholder="First Name" component={MyInput} />
<Field name="firstName" placeholder="First Name" /> // same as before
<Field name="firstName" placeholder="First Name" component={MyInput} /> // same as before
<Field
name="firstName"
render={({field, form}) => /* note: props to Field not passed thru, cuz they are available already */
<div>
<input {...field} placeholder="firstName" />
{/* whatever */}
</div>
}
/>
<Field name="firstName">
{({field, form}) =>
<div>
<input {...field} placeholder="firstName" />
{/* whatever */}
</div>}
</Field>
import * as React from 'react';
import { Formik, FormikProps, Form, Field, FieldProps } from 'formik';
interface MyFormValues {
firstName: string;
}
export const MyApp: React.SFC<{}> = () => {
return (
<div>
<h1>My Example</h1>
<Formik
initialValues={{ firstName: '' }}
onSubmit={(values: MyFormValues) => alert(JSON.stringify(values))}
render={(_formikBag: FormikProps<MyFormValues>) =>
<Form>
<Field
name="firstName"
render={({ field, form }: FieldProps<MyFormValues>) =>
<div>
<input type="text" {...field} placeholder="First Name" />
{form.touched.firstName &&
form.errors.firstName &&
form.errors.firstName}
</div>}
/>
</Form>}
/>
</div>
);
};
import * as React from 'react';
import * as cx from 'classnames';
import { Field, FieldConfig, FieldProps } from 'formik';
export interface FieldsetProps {
/** blah */
}
// This wraps `<Field render>` with our error/touched logic, display, and formatting.
export const Fieldset: React.SFC<FieldsetProps & FieldConfig> = ({ name, render, ...rest }) =>
<Field
name={name}
render={({ field, form }: FieldProps<MyFormValues>) =>
<div
className={cx('fieldset', {
'fieldset--error': form.errors[name] && form.touched[name],
})}
>
{render({ field, form })}
{form.touched.firstName &&
form.errors.firstName &&
form.errors.firstName}
</div>
}
{...rest}
/>;
import * as React from 'react';
import { Formik, FormikProps, Form, Field, FieldProps } from 'formik';
export interface MyAppProps {}
interface MyFormValues {
firstName: string;
}
export const MyApp: React.SFC<MyAppProps> = () => {
return (
<div>
<h1>My Example</h1>
<Formik
initialValues={{ firstName: '' }}
onSubmit={(values: MyFormValues) => alert(JSON.stringify(values))}
render={({
_formikBag /* values, errors, handleChange ... */,
}: FormikProps<MyFormValues>) =>
<Form>
<Fieldset
name="firstName"
render={({ field }: FieldProps<MyFormValues>) =>
/** Stay DRY, just deal with the input, since errors handled in Fieldset */
<input type="text" {...field} />}
/>
</Form>}
/>
</div>
);
};
Note: In order to maintain backwards compat, the order or precedence among the render functions is DIFFERENT than <Formik />
's.
<Field>
: render
> children
> component
<Formik>
: component
> render
> children
This is because <Field/>
defaults to component="input"
initialValues
change. I added this feature thinking it would be desirable on most apps. However, it appears that I was wrong and it created headaches for people using Redux to get their initial values. Sorry about that. That's on me. If you still want this behavior, pass <Formik enableReinitialize={true}
/> or withFormik({ enableReinitialize: true })(...)
. \If you want to have more control, initialValues
(i.e. initialValues
as of first mount) is now available in props
, so you can use that to control reset behavior with more granularity if you want to.
<Field/>
(i.e. one's without custom component's) using shouldComponentUpdate
. I have not yet released this because of I want to wait on #182 and #167 . Those on the bleeding edge can copy and paste the code from #185 .<Formik />
component<Field />
and <Form />
helpersmapValuesToPayload
has been removedvalidateOnChange
now defaults to true
validateOnBlur
now defaults to false
Formik()
has been renamed withFormik()
. (The named Formik
import is now used for the component and not for the HoC)The Formik()
HoC function in 0.8.x has been renamed withFormik()
in 0.9.0. Since withFormik()
is fully backwards compatible (in both plain JavaScript and TypeScript), you can safely do a search and replace and things will just work.
import { Formik } from 'formik'
const enhancer = Formik({ ... config ... })
...
import { withFormik } from 'formik'
const enhancer = withFormik({ ... config ... })
...
Published by jaredpalmer about 7 years ago
handleSubmit
of withFormik
Begin internal migration to 0.9.0 @PalmerHQ ...