package
A simple form builder for React in the style of Rails' form_for
npm install react-form-forGitHub-flavored package documentation.
Across all versions, last 30 days.
25 in 30 days
Runtime metadata.
Tagged topics.
An expressive and intuitive form builder for React, in the style of Rails' form_for
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)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>
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.
// 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
:warning: This module is pretty new and might have some bugs, please file an issue if you encounter any problems.
Estimated unpacked size across 5 packages.
29.3 kB
Includes dependencies.
^1.1.4
2.5.1
^2.4.1
4.1.2
^1.1.0
1.1.0