2.0.0-alpha-3 • Published 7 years ago

react-formik-ui v2.0.0-alpha-3

Weekly downloads
1,023
License
MIT
Repository
github
Last release
7 years ago

NPM JavaScript Style Guide license

Overview

React-Formik-UI is a simple component library, composed out of pure HTML form elements like: form, input, select, checkbox, radio and textarea.

The Idea behind React-Formik-UI is to make the composition of forms with React and Formik even easier, so you don't have to write any HTML markup or extra components for your forms.

Each component makes use of Formiks context, there for you need to have Formik installed in your project.

Markup, Styling and ClassNames

The markup for the components Input, Select, Checkbox, Radio, Textarea, Datepicker and DropZone consists of a wrapper div, label, the main component, hint, and error message.

By default all component have NO styling applied att all. This is intentionally, so you have the posibility to apply your own styling. All the components used in the form are scoped by the default classes, situated on the Form component, react-formik-ui form-wrapper

Each component has its corresponding wrapper class (eg: Input component input-wrapper ), as well as the class form-element. you also can pass your own custom class to the wrapper of each component bay passing the className prop.

Anyhow, if you pass the styled prop to the Form component, minimal styling will be applied to add some structure to the form and each form element.

Installation

npm install --save react-formik-ui

Usage

Peer Dependency

React-Formik-UI has a Peer dependency of Formik. This means that you need to add Formik to your project to make use of React-Formik-UI.

npm install --save formik@latest

Form validations

To validate the form fields, the use of Yup is recommended.

npm install --save yup

Components and Examples

See the Styleguide here

Form

The Form component, like in a normal HTML form is the main wrapper for your form. It renders with the classNames react-formik-ui and form-wrapper. A custom class can be passed with the className prop.

You don't need to pass an onSubmit handler, since this is already handled under the hood.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import { Form } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  render () {
    return (
      <Formik
        initialValues={ /* inital values setup */ }
        validationSchema={ /* validation schema setup */ }
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

          </Form>
        )}
      />
    )
  }
}

Input

The Input component renders with the classNames form-element and input-wrapper. A custom class can be passed through the className prop.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import * as yup from 'yup';

import { Form, Input, SubmitBtn } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  // example of validation with yup
  getSchema = () => {
    return yup.object().shape({
      nameField: yup
        .string()
        .required('Name Is required'),
    })
  }

  render () {
    return (
      <Formik
        initialValues={{
          nameField: ''
        }}
        validationSchema={this.getSchema}
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

            <Input
              name='nameField'
              label='Input field main label'
              placeholder='Your Name'
              hint='This is a hint'
              required
            />

            <SubmitBtn />
          </Form>
        )}
      />
    )
  }
}

Select

The Select component renders with the classNames form-element and select-wrapper. A custom class can be passed through the className prop.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import * as yup from 'yup';

import { Form, Select, SubmitBtn } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  // example of validation with yup
  getSchema = () => {
    return yup.object().shape({
      dropdown: yup
        .string(),
    })
  }

  render () {
    return (
      <Formik
        initialValues={{
          dropdown: ''
        }}
        validationSchema={this.getSchema}
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

            <Select
              name='dropdown'
              label='Select options main label'
              placeholder='Select an Option'
              options={[
                { value: '1', label: 'Option 1' },
                { value: '2', label: 'Option 2' },
                { value: '3', label: 'Option 3' }
              ]}
            />

            <SubmitBtn />
          </Form>
        )}
      />
    )
  }
}

Radio

The Radio component renders with the classNames form-element and radio-wrapper. A custom class can be passed through the className prop.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import * as yup from 'yup';

import { Form, Radio, SubmitBtn } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  // example of validation with yup
  getSchema = () => {
    return yup.object().shape({
      radioOptions: yup
        .string(),
    })
  }

  render () {
    return (
      <Formik
        initialValues={{
          radioOptions: ''
        }}
        validationSchema={this.getSchema}
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

            <Radio
              name='radioOptions'
              label='Radio options main label'
              options={[
                { value: '1', label: 'Option 1' },
                { value: '2', label: 'Option 2' },
                { value: '3', label: 'Option 3' }
              ]}
            />

            <SubmitBtn />
          </Form>
        )}
      />
    )
  }
}

