react-class
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -1,14 +0,27 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var React = require('react'); | ||
var assign = require('object-assign'); | ||
var skipMethods = { | ||
'constructor': 1, | ||
'render': 1, | ||
'shouldComponentUpdate': 1, | ||
'componentWillMount': 1, | ||
'componentDidMount': 1, | ||
'componentWillReceiveProps': 1, | ||
'componentWillUpdate': 1, | ||
'componentDidUpdate': 1, | ||
'componentWillUnmount': 1 | ||
}; | ||
function autoBind(object) { | ||
@@ -18,3 +31,3 @@ var proto = object.constructor.prototype; | ||
var names = Object.getOwnPropertyNames(proto).filter(function (key) { | ||
return key != 'constructor' && key != 'render' && typeof proto[key] == 'function'; | ||
return skipMethods[key] !== 1 && typeof proto[key] === 'function'; | ||
}); | ||
@@ -30,3 +43,3 @@ | ||
var ReactClass = (function (_React$Component) { | ||
var ReactClass = function (_React$Component) { | ||
_inherits(ReactClass, _React$Component); | ||
@@ -37,46 +50,14 @@ | ||
_get(Object.getPrototypeOf(ReactClass.prototype), 'constructor', this).call(this, props); | ||
autoBind(this); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(ReactClass).call(this, props)); | ||
autoBind(_this); | ||
return _this; | ||
} | ||
_createClass(ReactClass, [{ | ||
key: 'prepareProps', | ||
value: function prepareProps(thisProps) { | ||
var props = assign({}, thisProps); | ||
props.style = this.prepareStyle(props); | ||
props.className = this.prepareClassName(props); | ||
return props; | ||
} | ||
}, { | ||
key: 'prepareClassName', | ||
value: function prepareClassName(props) { | ||
var className = props.className || ''; | ||
var defaultProps = this.constructor.defaultProps; | ||
if (defaultProps && defaultProps.defaultClassName != null) { | ||
className += ' ' + defaultProps.defaultClassName; | ||
} | ||
return className; | ||
} | ||
}, { | ||
key: 'prepareStyle', | ||
value: function prepareStyle(props) { | ||
var defaultStyle; | ||
if (this.constructor.defaultProps) { | ||
defaultStyle = this.constructor.defaultProps.defaultStyle; | ||
} | ||
return assign({}, defaultStyle, props.style); | ||
} | ||
}]); | ||
return ReactClass; | ||
})(React.Component); | ||
}(React.Component); | ||
module.exports = ReactClass; | ||
module.exports = ReactClass; | ||
exports.default = ReactClass; | ||
exports.autoBind = autoBind; | ||
exports.Component = ReactClass; |
{ | ||
"name": "react-class", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "A carefully crafted base class for all your React components ", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"lib": "gulp", | ||
"test": "ava", | ||
"lib": "rimraf lib && babel --out-dir lib src", | ||
"dev": "webpack-dev-server --progress --colors --port 8080 --config ./build/dev.config.js", | ||
"dist": "webpack -p --progress --colors --config ./build/dist.config.js", | ||
"release": "npm run dist && npm run lib", | ||
"build": "npm run dist && npm run lib" | ||
"release": "npm run lib", | ||
"build": "npm run lib" | ||
}, | ||
@@ -25,3 +25,3 @@ "repository": { | ||
], | ||
"author": "Zippy Technologies", | ||
"author": "Zippy Technologies <contact@zippyui.com>", | ||
"license": "MIT", | ||
@@ -33,16 +33,38 @@ "bugs": { | ||
"devDependencies": { | ||
"babel-core": "^5.6.15", | ||
"babel-loader": "^5.2.2", | ||
"gulp": "^3.9.0", | ||
"gulp-babel": "^5.1.0", | ||
"node-libs-browser": "^0.5.2", | ||
"webpack": "^1.9.13", | ||
"webpack-dev-server": "^1.9.0" | ||
"ava": "^0.15.2", | ||
"babel-cli": "^6.10.1", | ||
"babel-core": "^6.10.4", | ||
"babel-loader": "^6.2.4", | ||
"babel-preset-es2015": "^6.9.0", | ||
"babel-preset-react": "^6.11.1", | ||
"babel-register": "^6.9.0", | ||
"enzyme": "^2.4.1", | ||
"jsdom": "^9.4.1", | ||
"react": "^15.2.1", | ||
"react-addons-test-utils": "^15.2.1", | ||
"react-dom": "^15.2.1", | ||
"rimraf": "^2.5.3", | ||
"webpack": "^1.13.1", | ||
"webpack-dev-server": "^1.14.1" | ||
}, | ||
"peerDependencies": { | ||
"react": ">=0.14.0 || >=15.0.0-0" | ||
"react": ">=0.14.0 || >=15.0.0-0", | ||
"react-dom": ">=0.14.0 || >=15.0.0-0" | ||
}, | ||
"dependencies": { | ||
"object-assign": "^4.0.1" | ||
"object-assign": "^4.1.0" | ||
}, | ||
"ava": { | ||
"source": [ | ||
"src/**/*.js" | ||
], | ||
"files": [ | ||
"src/**/__tests__/**/*.test.js" | ||
], | ||
"failFast": true, | ||
"require": [ | ||
"babel-register" | ||
], | ||
"babel": "inherit" | ||
} | ||
} |
@@ -8,3 +8,3 @@ # react-class | ||
* auto-bind methods | ||
* avoid boilerplate with default style and default class name | ||
* optimized to only auto-bind non-lifecycle methods | ||
@@ -23,10 +23,6 @@ ## Install | ||
import Component from 'react-class' | ||
// or import { Component } from 'react-class | ||
class MyApp extends Component { | ||
render(){ | ||
// you have to call prepareProps in order to get defaultClassName | ||
// and defaultStyle applied to props | ||
var props = this.prepareProps(this.props) | ||
render() { | ||
return <div {...props} onClick={this.onClick}> | ||
@@ -39,50 +35,6 @@ //onClick is auto-bound to "this", so you can keep your code dry | ||
console.log(this) | ||
// this is correctly bound to the component instance | ||
} | ||
} | ||
MyApp.defaultProps = { | ||
defaultStyle: { | ||
border: '2px solid red' | ||
}, | ||
defaultClassName: 'myapp' | ||
} | ||
``` | ||
So you can use `<MyApp style={{color: 'blue'}} className="main" />` and get `defaultProps.defaultClassName` always applied to your component and `defaultProps.defaultStyle` merged into `props.style`. Of course, any colliding style you specify in `props.style` will override the one in `defaultProps.defaultStyle` | ||
The result of | ||
```jsx | ||
<MyApp style={{color: 'blue'}} className="main" /> | ||
``` | ||
is a div with the following: | ||
```html | ||
<div style="color: blue; border: 2px solid red" class="myapp main"> | ||
</div> | ||
``` | ||
## prepareProps | ||
To get `defaultProps.defaultStyle` and `defaultProps.defaultClassName` applied on the props object, remember to call **prepareProps** | ||
```js | ||
var props = this.prepareProps(this.props) | ||
``` | ||
All it does is the following: | ||
```jsx | ||
function prepareProps(thisProps){ | ||
var props = assign({}, thisProps) | ||
props.style = assign({}, | ||
this.contructor.defaultProps.defaultStyle, props.style | ||
) | ||
props.className = (props.className || '') + ' ' + | ||
(this.constructor.defaultProps.defaultClassName || '') | ||
return props | ||
} | ||
``` | ||
## auto-binding | ||
@@ -104,8 +56,4 @@ | ||
Generally you want your components to have a default style (of course, which can be overriden). | ||
Autobinding, which is a nice-to-have feature! | ||
Very often you also want a default `className` to be applied all the time to your components, no matter if the user of your components passes a `className` attribute or not in the props. | ||
Also, autobinding is a nice feature! | ||
### What if I want to remove it in the future? | ||
@@ -112,0 +60,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
338
1
15992
3
15
11
65
1
+ Addedreact-dom@19.0.0(transitive)
+ Addedscheduler@0.25.0(transitive)
Updatedobject-assign@^4.1.0