npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-form-for

v1.1.0

Published

A simple form builder for React in the style of Rails' form_for

Downloads

27

Readme

react-form-for

An expressive and intuitive form builder for React, in the style of Rails' form_for

example

var React = require('react')
var {Form, Fields, Field} = require('react-form-for')
var {ListEditor} = require('react-form-for').Components

var DateField = require('./date-field')
var languages = require('./languages')

var ExampleForm = React.createClass({
  getInitialState: function() {
    return {value: {}}
  },
  handleChange: function(updatedValue) {
    this.setState({value: updatedValue})
  },
  renderLanguageSelectOptions: function() {
    return languages.map((name) =>
      <option key={name} value={name}>{name}</option>
    )
  },
  render: function() {
    var {value} = this.state
    var onChange = this.handleChange

    return (
      <Form for={value} onChange={this.handleChange}>
        <h2>A Beautiful Form</h2>
        <Field for="name" autofocus />
        <Field
          for="birthday"
          component={<DateField className="on-a-date" />}
          help="Choose a date"
        />
        <Field for="language" type="select">
          {this.renderLanguageSelectOptions()}
        </Field>
        <div className="panel collapsible">
          <Fields for="address">
            <Field for="street" />
            <Field for="town" />
            <Field for="state" />
          </Fields>
        </div>
        <List for="members" component={ListEditor}>
          <Field for="name" />
          <Field for="age" />
          <Field for="occupation" />
        </List>
      </Form>
    )
  }
})

React.render(<ExampleForm />, document.body)

Custom field components

A possible implementation of the DateField from the example above:

var React = require('react')

var DateField = React.createClass({
  render: function() {
    return (
      <div>
        <label>
          {this.props.label}
          <input
            type="date"
            value={this.props.value}
            onChange={this.props.onChange}
          />
        </label>
        <span>{this.props.help}</span>
      </div>
    )
  }
})

module.exports = DateField

Note the use of the important props value, onChange and label which are provided by the form builder. Other props such as help are passed through from the <Field /> proxy components used above.

Overriding the default field component

// as long as a component takes a `value` prop (and ideally a `label` prop)
// and an `onChange` callback prop, it can be used as a react-form-for field
var Input = require('react-bootstrap/Input')
var {Form, Fields, Field} = require('react-form-for')

var ExampleForm = React.createClass({
  handleChange: function(updatedValue) {
    this.setState({value: updatedValue})
  },
  // the checkbox Field gets an Input component with different layout classes
  getCheckboxComponent: function() {
    return (
      <Input type="checkbox" wrapperClassName="col-xs-offset-2 col-xs-10"/>
    )
  },
  render: function() {
    var formOpts = {
      onChange: this.handleChange,
      fieldComponent: (
        <Input labelClassName="col-xs-2" wrapperClassName="col-xs-10" />
      )
    }
      // all of these fields will be rendered as a react-bootstrap/Input
      <Form for={this.state.value} {...formOpts} className="form-horizontal">
        <Field for="name" />
        <Field for="active" component={this.getCheckboxComponent()} />
        <Fields for="financial_stuff"
          <Field for="account_balance" addonBefore="$" />
        </Fields>
      </Form>
    )
  }
})
Warning

:warning: This module is pretty new and might have some bugs, please file an issue if you encounter any problems.