Checkbox

The Checkbox component renders with the classNames form-element and checkbox-wrapper. A custom class can be passed through the className prop.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import * as yup from 'yup';

import { Form, Checkbox, SubmitBtn } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  // example of validation with yup
  getSchema = () => {
    return yup.object().shape({
      agreement: yup
        .boolean(),
    })
  }

  render () {
    return (
      <Formik
        initialValues={{
          agreement: false
        }}
        validationSchema={this.getSchema}
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

            <Checkbox
              name='agreement'
              label='Checkbox main label'
              text='Lorem ipsum dolor sit amet, consetetur sadipscing elitr.'
            />

            <SubmitBtn />
          </Form>
        )}
      />
    )
  }
}

Textarea

The Textarea component renders with the classNames form-element and textarea-wrapper. A custom class can be passed through the className prop.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import * as yup from 'yup';

import { Form, Textarea, SubmitBtn } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  // example of validation with yup
  getSchema = () => {
    return yup.object().shape({
      comment: yup
        .string(),
    })
  }

  render () {
    return (
      <Formik
        initialValues={{
          comment: ''
        }}
        validationSchema={this.getSchema}
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

            <Textarea
              name='comment'
              label='Write a comment'
            />

            <SubmitBtn />
          </Form>
        )}
      />
    )
  }
}

Datepicker

The Datepicker component uses ReactJS Datepicker under the hood. It renders with the classNames form-element and datePicker-wrapper. A custom class can be passed through the className prop.

For aditional configuration options and layouts, please refere to ReactJS Datepicker. You can then pass the desired configuration as props just like you would on ReactJS Datepicker.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import * as yup from 'yup';

import { Form, Datepicker, SubmitBtn } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  // example of validation with yup
  getSchema = () => {
    return yup.object().shape({
      birthday: yup
        .date(),
    })
  }

  render () {
    return (
      <Formik
        initialValues={{
          birthday: ''
        }}
        validationSchema={this.getSchema}
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

            <Datepicker
              name='birthDay'
              label='Birthdate'
            />

            <SubmitBtn />
          </Form>
        )}
      />
    )
  }
}

DropZone

The DropZone component uses react-dropzone under the hood. It renders with the classNames form-element and dropzone-wrapper. A custom class can be passed through the className prop.

For aditional configuration options and layouts, please refere to react-dropzone.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import * as yup from 'yup';

import { Form, DropZone, SubmitBtn } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  // example of validation with yup
  getSchema = () => {
    return yup.object().shape({
      files: yup
        .array()
    })
  }

  render () {
    return (
      <Formik
        initialValues={{
          files: []
        }}
        validationSchema={this.getSchema}
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

              <DropZone
                name='files'
                label='Image upload'
              />

            <SubmitBtn />
          </Form>
        )}
      />
    )
  }
}

Button

The Button component renders with the className btn. A custom class can be passed through the className prop.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import * as yup from 'yup';

import { Form, Button } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  render () {
    return (
      <Formik
        initialValues={ /* inital values setup */ }
        validationSchema={ /* validation schema setup */ }
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

            <Button onClick={(() => alert('Cancel'))}>
              Cancel
            </Button>

          </Form>
        )}
      />
    )
  }
}

Toggle

The Toggle button component, is the only component so far who has its own styling. Since it uses the Button component, it renders with the classNames btn and also toggle-btn. A custom class can be passed through the className prop.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import * as yup from 'yup';

import { Form, Toggle, SubmitBtn } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  // example of validation with yup
  getSchema = () => {
    return yup.object().shape({
      toggleBtn: yup
        .boolean(),
    })
  }

  render () {
    return (
      <Formik
        initialValues={{
          toggleBtn: false
        }}
        validationSchema={this.getSchema}
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

            <Toggle name='toggleBtn'/>

            <SubmitBtn />
          </Form>
        )}
      />
    )
  }
}

SubmitBtn

The SubmitBtn component renders with the classNames btn and submit-btn. A custom class can be passed through the className prop.

