hoist-non-react-methods
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -5,9 +5,2 @@ 'use strict'; | ||
exports.default = hoistNonReactMethods; | ||
var _invariant = require('invariant'); | ||
var _invariant2 = _interopRequireDefault(_invariant); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var REACT_PROTOTYPE = { | ||
@@ -62,18 +55,27 @@ autobind: true, | ||
function hoistNonReactMethods(targetComponent, sourceComponent) { | ||
var delegateTo = arguments.length <= 2 || arguments[2] === undefined ? function (w) { | ||
var defaultConfig = { | ||
delegateTo: function delegateTo(w) { | ||
return w.refs.child; | ||
} : arguments[2]; | ||
}, | ||
hoistStatics: true | ||
}; | ||
function hoistNonReactMethods(targetComponent, sourceComponent, config) { | ||
var targetComponentName = targetComponent.displayName || targetComponent.name || 'Wrapper'; | ||
var sourceComponentName = sourceComponent.displayName || sourceComponent.name || 'WrappedComponent'; | ||
var hoistStatics = config && typeof config.hoistStatics !== 'undefined' ? config.hoistStatics : defaultConfig.hoistStatics; | ||
var delegateTo = config && typeof config.delegateTo !== 'undefined' ? config.delegateTo : defaultConfig.delegateTo; | ||
// backwards compatible where config option is delegateTo function | ||
if (typeof config === 'function') delegateTo = config; | ||
var statics = Object.getOwnPropertyNames(sourceComponent).filter(function (k) { | ||
return !REACT_STATICS[k] && !KNOWN_STATICS[k]; | ||
}); | ||
if (hoistStatics) { | ||
var statics = Object.getOwnPropertyNames(sourceComponent).filter(function (k) { | ||
return !REACT_STATICS[k] && !KNOWN_STATICS[k]; | ||
}); | ||
statics.forEach(function (methodName) { | ||
(0, _invariant2.default)(!targetComponent[methodName], 'Static method ' + methodName + ' already exists in wrapper component ' + targetComponentName + ', and won\'t be hoisted. Consider changing the name on ' + sourceComponentName + '.'); | ||
targetComponent[methodName] = sourceComponent[methodName]; | ||
}); | ||
statics.forEach(function (methodName) { | ||
if (targetComponent[methodName]) console.warn('Static method ' + methodName + ' already exists in wrapper component ' + targetComponentName + ', and won\'t be hoisted. Consider changing the name on ' + sourceComponentName + '.'); | ||
targetComponent[methodName] = sourceComponent[methodName]; | ||
}); | ||
} | ||
@@ -85,4 +87,6 @@ var methods = Object.getOwnPropertyNames(sourceComponent.prototype).filter(function (k) { | ||
methods.forEach(function (methodName) { | ||
(0, _invariant2.default)(!targetComponent.prototype[methodName], 'Method ' + methodName + ' already exists in wrapper component ' + targetComponentName + ', and won\'t be hoisted. Consider changing the name on ' + sourceComponentName + '.'); | ||
if (targetComponent.prototype[methodName]) return; | ||
if (targetComponent.prototype[methodName]) { | ||
console.warn('Method ' + methodName + ' already exists in wrapper component ' + targetComponentName + ', and won\'t be hoisted. Consider changing the name on ' + sourceComponentName + '.'); | ||
return; | ||
} | ||
@@ -89,0 +93,0 @@ targetComponent.prototype[methodName] = function () { |
{ | ||
"name": "hoist-non-react-methods", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Copies non-react specific methods from a child component to a parent component", | ||
@@ -54,4 +54,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"invariant": "^2.2.0" | ||
"sinon": "^4.2.0" | ||
} | ||
} |
@@ -34,6 +34,8 @@ # hoist-non-react-methods | ||
render() { | ||
<div> | ||
<button onClick={e => this.refs.composer.focus()}></button> | ||
<Composer ref="composer" /> | ||
</div> | ||
return ( | ||
<div> | ||
<button onClick={e => this.refs.composer.focus()}></button> | ||
<Composer ref="composer" /> | ||
</div> | ||
) | ||
} | ||
@@ -78,6 +80,8 @@ } | ||
render() { | ||
<div> | ||
<button onClick={e => this.refs.composer.focus()}></button> | ||
<Composer ref="composer" /> | ||
</div> | ||
return ( | ||
<div> | ||
<button onClick={e => this.refs.composer.focus()}></button> | ||
<Composer ref="composer" /> | ||
</div> | ||
) | ||
} | ||
@@ -123,3 +127,5 @@ } | ||
return hoistNonReactMethods(Wrapper, WrappedComponent, c => c.refs.wrappedComponent) | ||
return hoistNonReactMethods(Wrapper, WrappedComponent, { | ||
delegateTo: c => c.refs.wrappedComponent | ||
}) | ||
} | ||
@@ -131,6 +137,8 @@ } | ||
render() { | ||
<div> | ||
<button onClick={e => this.refs.composer.focus()}></button> | ||
<Composer ref="composer" /> | ||
</div> | ||
return ( | ||
<div> | ||
<button onClick={e => this.refs.composer.focus()}></button> | ||
<Composer ref="composer" /> | ||
</div> | ||
) | ||
} | ||
@@ -146,8 +154,14 @@ } | ||
WrappedComponent: ReactComponent, | ||
delegateTo: function(ReactComponent wrapperComponentInstance):ReactComponent childComponentInstance | ||
{ | ||
delegateTo: function(ReactComponent wrapperComponentInstance):ReactComponent childComponentInstance, | ||
hoistStatics: boolean, | ||
} | ||
) | ||
``` | ||
The third parameter is a function that gets the instance of the wrapper component and returns the instance of the wrapped component (e.g. `(wrapper) => wrapper.refs.child`) | ||
The third parameter is a configuration object. Options: | ||
- `delegateTo`: a function that gets the instance of the wrapper component and returns the instance of the wrapped component (e.g. `wrapper => wrapper.refs.child`) | ||
- `hoistStatics: true/false` - controls whether to hoist statics or not | ||
## Test | ||
@@ -154,0 +168,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11834
89
172
+ Addedsinon@^4.2.0
+ Added@sinonjs/commons@1.8.6(transitive)
+ Added@sinonjs/formatio@2.0.03.2.2(transitive)
+ Added@sinonjs/samsam@3.3.3(transitive)
+ Added@sinonjs/text-encoding@0.7.3(transitive)
+ Addedarray-from@2.1.1(transitive)
+ Addeddiff@3.5.0(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedjust-extend@4.2.1(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlodash.get@4.4.2(transitive)
+ Addedlolex@2.7.55.1.2(transitive)
+ Addednise@1.5.3(transitive)
+ Addedpath-to-regexp@1.9.0(transitive)
+ Addedsamsam@1.3.0(transitive)
+ Addedsinon@4.5.0(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedtype-detect@4.0.84.1.0(transitive)
- Removedinvariant@^2.2.0
- Removedinvariant@2.2.4(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedloose-envify@1.4.0(transitive)