Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Create A 2 Column Form WIth React Material-UI?

I am creating a react form with material-ui.

I would like the form to be a fixed 2 column view and not flow based on the browser size so it is always like this:

|First Name      |Last Name|
|Street Address  |City     |

And not end up like

|First Name      |Last Name|  Street Address  |City     |

https://codesandbox.io/s/0q7kw76nyl

import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";

const styles = theme => ({
  textField: {
    marginLeft: theme.spacing.unit,
    marginRight: theme.spacing.unit
  }
});

class TextFields extends React.Component {
  state = {
    firstName: "",
    lastName: "",
    street: "",
    city: ""
  };

  handleChange = name => event => {
    this.setState({ [name]: event.target.value });
  };

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

    return (
      <form className={classes.container} noValidate autoComplete="off">
        <TextField
          id="first-name"
          label="First Name"
          className={classes.textField}
          value={this.state.firstName}
          onChange={this.handleChange("firstName")}
          margin="normal"
        />

        <TextField
          id="last-name"
          label="Last Name"
          className={classes.textField}
          value={this.state.lastName}
          onChange={this.handleChange("lastName")}
          margin="normal"
        />

        <TextField
          id="address-street"
          label="Street Address"
          className={classes.textField}
          value={this.state.street}
          onChange={this.handleChange("street")}
          margin="normal"
        />

        <TextField
          id="address-city"
          label="City"
          className={classes.textField}
          value={this.state.city}
          onChange={this.handleChange("city")}
          margin="normal"
        />
      </form>
    );
  }
}

TextFields.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(TextFields);
like image 935
opticyclic Avatar asked Dec 03 '22 18:12

opticyclic


2 Answers

As above, putting into divs work great.

You can also put them inside a Grid layout for additional customization.

The grid layout has some neat additional such as spacing and justification of items, customizable items width, here's the documentation.

import Grid from '@material-ui/core/Grid'
<Grid container>
     <Grid item xs={6}> 
         ... 
     </Grid>
     <Grid item xs={6}>
         ...
     </Grid>
</Grid>
like image 188
schapke Avatar answered Dec 06 '22 11:12

schapke


You can use both Grid container and item at the same time.

<Grid container spacing={2} >
  <Grid container item xs={6} direction="column" >
    <TextField />
    <TextField />
    <TextField />
  </Grid>
  <Grid container item xs={6} direction="column" >
    <TextField />
    <TextField />
  </Grid>
</Grid>
like image 32
Eduardo Oviedo Blanco Avatar answered Dec 06 '22 12:12

Eduardo Oviedo Blanco