Material-UI:无法在Grid direction = column alignItems =“ center”内调整Grid项目的大小

时间:2019-05-23 20:23:21

标签: material-ui

当我创建一个带有direction = column和alignItems =“ center”的网格时。我的子网格项目的尺寸固定,无法使用xs = {...}进行更改。以下代码生成具有相同宽度的文本字段。删除direction或alignItems会将Grid的大小调整为我分配的xs。 我想拥有更宽的文本框,并且仍然将它们放在中间。

<Grid container spacing={2} direction="column" alignItems="center">
          <Grid item xs={4}>
            <TextField
              name="username"
              variant="outlined"
              required
              fullWidth
              id="username"
              label="Username"
              autoFocus
              value="peter"
            />
          </Grid>
          <Grid item xs={6}>
            <TextField
              variant="outlined"
              required
              fullWidth
              id="shortDescription"
              label="Short Description"
              name="shortDescription"
              value="I create awesome games"
            />
          </Grid>
</Grid>

我不确定这是bug还是Material-ui的预期行为。也许有人知道使用Direction或alignItems的解决方法。

您可以在CodeSandbox 上查看并测试该问题

1 个答案:

答案 0 :(得分:0)

感谢Ryan Cogswell。我已经使用多个容器重写了代码。

import React, { Component } from "react";
import Grid from "@material-ui/core/Grid";
import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
import { withStyles } from "@material-ui/core/styles";
import Avatar from "@material-ui/core/Avatar";
import Button from "@material-ui/core/Button";
import Container from "@material-ui/core/Container";

const styles = theme => ({
  debug: {
    border: "1px grey solid"
  },
  root: {
    marginTop: 32
  },
  fieldName: {},
  avatar: {
    width: 200,
    height: 200
  },
  submitButton: {}
});

class EditProfile extends Component {
  render() {
    const { classes } = this.props;

    return (
      <Container maxWidth="xs">
        <Grid container className={classes.root} justify="center">
          <Grid item>
            <Avatar
              item
              alt="Remy Sharp"
              src="https://picsum.photos/300/300"
              className={classes.avatar}
            />
          </Grid>
          <Grid item>
            <Typography variant="h5" align="center">
              Change Profile Photo
            </Typography>
          </Grid>
        </Grid>
        <Grid container spacing="2" className={classes.root}>
          <Grid item xs={12}>
            <TextField
              name="username"
              variant="outlined"
              required
              fullWidth
              id="username"
              label="Username"
              autoFocus
              value="jingyi4"
            />
          </Grid>
          <Grid item xs={12}>
            <TextField
              variant="outlined"
              required
              fullWidth
              id="shortDescription"
              label="Short Description"
              name="shortDescription"
              value="I create awesome games"
            />
          </Grid>
          <Grid item xs={12}>
            <TextField
              variant="outlined"
              required
              fullWidth
              id="longDescription"
              label="Long Description"
              name="longDescription"
              value={`One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. "What's happened to me?" he thought. It wasn't a dream. His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather. Drops "`}
              multiline
            />
          </Grid>
          <Grid container justify="center" className={classes.root}>
            <Button
              type="submit"
              variant="contained"
              color="primary"
              className={classes.submitButton}
              disabled
            >
              Submit
            </Button>
          </Grid>
        </Grid>
      </Container>
    );
  }
}

export default withStyles(styles)(EditProfile);
相关问题