JSPM

formik

0.9.0-alpha.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3166474
  • Score
    100M100P100Q186496F
  • License MIT

Forms in React, without tears

Package Exports

  • formik

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (formik) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Formik Next

Experimental features. There will be tears. If you are not Jared or Ian, use at your own risk.

gzip size Build Status npm license Join the chat at on Slack

Let's face it, forms are really verbose in React. To make matters worse, most form helpers do wayyyy too much magic and often have a significant performance cost associated with them. Formik is a minimal Higher Order Component that helps you with the 3 most annoying parts:

  1. Transforming props to form state
  2. Validation and error messages
  3. Handling form submission

By colocating all of the above in one place, Formik will keep things organized--making testing, refactoring, and reasoning about your forms a breeze.

Installation

Add Formik (and optionally Yup to your project). Formik supports/recommends Yup (which is like Joi, but for the browser) for object schema validation.

npm i formik@next yup --save

Note: Yup is 100% optional. You are free to [write your own validators][validate].

You can also try before you buy with this demo of Formik on CodeSandbox.io

Table of Contents

<Formik />

Formik is now a component that uses render props!

API change from master: mapValuesToProps doesn't exist. Just pass an object to getInitialValues instead.

Aside from that, <Formik /> = Formik() except with a lot less ceremony...

import { Formik } from 'formik/next'

interface Values {
  name: string
}
 
const BasicExample: React.SFC<...> = () => 
  <div>
    <h1>My Form</h1>
    <Formik
      getInitialValues={{ name: 'jared' }}
      handleSubmit={(values: Values) => {
        setTimeout(() => alert(JSON.stringify(values, null, 2)), 1000);
      }}
      render={(props: FormComponentProps<Values>) =>
        <form onSubmit={props.handleSubmit}>
          <input
            type="text"
            onChange={props.handleChange}
            onBlur={props.handleBlur}
            value={props.values.name}
            name="name"
          />
          {props.errors.name &&
            <div id="feedback">
              {props.errors.name}
            </div>}
          <button type="submit">Submit</button>
        </form>}
    />
  </div>;

You can avoid render callbacks all together too... #perf

import { Formik } from 'formik/next'

interface Values {
  name: string
}

class BasicClassExample extends React.Component<any, any> {
  handleSubmit = (values: Values) => {
    setTimeout(() => alert(JSON.stringify(values, null, 2)), 1000);      
  }

  render() {
    return (
      <div>
        <h1>My Form</h1>
        <Formik
          getInitialValues={{ name: 'jared' }}
          handleSubmit={handleSubmit}
          component={ContactForm}
        />
      </div>
    );
  }
}

const ContactForm: React.SFC<FormComponentProps<Values>> = ({ 
  handleSubmit, 
  handleChange, 
  handleBlur, 
  values, 
  errors 
}) => {
  return
    <form onSubmit={props.handleSubmit}>
      <input
        type="text"
        onChange={props.handleChange}
        onBlur={props.handleBlur}
        value={props.values.name}
        name="name"
      />
      {props.errors.name &&
        <div>
          {props.errors.name}
        </div>}
      <button type="submit">Submit</button>
  </form>
}

Formik Render Methods

There are now three ways to render things with Formik

  • <Formik component>
  • <Formik render>
  • <Formik children>

Formik props

All three render methods will be passed the same three route props:

  • dirty
  • errors
  • handleBlur
  • handleChange
  • handleReset
  • handleSubmit
  • isSubmitting
  • isValid
  • resetForm
  • setErrors
  • setFieldError
  • setFieldTouched
  • setFieldValue
  • setStatus
  • setSubmitting
  • setTouched
  • setValues
  • status
  • touched
  • values

component

<Formik component={ContactForm} />

const ContactForm = ({ handleSubmit, handleChange, handleBlur, values, errors }) => {
  return
    <form onSubmit={props.handleSubmit}>
      <input
        type="text"
        onChange={props.handleChange}
        onBlur={props.handleBlur}
        value={props.values.name}
        name="name"
      />
      {props.errors.name &&
        <div>
          {props.errors.name}
        </div>}
      <button type="submit">Submit</button>
  </form>
}

Warning: <Formik component> takes precendence over <Formik render> so don’t use both in the same <Formik>.

render: (props: FormComponentProps<Values>) => ReactNode

<Formik render={props => <ContactForm {...props} />}/>

<Formik 
  render={({ handleSubmit, handleChange, handleBlur, values, errors }) => ( 
    <form onSubmit={props.handleSubmit}>
      <input
        type="text"
        onChange={props.handleChange}
        onBlur={props.handleBlur}
        value={props.values.name}
        name="name"
      />
      {props.errors.name &&
        <div>
          {props.errors.name}
        </div>}
      <button type="submit">Submit</button>
    </form>
  )} 
/>

children: func

<Formik children={props => <ContactForm {...props} />}/>

// or...

<Formik>
  {({ handleSubmit, handleChange, handleBlur, values, errors }) => ( 
    <form onSubmit={props.handleSubmit}>
      <input
        type="text"
        onChange={props.handleChange}
        onBlur={props.handleBlur}
        value={props.values.name}
        name="name"
      />
      {props.errors.name &&
        <div>
          {props.errors.name}
        </div>}
      <button type="submit">Submit</button>
    </form>
  )} 
</Formik>

<Field />

(Much Experimental. Very magic )

<Field /> will automagically hook up inputs to Formik. It uses the name attribute to match up with Formik state. <Field/> will default to and <input/> element. To change the underlying element of <Field/>, specify a component prop. It can either be a string like select or another React component.

import * as React from 'react';
import { Formik, Field,  FormComponentProps  } from 'formik/next';

interface Values {
  email: string;
  color: string;
  firstName: string;
}
 
const Example: React.SFC<...> = () => (
  <div>
    <h1>My Form</h1>
    <Formik
      getInitialValues={{ email: '', color: 'red', firstName: ''  }}
      handleSubmit={(values: Values) => {
        setTimeout(() => alert(JSON.stringify(values, null, 2)), 1000);
      }}
      render={(props: FormComponentProps<Values>) =>
        <form onSubmit={props.handleSubmit}>
          <Field type="email" name="email" placeholder="Email" />
          <Field component="select" name="color" >
            <option value="red">Red</option>
            <option value="green">Green</option>
            <option value="blue">Blue</option>
          </Field>
          <Field component={CustomInputComponent} name="firstName" />
          <button type="submit">Submit</button>
        </form>}
    />
  </div>
);

const CustomInputComponent: React.SFC<FormComponentProps<Values> & CustomInputProps> => ({
  name,
  placeholder,
  values,
  errors,
  handleBlur,
  handleChange,
  ...props
}) => (
  <div>
    <input 
      className="custom" 
      type="text"
      value={values[name]} 
      placeholder={placeholder}
      onChange={handleChange}
      onBlur={handlerBlur}
    />
    {touched[name] && errors[name] && <div className="error">{errors[name]}</div>}
  </div>
)

FormikFactory(options)

not implemented yet

import { Formik, InjectedFormikProps } from 'formik/next';

// backwards compatible API 

const withFormik = FormikFactory<InjectedFormikProps<Props, Values>>({ ... })

const Form = props => (
  <form onSubmit={props.handleSubmit}>
    {/* same as usual */}
  </form>
)

export default withFormik(Form)

Authors

MIT License.