Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: mui-themeable higher order component #2493

Merged
merged 1 commit into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will have no name, please add a displayName member 😁


export default Divider;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could just write

export default muiThemeable(Divider);

to keep Divider a const. Just a thought :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that originally (and wanted to keep it as const)! But I think our eslinter didn't like that for some reason...let me try again and get the actual error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, not an eslint error, there is a runtime error saying Uncaught Error: No suitable component definition found.

Will have to investigate this as well.

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;
}