hoist-non-react-methods
Advanced tools
| # 1.1.0 | ||
| - `hoistNonReactMethods` now gets a config object, with: | ||
| - `delegateTo` - unchanged functionality (see README) | ||
| - `hoistStatics: true/false` - controls whether to hoist statics or not | ||
| # 1.0.0 | ||
| Initial release |
+3
-2
@@ -15,3 +15,3 @@ { | ||
| "array-bracket-spacing": [ 2, "always" ], | ||
| "comma-dangle": [ 2, "never" ], | ||
| "comma-dangle": 0, | ||
| "eol-last": 2, | ||
@@ -32,3 +32,4 @@ "indent": [ 2, 2, { "SwitchCase": 1 } ], | ||
| "react/jsx-no-undef": 2, | ||
| "react/wrap-multilines": 2 | ||
| "react/wrap-multilines": 2, | ||
| "no-console": 0 | ||
| }, | ||
@@ -35,0 +36,0 @@ "plugins": [ |
+23
-19
@@ -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 () { |
+2
-2
| { | ||
| "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" | ||
| } | ||
| } |
+29
-15
@@ -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
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
11834
5.28%89
9.88%172
8.86%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed