React High Order Form Component.
npm install
npm start
http://localhost:8000/examples/
online example: http://react-component.github.io/form/examples/
- support reactjs and even react-native
import { createForm } from 'rc-form';
@createForm()
class Form extends React.Component {
submit = () => {
this.props.form.validateFields((error, value) => {
console.log(error, value);
});
}
render() {
let errors;
const {getFieldProps, getFieldError} = this.props.form;
return (<div>
<input {...getFieldProps('normal')}/>
<input {...getFieldProps('required', {
rules: [{required: true}],
})}/>
{(errors = getFieldError('required')) ? errors.join(',') : null}
<button onClick={this.submit}>submit</button>
</div>)
}
}
preset messages of async-validator
Get new props transfered to WrappedComponent. Defaults to props=>props.
Called when field changed, you can dispatch fields to redux store.
convert value from props to fields. used for read fields from redux store.
createForm() will return another function:
Will pass a object as prop form with the following members to WrappedComponent:
Will create props which can be set on a input/InputComponent which support value and onChange interface.
After set, this will create a binding with this input.
This input's unique name.
Prop name of component's value field, eg: checkbox should be set to checked
...
Specify how to get value from event. Defaults to
function (e) {
if (!e || !e.target) {
return e;
}
const { target } = e;
return target.type === 'checkbox' ? target.checked : target.value;
}
get the component props according to field value. Defaults to
function (value) {
return { value };
}
Initial value of current component.
Return normalized value
Defaults to onChange
. Event which is listened to collect form data.
Event which is listened to validate.
Defaults to onChange
, set to falsy to only validate when call props.validateFields.
Validator rules. see: async-validator
{
validateTrigger: 'onBlur',
rules: [{required: true}],
}
// is the shorthand of
{
validate: [{
trigger: 'onBlur',
rules: [required: true],
}],
}
Defaults to false. whether stop validate on first rule of error for this field.
Get fields value by fieldNames.
Get field value by fieldName.
Get field react public instance by fieldName.
Set fields value by kv object.
Set fields initialValue by kv object. use for reset and initial display/value.
Set fields by kv object. each field can contain errors and value member.
Validate and get fields value by fieldNames.
options is the same as validate method of async-validator. add a new force member.
Defaults to false. Whether to validate fields which have been validated(caused by validateTrigger).
Get input's validate errors.
Whether this input is validating.
Whether one of the inputs is validating.
Whether the form is submitting.
Cause isSubmitting to return true, after callback called, isSubmitting return false.
Reset specified inputs. Defaults to all.
createForm enhancement, support props.form.validateFieldsAndScroll
props.form.validateFieldsAndScroll([fieldNames: String[]], [options: Object], callback: Function(errors, values))
props.form.validateFields enhancement, support scroll to the first invalid form field
Defaults to first scrollable container of form field(until document).
- you can not set same prop name as the value of validateTrigger/trigger.
<input {...getFieldProps('change',{
onChange: this.iWantToKnow // you must set onChange here
})}>
- you can not use ref prop.
<input {...getFieldProps('ref')} />
this.props.form.getFieldInstance('ref') // use this to get ref
or
<input {...getFieldProps('ref',{
ref: this.saveRef // or use function here
})} />
npm test
npm run chrome-test
npm run coverage
open coverage/ dir
rc-form is released under the MIT license.