Gathering detailed insights and metrics for rc-form
Gathering detailed insights and metrics for rc-form
Gathering detailed insights and metrics for rc-form
Gathering detailed insights and metrics for rc-form
React High Order Form Component(web & react-native)
npm install rc-form
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,806 Stars
288 Commits
296 Forks
48 Watching
16 Branches
48 Contributors
Updated on 10 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-16.7%
29,098
Compared to previous day
Last week
10.8%
180,074
Compared to previous week
Last month
7.4%
690,199
Compared to previous month
Last year
13.2%
7,419,671
Compared to previous year
9
1
React High Order Form Component.
npm install
npm start
open http://localhost:8000/examples/
1import { createForm, formShape } from 'rc-form'; 2 3class Form extends React.Component { 4 static propTypes = { 5 form: formShape, 6 }; 7 8 submit = () => { 9 this.props.form.validateFields((error, value) => { 10 console.log(error, value); 11 }); 12 } 13 14 render() { 15 let errors; 16 const { getFieldProps, getFieldError } = this.props.form; 17 return ( 18 <div> 19 <input {...getFieldProps('normal')}/> 20 <input {...getFieldProps('required', { 21 onChange(){}, // have to write original onChange here if you need 22 rules: [{required: true}], 23 })}/> 24 {(errors = getFieldError('required')) ? errors.join(',') : null} 25 <button onClick={this.submit}>submit</button> 26 </div> 27 ); 28 } 29} 30 31export createForm()(Form);
Expo preview
Or a quicker version:
1import { createForm } from 'rc-form'; 2 3class Form extends React.Component { 4 componentWillMount() { 5 this.requiredDecorator = this.props.form.getFieldDecorator('required', { 6 rules: [{required: true}], 7 }); 8 } 9 10 submit = () => { 11 this.props.form.validateFields((error, value) => { 12 console.log(error, value); 13 }); 14 } 15 16 render() { 17 let errors; 18 const { getFieldError } = this.props.form; 19 return ( 20 <div> 21 {this.requiredDecorator( 22 <input 23 onChange={ 24 // can still write your own onChange 25 } 26 /> 27 )} 28 {(errors = getFieldError('required')) ? errors.join(',') : null} 29 <button onClick={this.submit}>submit</button> 30 </div> 31 ); 32 } 33} 34 35export createForm()(Form);
Option | Description | Type | Default |
---|---|---|---|
option.validateMessages | Preseted messages of async-validator | Object | {} |
option.onFieldsChange | Called when field changed, you can dispatch fields to redux store. | (props, changed, all): void | NOOP |
option.onValuesChange | Called when value changed. | (props, changed, all): void | NOOP |
option.mapProps | Get new props transferred to WrappedComponent. | (props): Object | props => props |
option.mapPropsToFields | Convert value from props to fields. Used for read fields from redux store. | (props): Object | NOOP |
option.fieldNameProp | Where to store the name argument of getFieldProps . | String | - |
option.fieldMetaProp | Where to store the meta data of getFieldProps . | String | - |
option.fieldDataProp | Where to store the field data | String | - |
option.withRef(deprecated) | Maintain an ref for wrapped component instance, use refs.wrappedComponent to access. | boolean | false |
1class Form extends React.Component { ... } 2 3// deprecated 4const EnhancedForm = createForm({ withRef: true })(Form); 5<EnhancedForm ref="form" /> 6this.refs.form.refs.wrappedComponent // => The instance of Form 7 8// Recommended 9const EnhancedForm = createForm()(Form); 10<EnhancedForm wrappedComponentRef={(inst) => this.formRef = inst} /> 11this.formRef // => The instance of Form
The returned function of createForm(). It will pass an 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.
1<form> 2 <input {...getFieldProps('name', { ...options })} /> 3</form>
This input's unique name.
Option | Description | Type | Default |
---|---|---|---|
option.valuePropName | Prop name of component's value field, eg: checkbox should be set to checked ... | String | 'value' |
option.getValueProps | Get the component props according to field value. | (value): Object | (value) => ({ value }) |
option.getValueFromEvent | Specify how to get value from event. | (e): any | See below |
option.initialValue | Initial value of current component. | any | - |
option.normalize | Return normalized value. | (value, prev, all): Object | - |
option.trigger | Event which is listened to collect form data. | String | 'onChange' |
option.validateTrigger | Event which is listened to validate. Set to falsy to only validate when call props.validateFields. | String | String[] |
option.rules | Validator rules. see: async-validator | Object[] | - |
option.validateFirst | Whether stop validate on first rule of error for this field. | boolean | false |
option.validate | Object[] | - | |
option.validate[n].trigger | Event which is listened to validate. Set to falsy to only validate when call props.validateFields. | String | String[] |
option.validate[n].rules | Validator rules. see: async-validator | Object[] | - |
option.hidden | Ignore current field while validating or gettting fields | boolean | false |
option.preserve | Whether to preserve the value. That will remain the value when the field be unmounted and be mounted again | boolean | false |
getValueFromEvent
1function defaultGetValueFromEvent(e) { 2 if (!e || !e.target) { 3 return e; 4 } 5 const { target } = e; 6 return target.type === 'checkbox' ? target.checked : target.value; 7}
1{ 2 validateTrigger: 'onBlur', 3 rules: [{required: true}], 4} 5// is the shorthand of 6{ 7 validate: [{ 8 trigger: 'onBlur', 9 rules: [{required: true}], 10 }], 11}
Similar to getFieldProps
, but add some helper warnings and you can write onXX directly inside React.Node props:
1<form> 2 {getFieldDecorator('name', otherOptions)(<input />)} 3</form>
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.
And add force
.
Defaults to false. Whether to validate fields which have been validated(caused by validateTrigger).
Get inputs' validate errors.
Get input's validate errors.
Whether this input is validating.
Whether one of the inputs is validating.
Whether this input's value had been changed by user.
Whether one of the inputs' values had been changed by user.
Reset specified inputs. Defaults to all.
Whether the form is submitting.
Cause isSubmitting to return true, after callback called, isSubmitting return false.
createDOMForm enhancement, support props.form.validateFieldsAndScroll
props.form.validateFields enhancement, support scroll to the first invalid form field, scroll
is the same as dom-scroll-into-view's function parameter config
.
Defaults to first scrollable container of form field(until document).
Do not use stateless function component inside Form component: https://github.com/facebook/react/pull/6534
you can not set same prop name as the value of validateTrigger/trigger for getFieldProps
1<input {...getFieldProps('change',{
2 onChange: this.iWantToKnow // you must set onChange here or use getFieldDecorator to write inside <input>
3})}>
1<input {...getFieldProps('ref')} /> 2 3this.props.form.getFieldInstance('ref') // use this to get ref
or
1<input {...getFieldProps('ref',{
2 ref: this.saveRef // use function here or use getFieldDecorator to write inside <input> (only allow function)
3})} />
npm test
npm run chrome-test
npm run coverage
open coverage/ dir
rc-form is released under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 9/27 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More