首页 > 解决方案 > Material-UI:无法在网格方向内调整网格项目的大小=列 alignItems="center"

问题描述

我正在创建一个 Griddirection="column"alignItems="center"。我的子网格项目的大小固定,我无法使用xs={...}.

以下代码生成具有相同宽度的文本字段。删除方向或 alignItems 将网格调整为我分配的 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 的预期行为。也许任何人都知道使用方向或 alignItems 的解决方法。

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

标签: javascriptreactjsmaterial-ui

解决方案


谢谢瑞恩·科格斯韦尔。我已经使用多个容器重写了我的代码。

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

推荐阅读