Skip to content

Commit

Permalink
feat: add wrappedComponentRef and deprecate withRef
Browse files Browse the repository at this point in the history
  • Loading branch information
benjycui committed Jun 13, 2017
1 parent 81acfc4 commit edfe6c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,23 @@ export createForm()(Form);
| formOption.mapPropsToFields | Convert value from props to fields. Used for read fields from redux store. | (props): Object | NOOP |
| formOption.withRef(deprecated) | Maintain an ref for wrapped component instance, use `refs.wrappedComponent` to access. | boolean | false |
createForm() will return another function:
### Note: use wrappedComponentRef instead of withRef after [email protected]
```jsx
class Form extends React.Component { ... }

// deprecated
const EnhancedForm = createForm({ withRef: true })(Form);
<EnhancedForm ref="form" />
this.refs.form.refs.wrappedComponent // => The instance of Form

// Recommended
const EnhancedForm = createForm()(Form);
<EnhancedForm wrappedComponentRef={(inst) => this.formRef = inst} />
this.formRef // => The instance of Form
```
## createForm() will return another function
### function(WrappedComponent: React.Component): React.Component
Expand Down Expand Up @@ -272,7 +288,7 @@ Reset specified inputs. Defaults to all.
## rc-form/lib/createDOMForm(formOption): Function
createForm enhancement, support props.form.validateFieldsAndScroll
createDOMForm enhancement, support props.form.validateFieldsAndScroll
### props.form.validateFieldsAndScroll([fieldNames: String[]], [options: Object], callback: Function(errors, values))
Expand Down
5 changes: 4 additions & 1 deletion src/createBaseForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,18 @@ function createBaseForm(option = {}, mixins = []) {
},

render() {
const { wrappedComponentRef, ...restProps } = this.props;
const formProps = {
[formPropName]: this.getForm(),
};
if (withRef) {
formProps.ref = 'wrappedComponent';
} else if (wrappedComponentRef) {
formProps.ref = wrappedComponentRef;
}
const props = mapProps.call(this, {
...formProps,
...this.props,
...restProps,
});
return <WrappedComponent {...props}/>;
},
Expand Down

0 comments on commit edfe6c3

Please sign in to comment.