【问题标题】:Material UI grid not displayed correctly in typescript材质 UI 网格在打字稿中未正确显示
【发布时间】:2018-08-23 10:02:36
【问题描述】:

我想使用打字稿在反应中重现grid from material-ui。现场演示可以在here找到。

我已经调整了示例以使用打字稿,这样我的 demo.tsx 看起来像:

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

正如您在live demo 中看到的,它看起来与原始的 javascript 示例不同。这三个盒子不是水平对齐的,而是垂直堆叠的。我是否遗漏了特定于打字稿的内容?

干杯!

【问题讨论】:

    标签: reactjs typescript material-ui


    【解决方案1】:

    它不是特定于打字稿的。 muigrid-spacing 元素中的宽度被“演示”样式覆盖,将其注释掉可以解决您的问题。

    fixed demo

    【讨论】:

    • 添加了正确的链接@Sisir
    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2019-12-09
    • 1970-01-01
    • 2019-04-13
    • 2019-10-05
    相关资源
    最近更新 更多