Package Exports
- react-formstate
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-formstate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
react-formstate
a clean, simple form framework for react
$ npm install react-formstate --saveexample
import React from 'react';
import { FormState, FormObject } from 'react-formstate';
import Input from './Input.jsx';
export default class LoginForm extends React.Component {
constructor(props) {
super(props);
this.formState = new FormState(this);
// if you were editing a model, you could "inject" props.model
this.state = this.formState.createUnitOfWork().injectModel();
// since we're not injecting a model, the above is equivalent to
this.state = {};
this.handleSubmit = this.handleSubmit.bind(this);
}
render() {
let submitMessage = '';
if (this.formState.isInvalid()) {
submitMessage = 'Please fix validation errors';
}
return (
<form>
<FormObject formState={this.formState}>
<Input formField='username' label='Username' required />
<Input formField='password' label='Password' required type='password' />
</FormObject>
<input type='submit' value='Submit' onClick={this.handleSubmit} />
<span>submitMessage</span>
</form>
);
}
handleSubmit(e) {
e.preventDefault();
let model = this.formState.createUnitOfWork().createModel();
if (model) {
alert(JSON.stringify(model)); // proceed with valid data
}
// else: createModel called setState to set the appropriate validation messages
}
}your input component might look like
import React from 'react';
export default class Input extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return !nextProps.fieldState.equals(this.props.fieldState);
}
render() {
console.log('render ' + this.props.label); // for demonstration only
return (
<div>
<label>{this.props.label}</label>
<input
type={this.props.type || 'text'}
value={this.props.fieldState.getValue()}
onChange={this.props.updateFormState}
/>
<span>{this.props.fieldState.getMessage()}</span>
</div>
);
}
}remarks
- no mixin or decoration, just an api
- form state lives with your form component until the form is submitted with valid data
- designed to work with react controlled components
- framework simply provides props, you lay out your inputs
features and examples
- validation
- nested form components
- asynchronous validation
- arrays, adding and removing inputs in response to state changes
- other input types: checkbox, checkbox group, radio group, select, and multi-select
- show validation message on blur
- onUpdate callback
documentation
peer dependencies
- react (!)
- assumes es5 (for example: Object.keys and Array.isArray)