Giter VIP home page Giter VIP logo

react-form's Introduction

React-Form

Simple, powerful, highly composable forms in React

Build Status npm version npm downloads license React-Tools-Chat

Features

  • ๐Ÿš€ Lightweight and fast.
  • ๐Ÿ”ฅ Built-in input primitives for building quickly.
  • โš–๏ธ Scales from tiny to massively complex forms with ease.
  • ๐Ÿšš Easily integrate with 3rd party components or build your own!
  • โœ๏ธ Nested Fields and ultra-composable syntax for complex form shapes.
  • โฒ Asynchronous validation
  • ๐ŸŽ› Simple API that supports manipulating values, errors, warnings, and successes
  • ๐Ÿ‘‰ Render Props!
  • ๐Ÿ˜‚ Works in IE (with a polyfill or two)

Questions? Ideas? Chat with us!

Sign up for the React-Tools Slack Org!

Installation

npm install --save react-form

Basic Usage

import { Form, Text, Radio, TextArea, Checkbox } from 'react-form';

const ExampleForm = () => (
  <Form render={({
    submitForm
  }) => (
    <form onSubmit={submitForm}>
      <Text field="firstName" placeholder='First Name' />
      <Text field="lastName" placeholder='Last Name' />
      <div>
        <label>
          Male <Radio field='gender' value="male" />
        </label>
        <label>
          Female <Radio field='gender' value="female" />
        </label>
      </div>
      <TextArea field="bio" />
      <Checkbox field="agreesToTerms" />
      <button type="submit">Submit</button>
    </form>
  )} />
)

Array and Data-driven fields

import { Form, Text } from "react-form";

const ExampleForm = () => (
  <Form
    render={({ submitForm, values, addValue, removeValue }) => (
      <form onSubmit={submitForm}>
        <Text field="firstName" placeholder="First Name" />
        <Text field="lastName" placeholder="Last Name" />
        <div>
          Friends
          {values.friends &&
            values.friends.map((friend, i) => (
              // Loop over the friend values and create fields for each friend
              <div>
                <Text
                  field={["friends", i, "firstName"]}
                  placeholder="First Name"
                />
                <Text
                  field={["friends", i, "lastName"]}
                  placeholder="Last Name"
                />
                // Use the form api to add or remove values to the friends array
                <button type="button" onClick={() => removeValue("friends", i)}>
                  Remove Friend
                </button>
              </div>
            ))}
          // Use the form api to add or remove values to the friends array
          <button type="button" onClick={() => addValue("friends", {})}>Add Friend</button>
        </div>
        <button type="submit">Submit</button>
      </form>
    )}
  />
);

Advanced Field reuse, and Nested Fields

import { Form, FormApi, NestedField, Text } from "react-form"

// Reuse The user fields for the user and their friends!
const UserFields = () => (
  <div>
    <Text field="firstName" placeholder="First Name" />
    <Text field="lastName" placeholder="Last Name" />
  </div>
)

const ExampleForm = () => (
  <Form
    onSubmit={values => console.log(values)}
    render={({ submitForm, values, addValue, removeValue }) => (
      <form onSubmit={submitForm}>
        <UserFields />
        <NestedField
          field="friends"
          render={() => (
            // Create a new nested field context
            <div>
              Friends
              {values.friends &&
                values.friends.map((friend, i) => (
                  <div key={i}>
                    <NestedField
                      field={i}
                      render={() => (
                        <UserFields /> // Now the user fields will map to each friend!
                      )}
                    />
                    <button type="button" onClick={() => removeValue("friends", i)}>
                      Remove Friend
                    </button>
                  </div>
                ))}
              <button type="button" onClick={() => addValue("friends", {})}>
                Add Friend
              </button>
            </div>
          )}
        />
        <button type="submit">Submit</button>
      </form>
    )}
  />
)

Examples & Documentation

Visit react-form.js.org for examples and documentation for 3.x.x of React-Form.

Older versions:

react-form's People

Contributors

anyxem avatar averagez avatar avrahams1 avatar bruceharris avatar chibicode avatar davidhooper avatar djlechuck avatar escocreeks avatar hnq90 avatar jihchi avatar joepuzzo avatar krizka avatar marshallswain avatar mikehazell avatar mjhm avatar sebastiaannijland avatar seethruhead avatar tannerlinsley avatar thchia avatar thg303 avatar tiffwu avatar vadimzhiltsov avatar z-vr avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.