UNPKG

hc-components-test

Version:

基于react的通用组件库

61 lines (55 loc) 1.84 kB
import React from 'react'; import hoistNonReactStatics from 'hoist-non-react-statics'; export function getComponent(option, getProps) { if (option === false) { return () => getComponent.emptyComponent; } let AppointComponent; let propValues; if (Array.isArray(option)) { if (option[0] && option[0].prototype && option[0].prototype.isReactComponent) { AppointComponent = option[0]; propValues = option[1] || {}; } else { propValues = option[0] || {}; } } else { if (option && option.prototype && option.prototype.isReactComponent) { AppointComponent = option; } else { propValues = option || {}; } } return BaseComponent => { AppointComponent = AppointComponent || BaseComponent || getComponent.emptyComponent; const defaultProps = AppointComponent.defaultProps || {}; class Component extends React.PureComponent { state = {} getRealInstance() { return this._wrappedComponent; } render() { getProps = getProps || propValues.getProps; const props = Object.assign(getProps ? getProps.call(this, this.props, this.context, this.setState.bind(this)) : {}, propValues); merge(props, propValues, defaultProps, this.props); return (<AppointComponent ref={inst => this._wrappedComponent = inst} {...props} {...this.state} />); } } return hoistNonReactStatics(Component, AppointComponent); }; } function merge(props, o, t, p) { for (let key in p) { if (props[key] === undefined) { if (t.hasOwnProperty(key)) { props[key] = o[key] === undefined ? p[key] : o[key]; } else { props[key] = p[key] === undefined ? o[key] : p[key]; } } } return props; } getComponent.emptyComponent = function EmptyComponent() { return (<div></div>); };