├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── scripts └── test ├── src └── mixin.js └── tests └── mixin.spec.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | lib/ 3 | **/.DS_Store 4 | *.tgz 5 | tmp_test 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src/ 2 | tests/ 3 | karma.conf.js 4 | .travis.yml 5 | lib/*.map 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "4.0" 4 | before_script: 5 | - "npm install" 6 | - "export DISPLAY=:99.0" 7 | - "sh -e /etc/init.d/xvfb start" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 angus croll 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # es6-react-mixins 2 | 3 | [](http://travis-ci.org/angus-c/es6-react-mixins) [](https://npmjs.org/package/es6-react-mixins) 4 | 5 | `es6-react-mixins` is a module that lets you augment your ES6 React component classes with any number of custom ES6 mixins. You can also use it to merge traditional pre-ES6 React mixin objects into your ES6 React classes. 6 | 7 | Inspired by [this gist](https://gist.github.com/sebmarkbage/fac0830dbb13ccbff596) by [Sebastian Markbåge](https://github.com/sebmarkbage) the strategy is transient class hierarchies – instead of locking classes into permanent *is a* roles, class realtionships are assembled and re-assembled at will. 8 | 9 | ES6 mixins are functions that return classes. The `base` parameter is used internally to construct the mixin chain. There's no need to extend `React.component`, you get that for free. 10 | 11 | ```js 12 | const es6Mixin = base => class extends base { 13 | componentWillMount() { 14 | console.log("augmented componentWillMount"); 15 | } 16 | render() { 17 | console.log("augmented render"); 18 | } 19 | }; 20 | ``` 21 | 22 | React components invoke mixins with a call to `super`. 23 | 24 | ```js 25 | import mixin from 'es6-react-mixins'; 26 | import React from 'react'; 27 | 28 | class MyComponent extends mixin(es6Mixin) { 29 | componentWillMount() { 30 | super.componentWillMount(); 31 | } 32 | render() { 33 | super.render(); 34 | return
Test
62 | ); 63 | } 64 | } 65 | 66 | ReactTestUtils.renderIntoDocument(Test
86 | ); 87 | } 88 | } 89 | 90 | ReactTestUtils.renderIntoDocument(Test
109 | ); 110 | } 111 | } 112 | 113 | ReactTestUtils.renderIntoDocument(Test
122 | ); 123 | } 124 | } 125 | 126 | const testInstance = new Test({}); 127 | assert.ok(testInstance.shouldComponentUpdate()); 128 | }); 129 | }); 130 | 131 | describe('static property mixin support', () => { 132 | jsdom(); 133 | 134 | const propTypes = {'test': true}; 135 | 136 | const es6Mixin = base => { 137 | class newClass extends base {}; 138 | newClass.propTypes = propTypes; 139 | return newClass; 140 | } 141 | 142 | const es5Mixin = { 143 | propTypes: propTypes 144 | }; 145 | 146 | it('upgrades ES6 properties', () => { 147 | class Test extends mixin(es6Mixin) {} 148 | assert.equal(Test.propTypes, propTypes); 149 | }); 150 | 151 | it('upgrades ES5 properties', () => { 152 | class Test extends mixin(es5Mixin) {} 153 | assert.deepEqual(Test.propTypes, propTypes); 154 | }) 155 | }); 156 | 157 | describe('mergeStaticProps', () => { 158 | jsdom(); 159 | 160 | const mixin1 = { 161 | propTypes: {a: 1} 162 | }; 163 | const mixin2 = (Base) => { 164 | class NewClass extends Base {} 165 | NewClass.propTypes = { 166 | ...Base.propTypes, 167 | a: 2, 168 | b: 1 169 | }; 170 | return NewClass; 171 | }; 172 | 173 | it('merges ES7 static props of final class from its base class', () => { 174 | class Test extends mixin(mixin2, mixin1) { 175 | static propTypes = {a: 0, c: 3}; 176 | } 177 | mergeStaticProps(Test); 178 | assert.equal(Test.propTypes.a, 2); 179 | assert.equal(Test.propTypes.b, 1); 180 | assert.equal(Test.propTypes.c, 3); 181 | }); 182 | }); 183 | --------------------------------------------------------------------------------