Giter Club home page Giter Club logo

react-formation's Introduction

Get started with React Formation

Build Status Coverage Status

Install

You can install React Formation from npm by running npm install react-formation. If you are using common js, you can require it like this:

var Formation = require('react-formation');

Create a Form

First, let's define the structure of your form. You can do that by using CreateForm just like how you would use React.createClass, including a render function:

var Formation = require('react-formation');

var Form = Formation.CreateForm({
  render: function () {
    return (<form>

      <label>Name</label>
      <input type="text" name="name" />

      <label>Email</label>
      <input type="text" name="email" />

      <button>Submit</button>

    </form>);
  }
});

Next, add a getSchema method that returns a schema defining all the fields in the form, and link corresponding inputs with this.linkField:

var Form = Formation.CreateForm({

  getSchema: function () {
    return {
      name: {required: true}
      email: {validations: 'email'}
    };
  },

  render: function () {
    return (<form>

      <label>Name</label>
      <input type="text" valueLink={this.linkField('name')} />

      <label>Email</label>
      <input type="text" valueLink={this.linkField('email')} />

      <button>Submit</button>

    </form>);
  }

});

Finally, add an onSuccess callback that gets called on a successful submit, and add this.submitForm as a callback to any submit buttons.

var Form = Formation.CreateForm({

  getSchema: function () {
    return {
      name: {required: true}
      email: {validations: 'email'}
    };
  },

  onSuccess: function (data) {
    console.log(data);
  },

  render: function () {
    return (<form>

      <label>Name</label>
      <input type="text" valueLink={this.linkField('name')} />

      <label>Email</label>
      <input type="text" valueLink={this.linkField('email')} />

      <button onClick={this.submitForm}>Submit</button>

    </form>);
  }

});

Rendering a Form

You can use your new Form class just like you would any other React element, including passing props. For example, if you wanted to render it directly into document.body:

React.render(<Form />, document.body);

More

Check out the guide and examples:

react-formation's People

Contributors

eiriklv avatar k88hudson avatar pippinlee avatar

Watchers

 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.