Jingyi Wang
Jingyi Wang

Reputation: 956

Material-UI: Unable to resize Grid item inside Grid direction=column alignItems="center"

I'm creating a Grid with direction="column" and alignItems="center". My child grid items gets a fixed size and I am unable to change it with xs={...}.

The following code produces Textfields with the same width. Removing either direction or alignItems resizes the Grids to the xs I assigned. I'd like to have wider textfields and still keep them in the center.

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

I am not sure if this is a bug or a expected behaviour of material-ui. Maybe anyone knows a workaround using either direction or alignItems.

You can see and test the issue on CodeSandbox

Upvotes: 1

Views: 1654

Answers (1)

Jingyi Wang
Jingyi Wang

Reputation: 956

Thanks Ryan Cogswell. I've rewritten my code using multiple containers.

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

Upvotes: 2

Related Questions