Commit 58c2d138 authored by Olivier Tassinari's avatar Olivier Tassinari Committed by Tim Neutkens

examples(material-ui): change the wrapping strategy (#2648)

By creating an intermediary component (with the HOC)
we save some React rendering logic.
That's also addressing an issue with JssProvider (react-jss)
that generate a new index at each render.
parent 051ee314
import React, { Component } from 'react' import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { JssProvider } from 'react-jss' import { JssProvider } from 'react-jss'
import { withStyles, createStyleSheet, MuiThemeProvider } from 'material-ui/styles' import { withStyles, createStyleSheet, MuiThemeProvider } from 'material-ui/styles'
import { getContext } from '../styles/context' import { getContext } from '../styles/context'
// Apply some reset // Apply some reset
const styleSheet = createStyleSheet('App', theme => ({ const styleSheet = createStyleSheet(theme => ({
'@global': { '@global': {
html: { html: {
background: theme.palette.background.default, background: theme.palette.background.default,
...@@ -22,32 +21,42 @@ let AppWrapper = props => props.children ...@@ -22,32 +21,42 @@ let AppWrapper = props => props.children
AppWrapper = withStyles(styleSheet)(AppWrapper) AppWrapper = withStyles(styleSheet)(AppWrapper)
class App extends Component { function withRoot (BaseComponent) {
componentDidMount () { class WithRoot extends Component {
// Remove the server-side injected CSS. static getInitialProps (ctx) {
const jssStyles = document.querySelector('#jss-server-side') if (BaseComponent.getInitialProps) {
if (jssStyles && jssStyles.parentNode) { return BaseComponent.getInitialProps(ctx)
jssStyles.parentNode.removeChild(jssStyles) }
return {}
}
componentDidMount () {
// Remove the server-side injected CSS.
const jssStyles = document.querySelector('#jss-server-side')
if (jssStyles && jssStyles.parentNode) {
jssStyles.parentNode.removeChild(jssStyles)
}
} }
}
render () { render () {
const context = getContext() const context = getContext()
return ( return (
<JssProvider registry={context.sheetsRegistry} jss={context.jss}> <JssProvider registry={context.sheetsRegistry} jss={context.jss}>
<MuiThemeProvider theme={context.theme} sheetsManager={context.sheetsManager}> <MuiThemeProvider theme={context.theme} sheetsManager={context.sheetsManager}>
<AppWrapper> <AppWrapper>
{this.props.children} <BaseComponent />
</AppWrapper> </AppWrapper>
</MuiThemeProvider> </MuiThemeProvider>
</JssProvider> </JssProvider>
) )
}
} }
}
App.propTypes = { WithRoot.displayName = `withRoot(${BaseComponent.displayName})`
children: PropTypes.node.isRequired
return WithRoot
} }
export default App export default withRoot
...@@ -7,7 +7,7 @@ import Dialog, { ...@@ -7,7 +7,7 @@ import Dialog, {
DialogActions DialogActions
} from 'material-ui/Dialog' } from 'material-ui/Dialog'
import Typography from 'material-ui/Typography' import Typography from 'material-ui/Typography'
import App from '../components/App' import withRoot from '../components/withRoot'
const styles = { const styles = {
container: { container: {
...@@ -35,26 +35,24 @@ class Index extends Component { ...@@ -35,26 +35,24 @@ class Index extends Component {
render () { render () {
return ( return (
<App> <div style={styles.container}>
<div style={styles.container}> <Dialog open={this.state.open} onRequestClose={this.handleRequestClose}>
<Dialog open={this.state.open} onRequestClose={this.handleRequestClose}> <DialogTitle>Super Secret Password</DialogTitle>
<DialogTitle>Super Secret Password</DialogTitle> <DialogContent>
<DialogContent> <DialogContentText>1-2-3-4-5</DialogContentText>
<DialogContentText>1-2-3-4-5</DialogContentText> </DialogContent>
</DialogContent> <DialogActions>
<DialogActions> <Button color='primary' onClick={this.handleRequestClose}>OK</Button>
<Button color='primary' onClick={this.handleRequestClose}>OK</Button> </DialogActions>
</DialogActions> </Dialog>
</Dialog> <Typography type='display1' gutterBottom>Material-UI</Typography>
<Typography type='display1' gutterBottom>Material-UI</Typography> <Typography type='subheading' gutterBottom>example project</Typography>
<Typography type='subheading' gutterBottom>example project</Typography> <Button raised color='accent' onClick={this.handleClick}>
<Button raised color='accent' onClick={this.handleClick}> Super Secret Password
Super Secret Password </Button>
</Button> </div>
</div>
</App>
) )
} }
} }
export default Index export default withRoot(Index)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment