Skip to content

Commit

Permalink
Merge pull request #2493 from newoga/mui-themeable-hoc
Browse files Browse the repository at this point in the history
Proposal: mui-themeable higher order component
  • Loading branch information
oliviertassinari committed Dec 16, 2015
2 parents 6ee490b + fa4f480 commit c8a5d45
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/divider.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import DefaultRawTheme from './styles/raw-themes/light-raw-theme';
import ThemeManager from './styles/theme-manager';
import muiThemeable from './mui-themeable';
import styleUtils from './utils/styles';

const propTypes = {
Expand All @@ -20,19 +19,11 @@ const propTypes = {
style: React.PropTypes.object,
};

const contextTypes = {
muiTheme: React.PropTypes.object,
};

const childContextTypes = {
muiTheme: React.PropTypes.object,
};

const defaultProps = {
inset: false,
};

const Divider = ({inset, style, ...other}, {muiTheme = ThemeManager.getMuiTheme(DefaultRawTheme)}) => {
let Divider = ({inset, muiTheme, style, ...other}) => {
const styles = {
root: {
margin: 0,
Expand All @@ -49,9 +40,9 @@ const Divider = ({inset, style, ...other}, {muiTheme = ThemeManager.getMuiTheme(
);
};

Divider.displayName = 'Divider';
Divider.propTypes = propTypes;
Divider.defaultProps = defaultProps;
Divider.contextTypes = contextTypes;
Divider.childContextTypes = childContextTypes;
Divider = muiThemeable(Divider);

export default Divider;
25 changes: 25 additions & 0 deletions src/mui-theme-provider.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {Component, PropTypes} from 'react';

class ThemeProvider extends Component {

static propTypes = {
children: PropTypes.element,
muiTheme: PropTypes.object,
};

static childContextTypes = {
muiTheme: PropTypes.object,
};

getChildContext() {
return {
muiTheme: this.props.muiTheme,
};
}

render() {
return this.props.children;
}
}

export default ThemeProvider;
23 changes: 23 additions & 0 deletions src/mui-themeable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import DefaultRawTheme from './styles/raw-themes/light-raw-theme';
import ThemeManager from './styles/theme-manager';

function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}

export default function muiThemeable(WrappedComponent) {
const MuiComponent = (props, {muiTheme = ThemeManager.getMuiTheme(DefaultRawTheme)}) => {
return <WrappedComponent {...props} muiTheme={muiTheme} />;
};

MuiComponent.displayName = getDisplayName(WrappedComponent);
MuiComponent.contextTypes = {
muiTheme: React.PropTypes.object,
};
MuiComponent.childContextTypes = {
muiTheme: React.PropTypes.object,
};

return MuiComponent;
}

0 comments on commit c8a5d45

Please sign in to comment.