This repository has been archived by the owner on Nov 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
enhancer.js
70 lines (62 loc) · 2.89 KB
/
enhancer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import copyProperties from '../utils/copyProperties'
import { PropTypes } from 'react'
import _ from 'lodash'
const contextType = { _lookConfig: PropTypes.object }
/**
* Wrapper that maps your styles to a React Component
* @param {Object} CustomComponent - a valid React Component that gets styles applied
* @param {Object} config - additional processors that modify the styles
*/
export default (CustomComponent, config = { }) => {
// Enhancing stateless functional Components
// Depending on availability of setState
if (!CustomComponent.prototype.setState) {
const LookStateless = (props, context) => {
const renderedElement = CustomComponent(props, context)
const contextConfig = context._lookConfig || null
const elementConfig = renderedElement.props.lookConfig || null
// Compose all possible ways to configure Look
const composedConfig = _.merge({ }, contextConfig, config, elementConfig)
// Mocking the Component to use the same consistent interface
// for all plugins, mixins and to improve developer experience
const Component = { props, context }
// Passing the displayName to improve developer experience
Component.constructor = {
displayName: CustomComponent.name || 'Component'
}
return context._lookConfig._resolveStyles(Component, renderedElement, composedConfig)
}
// Transfer static props of stateless components:
copyProperties(CustomComponent, LookStateless)
// Passing contextTypes to be able to reference context
LookStateless.contextTypes = _.merge({ }, CustomComponent.contextTypes, contextType)
// Flag as Look-enhanced Component
LookStateless._isLookEnhanced = true
return LookStateless
}
// Enhancing ES2015 classes
// This will let you use state and do some render optimizations
class LookComponent extends CustomComponent {
constructor() {
super(...arguments)
}
// Inherit the original displayName for proper use later on
static displayName = CustomComponent.displayName || CustomComponent.name || 'Component';
static childContextTypes = { ...CustomComponent.childContextTypes, ...contextType };
static contextTypes = { ...CustomComponent.contextTypes, ...contextType };
static _isLookEnhanced = true;
render() {
const renderedElement = super.render() // eslint-disable-line
const contextConfig = this.context._lookConfig || null
const elementConfig = renderedElement.props.lookConfig || null
// Compose all possible ways to configure Look
const composedConfig = _.merge({ }, contextConfig, config, elementConfig)
return this.context._lookConfig._resolveStyles(this, renderedElement, composedConfig)
}
}
// copy props in order to get hmr working correctly
if (process.env.NODE_ENV !== 'production') {
copyProperties(CustomComponent.prototype, LookComponent.prototype)
}
return LookComponent
}