JSPM

  • Created
  • Published
  • Downloads 167208
  • Score
    100M100P100Q161729F

React High Order Form Component

Package Exports

  • rc-form

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 (rc-form) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

rc-form

React High Order Form Component.

NPM version build status Test coverage gemnasium deps node version npm download

Development

npm install
npm start

Example

http://localhost:8000/examples/

online example: http://react-component.github.io/form/examples/

Feature

  • support reactjs and even react-native

install

rc-form

Usage

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>)
  }
}

createForm(formOption): Function

formOption.validateMessages: Object

preset messages of async-validator

formOption.refComponent: Boolean

Defaults to false. Whether save component as ref. Can get instance from validateFields's callback parameter by instance member.

formOption.onFieldsChange(props, fields)

Called when field changed, you can dispatch fields to redux store.

formOption.mapPropsToFields(props)

convert value from props to fields. used for read fields from redux store.

createForm() will return another function:

function(WrappedComponent: React.Component): React.Component

Will pass a object as prop form with the following members to WrappedComponent:

getFieldProps(name, option): Object

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.

name: String

This input's unique name.

option.valuePropName: String

Prop name of component's value field, eg: checkbox should be set ti checked ...

option.initialValue

Initial value of current component.

option.normalize(value, prevValue, allValues)

Return normalized value

option.trigger: String

Defaults to onChange. Event which is listened to collect form data.

option.validate: Object[]

option.validate[n].trigger: String|String[]

Event which is listened to validate. Defaults to onChange, set to falsy to only validate when call props.validateFields.

option.validate[n].rules: Object[]

Validator rules. see: async-validator

option.validateTrigger && option.rules

{
  validateTrigger: 'onBlur',
  rules: [{required: true}],
}
// is the shorthand of
{
  validate: [{
    trigger: 'onBlur',
    rules: [required: true],
  }],
}

option.validateFirst: Boolean

Defaults to false. whether stop validate on first rule of error for this field.

getFieldsValue([fieldNames: String[]])

Get fields value by fieldNames.

getFieldValue(fieldName: String)

Get field value by fieldName.

setFieldsValue(obj: Object)

Set fields value by kv object.

setFieldsInitialValue(obj: Object)

Set fields initialValue by kv object. use for reset and initial display/value.

setFields(obj: Object)

Set fields by kv object. each field can contain errors and value member.

validateFields([fieldNames: String[]], [options: Object], callback: Function(errors, values))

Validate and get fields value by fieldNames.

options is the same as validate method of async-validator. add a new force member.

options.force: Boolean

Defaults to false. Whether to validate fields which have been validated(caused by validateTrigger).

getFieldError(name): String[]

Get input's validate errors.

isFieldValidating(name: String): Bool

Whether this input is validating.

isFieldsValidating(names: String[]): Bool

Whether one of the inputs is validating.

isSubmitting(): Bool

Whether the form is submitting.

submit(callback: Function)

Cause isSubmitting to return true, after callback called, isSubmitting return false.

resetFields([names: String[]])

Reset specified inputs. Defaults to all.

Test Case

http://localhost:8000/tests/runner.html?coverage

Coverage

http://localhost:8000/node_modules/node-jscover/lib/front-end/jscoverage.html?w=http://localhost:8000/tests/runner.html?coverage

License

rc-form is released under the MIT license.