Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

hoist-non-react-methods

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hoist-non-react-methods - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

CHANGELOG.md

42

lib/index.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc