Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
Merge 4386311 into ea96cfa
Browse files Browse the repository at this point in the history
  • Loading branch information
ianobermiller committed May 13, 2015
2 parents ea96cfa + 4386311 commit 3d6f424
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 16 deletions.
4 changes: 2 additions & 2 deletions modules/__tests__/resolve-styles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ describe('resolveStyles', function () {
});

describe('React.Children.only', function () {
it.only('doesn\'t break React.Children.only', function () {
it('doesn\'t break React.Children.only', function () {
var component = genComponent();
var renderedElement = <div><span /></div>;

Expand All @@ -728,7 +728,7 @@ describe('resolveStyles', function () {
expect(React.Children.only(result.props.children)).toBeTruthy();
});

it.only('doesn\'t break when only child isn\'t ReactElement', function () {
it('doesn\'t break when only child isn\'t ReactElement', function () {
var component = genComponent();
var renderedElement = <div>Foo</div>;

Expand Down
1 change: 1 addition & 0 deletions modules/__tests__/wrap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'use strict';

jest.dontMock('../wrap.js');
jest.dontMock('../wrap-utils.js');

var resolveStyles = require('../resolve-styles.js');
var wrap = require('../wrap.js');
Expand Down
43 changes: 43 additions & 0 deletions modules/enhancer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

var resolveStyles = require('./resolve-styles.js');
var wrapUtils = require('./wrap-utils.js');

var enhanceWithRadium = function (ComposedComponent) {
var RadiumEnhancer = function () {
ComposedComponent.prototype.constructor.call(this);

if (!this.state) {
this.state = {};
}

var radiumInitialState = wrapUtils.getInitialState();
Object.keys(radiumInitialState).forEach(function (key) {
this.state[key] = radiumInitialState[key];
}, this);
};

RadiumEnhancer.prototype = new ComposedComponent();
RadiumEnhancer.prototype.constructor = RadiumEnhancer;

RadiumEnhancer.prototype.render = function () {
var renderedElement = ComposedComponent.prototype.render.call(this);
return resolveStyles(this, renderedElement);
};

RadiumEnhancer.prototype.componentWillUnmount = function () {
if (ComposedComponent.prototype.componentWillUnmount) {
ComposedComponent.prototype.componentWillUnmount.call(this);
}

wrapUtils.componentWillUnmount(this);
};

RadiumEnhancer.defaultProps = ComposedComponent.defaultProps;
RadiumEnhancer.propTypes = ComposedComponent.propTypes;
RadiumEnhancer.contextTypes = ComposedComponent.contextTypes;

return RadiumEnhancer;
};

module.exports = enhanceWithRadium;
1 change: 1 addition & 0 deletions modules/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

exports.Enhancer = require('./enhancer');
exports.Style = require('./components/style');
exports.getState = require('./get-state');
exports.keyframes = require('./keyframes');
Expand Down
22 changes: 22 additions & 0 deletions modules/wrap-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

module.exports = {
getInitialState: function () {
return {_radiumStyleState: {}};
},

componentWillUnmount: function (component) {
if (component._radiumMouseUpListener) {
component._radiumMouseUpListener.remove();
}

if (component._radiumMediaQueryListenersByQuery) {
Object.keys(component._radiumMediaQueryListenersByQuery).forEach(
function (query) {
component._radiumMediaQueryListenersByQuery[query].remove();
},
component
);
}
}
};
18 changes: 4 additions & 14 deletions modules/wrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var resolveStyles = require('./resolve-styles.js');
var wrapUtils = require('./wrap-utils.js');

var merge = require('lodash/object/merge');

Expand All @@ -10,24 +11,13 @@ var wrap = function (config) {
var existingInitialState = config.getInitialState ?
config.getInitialState.call(this) :
{};
return merge({}, existingInitialState, {_radiumStyleState: {}});
var radiumInitialState = wrapUtils.getInitialState();
return merge({}, existingInitialState, radiumInitialState);
},

componentWillUnmount: function () {
config.componentWillUnmount && config.componentWillUnmount.call(this);

if (this._radiumMouseUpListener) {
this._radiumMouseUpListener.remove();
}

if (this._radiumMediaQueryListenersByQuery) {
Object.keys(this._radiumMediaQueryListenersByQuery).forEach(
function (query) {
this._radiumMediaQueryListenersByQuery[query].remove();
},
this
);
}
wrapUtils.componentWillUnmount(this);
},

render: function () {
Expand Down

0 comments on commit 3d6f424

Please sign in to comment.