Created
March 7, 2017 18:41
-
-
Save dtothefp/c04ddba8ec6110e5f59f25045a67b464 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {createClass} from 'react'; | |
import {assign} from 'lodash'; | |
import hoistNonReactStatics from 'hoist-non-react-statics'; | |
function createComponent(Component, dataBindings) { | |
const displayName = `ProvideContextComponent(${Component.displayName || Component.name})`; | |
const ProvideContextComponent = createClass({ | |
displayName, | |
contextTypes: dataBindings, | |
render() { | |
/** | |
* Important to override context values with props because `formId` can be passed on the | |
* `<Form />` as props or as context | |
*/ | |
return React.createElement(Component, assign({}, this.state, this.context, this.props)); | |
} | |
}); | |
hoistNonReactStatics(ProvideContextComponent, Component); | |
return ProvideContextComponent; | |
} | |
/* | |
* Decorator using `React.Component` | |
*/ | |
export default function provideContextDec(Component, dataBindings) { | |
if (arguments.length === 0 || typeof arguments[0] !== 'function') { | |
dataBindings = arguments[0]; | |
return function connectToData(ComponentToDecorate) { | |
return createComponent(ComponentToDecorate, dataBindings); | |
}; | |
} | |
return createComponent.apply(null, arguments); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment