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);
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With