By default the SubmitBtn handles the submition, no further handler or configuration is needed.

Props:

Code example:

import React, { Component } from 'react'
import { Formik } from 'formik'
import * as yup from 'yup';

import { Form, SubmitBtn } from 'react-formik-ui'

class Example extends Component {

  onSubmit = data => {
    // here you hanlde the data to be submited
  }

  render () {
    return (
      <Formik
        initialValues={ /* inital values setup */ }
        validationSchema={ /* validation schema setup */ }
        onSubmit={this.onSubmit}
        render={() => (
          <Form styled>

            <SubmitBtn />

          </Form>
        )}
      />
    )
  }
}

Complete Form Example

import React, { Component } from 'react';
import { Formik } from 'formik'
import *  as yup from 'yup'
import {
  Form,
  Input,
  Datepicker,
  Select,
  Checkbox,
  Radio,
  Textarea,
  Button,
  SubmitBtn,
  Toggle,
  DropZone,
} from './components'
import logo from './logo.svg';
import './App.css';

class ExampleForm extends Component {
  onSubmit = data => {
    console.log(data)
  }

  // example of validation with yup
  getSchema = () => {
    return yup.object().shape({
      salutation: yup
        .string(),
      firstName: yup
        .string()
        .required('First name is required'),
      lastName: yup
        .string()
        .required('Last name is required'),
      email: yup
        .string()
        .email('Wrong format')
        .required('Email is required'),
      birthDay: yup
        .date(),
      maritalStatus: yup
        .string()
        .nullable(),
      driverLicense: yup
        .boolean(),
      pets: yup
        .boolean(),
      income: yup
        .string()
        .required('Income is required'),
      files: yup
        .array()
        .required('Image is required'),
      additionalInfo: yup
        .string(),
      termsAndConitions: yup
        .boolean(),
    });
  }

  render() {
    const styledDiv = {
      display: 'flex',
      justifyContent: 'space-between',
      alignItems: 'center',
      marginBottom: '15px'
    }

    return (
      <Formik
        initialValues={{
          salutation: 'Mr',
          firstName: '',
          lastName: '',
          email: '',
          birthDay: '',
          maritalStatus: '',
          driverLicense: false,
          pets: false,
          income: '',
          files: [],
          additionalInfo: '',
          termsAndConitions: false,
        }}
        validationSchema={this.getSchema}
        onSubmit={this.onSubmit}
        render={({ values }) => {
          return (
            <Form styled>
              <fieldset>
                <legend>Form Example:</legend>

                <Radio
                  name='salutation'
                  label='Salutation'
                  options={[
                    { value: 'Mr', label: 'Mr.' },
                    { value: 'Mrs', label: 'Mrs.' },
                    { value: 'Ms', label: 'Ms.' }
                  ]}
                />

                <Input
                  name='firstName'
                  label='First name'
                  required
                />

                <Input
                  name='lastName'
                  label='Last name'
                  required
                />

                <Input
                  name='email'
                  label='Enter your Email'
                  required
                />

                <Datepicker
                  name='birthDay'
                  label='Birthday'
                  dateFormat='D.M.YYYY'
                  placeholder='D.M.YYYY'
                  hint='Please enter your birth date'
                />

                <Select
                  name='maritalStatus'
                  label='Marital Status'
                  placeholder='Select an Option'
                  options={[
                    { value: '1', label: 'Married' },
                    { value: '2', label: 'Single' },
                    { value: '3', label: 'Divorced' },
                    { value: '4', label: 'Widowed' }
                  ]}
                />

                <div style={styledDiv}>
                  <div>
                    {`Do you have a drivers license ? ${values.driverLicense ? 'Yes' : 'No'}`}
                  </div>
                  <Toggle name='driverLicense' />
                </div>

                <div style={styledDiv}>
                  <div>
                    {`Do you own pets ? ${values.pets ? 'Yes' : 'No'}`}
                  </div>
                  <Toggle name='pets' />
                </div>

                <Input
                  name='income'
                  label={`What is your monthly income $${values.income}`}
                  type='range'
                  min='0'
                  max='10000'
                  step='500'
                  required
                />

                <DropZone
                  name='files'
                  label='File upload'
                />

                <Textarea
                  name='additionalInfo'
                  label='Aditional information'
                  hint='this is optional'
                />

                <Checkbox
                  name='termsAndConitions'
                  label='Terms and Conditions'
                  text='Click to accept the terms and conditions'
                />

                <SubmitBtn disabled={!values.termsAndConitions} />
                <Button onClick={(() => alert('Cancel'))}>Cancel</Button>
              </fieldset>
            </Form>
          );
        }}
      />
    );
  }
}

