这是一个高阶函数,传入的是react组件,返回一个新的react组件,在函数内部会对传入组件进行改造,添加上一定的方法用于进行一些秘密操作 如果有对高阶组件有想要深入的请移步这里,我们这里不做过多的深究。接下来我们直接看这个函数的代码
static create = function<TOwnProps>(options: FormCreateOption<TOwnProps> = {}): ComponentDecorator<TOwnProps> {
const formWrapper = createDOMForm({
fieldNameProp: 'id',
...options,
fieldMetaProp: FIELD_META_PROP,
});
/* eslint-disable react/prefer-es6-class */
return (Component) => formWrapper(createReactClass({
propTypes: {
form: PropTypes.object.isRequired,
},
childContextTypes: {
form: PropTypes.object.isRequired,
},
getChildContext() {
return {
form: this.props.form,
};
},
componentWillMount() {
this.__getFieldProps = this.props.form.getFieldProps;
},
deprecatedGetFieldProps(name, option) {
warning(
false,
'`getFieldProps` is not recommended, please use `getFieldDecorator` instead, ' +
'see: https://u.ant.design/get-field-decorator',
);
return this.__getFieldProps(name, option);
},
render() {
this.props.form.getFieldProps = this.deprecatedGetFieldProps;
const withRef: any = {};
if (options.withRef) {
withRef.ref = 'formWrappedComponent';
} else if (this.props.wrappedComponentRef) {
withRef.ref = this.props.wrappedComponentRef;
}
return <Component {...this.props} {...withRef} />;
},
}));
};
function createBaseForm(option = {}, mixins = []) {
const { ... } = option;
return function decorate(WrappedComponent) {
const Form = createReactClass({ ... });
return argumentContainer(Form, WrappedComponent);
};
}
import hoistStatics from 'hoist-non-react-statics';
export function argumentContainer(Container, WrappedComponent) {
/* eslint no-param-reassign:0 */
Container.displayName = `Form(${getDisplayName(WrappedComponent)})`;
Container.WrappedComponent = WrappedComponent;
return hoistStatics(Container, WrappedComponent);
}