Peterhack
Peterhack

Reputation: 1051

Material UI grid not displayed correctly in typescript

I would like to reproduce a grid from material-ui in react using typescript. A live demo can be found here.

I have adjusted the example to work with typescript such that my demo.tsx looks as:

import * as React from 'react';
import { withStyles } from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import FormLabel from '@material-ui/core/FormLabel';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import RadioGroup from '@material-ui/core/RadioGroup';
import Radio from '@material-ui/core/Radio';
import Paper from '@material-ui/core/Paper';
import { GridSpacing } from '@material-ui/core/Grid';
import { WithStyles } from "@material-ui/core/styles/withStyles";


const styles = (theme: any) => ({
  root: {
    flexGrow: 1,
  },
  paper: {
    height: 140,
    width: 100,
  },
  demo: {
    height: 140,
    width: 100,
  },
  control: {
    padding: theme.spacing.unit * 2,
  },
});

class Demo extends React.Component<WithStyles<typeof styles>> {
  state = {
    spacing: '16',
  };

  handleChange = (key: any) => (event: any, value: any) => {
    this.setState({
      [key]: value,
    });
  };

  render() {
    const { classes } = this.props;
    const { spacing } = this.state;

    return (
      <Grid container className={classes.root} spacing={16}>
        <Grid item xs={12}>
          <Grid container className={classes.demo} justify="center" spacing={Number(spacing) as GridSpacing}>
            {[0, 1, 2].map(value => (
              <Grid key={value} item>
                <Paper className={classes.paper} />
              </Grid>
            ))}
          </Grid>
        </Grid>
        <Grid item xs={12}>
          <Paper className={classes.control}>
            <Grid container>
              <Grid item>
                <FormLabel>spacing</FormLabel>
                <RadioGroup
                  name="spacing"
                  aria-label="Spacing"
                  value={spacing}
                  onChange={this.handleChange('spacing')}
                  row
                >
                  <FormControlLabel value="0" control={<Radio />} label="0" />
                  <FormControlLabel value="8" control={<Radio />} label="8" />
                  <FormControlLabel value="16" control={<Radio />} label="16" />
                  <FormControlLabel value="24" control={<Radio />} label="24" />
                  <FormControlLabel value="32" control={<Radio />} label="32" />
                  <FormControlLabel value="40" control={<Radio />} label="40" />
                </RadioGroup>
              </Grid>
            </Grid>
          </Paper>
        </Grid>
      </Grid>
    );
  }
}

export default withStyles(styles)(Demo);

As you can see in a live demo, it does not look the same as the original javascript example. The three boxes are not aligned horizontally but are stacked vertically. Am I missing something typscript-specific?

Cheers!

Upvotes: 0

Views: 2344

Answers (1)

Andy Theos
Andy Theos

Reputation: 3346

It is not a typescript-specific. Width in muigrid-spacing element was overridden by "demo" styles, commenting it out fixes your problem.

See fixed demo.

Upvotes: 1

Related Questions