export default ExampleForm;

License

MIT © KaiHotz

5.8.0

2 years ago

5.7.4

3 years ago

5.7.3

3 years ago

5.7.2

3 years ago

5.7.1

3 years ago

5.7.0

4 years ago

5.5.4

4 years ago

5.6.1

4 years ago

5.6.0

4 years ago

5.5.3

4 years ago

5.5.2

4 years ago

5.5.1

4 years ago

5.5.0

4 years ago

5.4.5

4 years ago

5.4.4

4 years ago

5.4.3

4 years ago

5.4.2

4 years ago

5.4.1

4 years ago

5.4.0

4 years ago

5.3.8

4 years ago

5.3.7

4 years ago

5.3.3

4 years ago

5.3.2

4 years ago

5.3.6

4 years ago

5.3.5

4 years ago

5.3.4

4 years ago

5.3.1

4 years ago

5.3.0

4 years ago

5.2.3

4 years ago

5.2.2

4 years ago

5.2.1

4 years ago

5.2.0

4 years ago

5.1.11

5 years ago

5.1.10

5 years ago

5.1.9

5 years ago

5.1.8

5 years ago

5.1.7

5 years ago

5.1.6

5 years ago

5.1.5

5 years ago

5.1.4

5 years ago

5.1.3

5 years ago

5.1.2

5 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.2

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.1.2

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.8

6 years ago

4.0.7

6 years ago

4.0.6

6 years ago

4.0.5

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.1.4

6 years ago

3.1.3

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.20

6 years ago

3.0.19

6 years ago

3.0.18

6 years ago

3.0.17

6 years ago

3.0.16

6 years ago

3.0.15

6 years ago

3.0.14

6 years ago

3.0.13

6 years ago

3.0.12

6 years ago

3.0.11

6 years ago

3.0.10

6 years ago

3.0.9

6 years ago

3.0.8

7 years ago

3.0.7

7 years ago

3.0.6

7 years ago

3.0.5

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.3.17

7 years ago

2.3.16

7 years ago

2.3.15

7 years ago

2.3.14

7 years ago

2.3.13

7 years ago

2.3.12

7 years ago

2.3.11

7 years ago

2.3.10

7 years ago

2.3.9

7 years ago

2.3.8

7 years ago

2.3.7

7 years ago

2.3.6

7 years ago

2.3.5

7 years ago

2.3.4

7 years ago

2.3.3

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.5

7 years ago

2.2.4

7 years ago

2.2.3

7 years ago

2.2.2

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.4

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

1.2.5

7 years ago

2.0.0

7 years ago

2.0.0-rc

7 years ago

1.2.4

7 years ago

2.0.0-beta-1

7 years ago

2.0.0-alpha-4

7 years ago

2.0.0-alpha-3

7 years ago

2.0.0-alpha-2

7 years ago

2.0.0-alpha-1

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

1.1.16

7 years ago

1.1.15

7 years ago

1.1.14

7 years ago

1.1.13

7 years ago

1.1.12

7 years ago

1.1.11

7 years ago

1.1.10

7 years ago

1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.1.26

8 years ago

0.1.25

8 years ago

0.1.24

8 years ago

0.1.23

8 years ago

0.1.22

8 years ago

0.1.21

8 years ago

0.1.20

8 years ago

0.1.19

8 years ago

0.1.18

8 years ago

0.1.17

8 years ago

0.1.16

8 years ago

0.1.15

8 years ago

0.1.14

8 years ago

0.1.13

8 years ago

0.1.12

8 years ago

0.1.11

8 years ago

0.1.10

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago