6

i am trying to align different Material UI select components in my application.

in the image you can see that the components are not aligned.

how can i give these components the same style, so that they line up next to each other.

This is the code that i'm using for the button that is not on the same line as the other components(see image):

import React, { useEffect, useState, FunctionComponent, useRef, useCallback } from 'react'
import { Toolbar, Snackbar, Box, Grid, TextField, Select, MenuItem, Button, FormControl, InputLabel, Checkbox, Input, ListItemText } from '@material-ui/core'
import { RouteComponentProps } from '@reach/router'
import { useStyles } from '../Common/Style.css'
import ChartElement from '../Components/Chart'

const ITEM_HEIGHT = 48;
const ITEM_PADDING_TOP = 8;
const MenuProps = {
    PaperProps: {
        style: {
            maxHeight: ITEM_HEIGHT * 4.5 + ITEM_PADDING_TOP,
            width: 250,
        },
    },
};

                            <FormControl className={classes.formControl}>
                            <InputLabel id="demo-mutiple-checkbox-label">kerk</InputLabel>
                            <Select
                                labelId="demo-mutiple-checkbox-label"
                                id="demo-mutiple-checkbox"
                                multiple
                                value={personName}
                                onChange={onChangeKerk}
                                input={<Input />}
                                renderValue={(selected) => (selected as string[]).join(', ')}
                                MenuProps={MenuProps}
                            >
                                {churches.map((name) => (
                                    <MenuItem key={name} value={name}>
                                        <Checkbox checked={personName.indexOf(name) > -1} />
                                        <ListItemText primary={name} />
                                    </MenuItem>
                                ))}
                            </Select>
                        </FormControl>

this is the code that i'm using for the buttons that are on one line with the others(see image):

                            <TextField
                            id="Startdatum"
                            label="Startdatum"
                            onChange={onChangeStartDatum}
                            defaultValue={startDatum}
                            type="date"
                            InputLabelProps={{
                                shrink: true,
                            }}
                        />
                        <TextField
                            id="Einddatum"
                            label="Einddatum"
                            onChange={onChangeEindDatum}
                            defaultValue={eindDatum}
                            type="date"
                            InputLabelProps={{
                                shrink: true,
                            }}
                        />
                        <FormControl className={classes.formControl}>
                            <InputLabel htmlFor="age-native-simple">Maanden</InputLabel>
                            <Select
                            native
                            value={currentOption}
                            onChange={onChangeFixedDate}
                            inputProps={{
                                name: 'age',
                                id: 'age-native-simple',
                            }}
                            >
                            <option value={"All"}>All</option>
                            <option value={"3 months"}>3 months</option>
                            </Select>
                        </FormControl>

update after adding grids to code:

<Grid justify="center" alignItems="center" direction="row">
            <Toolbar className={classes.toolbar}>
                <Grid item lg={6} md={12} xs={12}>
                    <h4 className={classes.title}>{"DASHBOARD"}</h4>
                </Grid>

         
                <Grid item lg={1} md={12} xs={12}>
                    <FormControl className={classes.formControl}>
                        <Select
                            labelId="demo-mutiple-checkbox-label"
                            id="demo-mutiple-checkbox"
                            multiple
                            value={churchName}
                            onChange={onChangeChurch}
                            input={<Input />}
                            renderValue={(selected) => (selected as string[]).join(', ')}
                            MenuProps={MenuProps}
                        >
                            {churches.map((name) => (
                                <MenuItem key={name} value={name}>
                                    <Checkbox checked={churchName.indexOf(name) > -1} />
                                    <ListItemText primary={name} />
                                </MenuItem>
                            ))}
                        </Select>
                    </FormControl>
                </Grid>
                <Grid item lg={12} md={12} xs={10}>
                    <TextField
                        id="Startdatum"
                        label="Startdatum"
                        onChange={onChangeStartDatum}
                        value={startDatum}
                        type="date"
                        InputLabelProps={{
                            shrink: true,
                        }}
                    />
                    <TextField
                        id="Einddatum"
                        label="Einddatum"
                        onChange={onChangeEindDatum}
                        value={eindDatum}
                        type="date"
                        InputLabelProps={{
                            shrink: true,
                        }}
                    />
                </Grid>
                <Grid item lg={8} md={12} xs={12}>
                    <FormControl className={classes.formControl}>
                        <InputLabel htmlFor="age-native-simple">Maanden</InputLabel>
                        <Select
                            native
                            value={currentOption}
                            onChange={onChangeFixedDate}
                            inputProps={{
                                name: 'age',
                                id: 'age-native-simple',
                            }}
                        >
                            <option value={"All"}>All</option>
                            <option value={"3 months"}>3 months</option>
                        </Select>
                    </FormControl>
                </Grid>
                {/*<Button id="refresh" onClick={onClick} variant="contained" color="primary">
                            ↺
                        </Button>*/}
            </Toolbar>
        </Grid>

My components now look like this:(see image)

image 2

after some adjustment to the width en height, i still can't get the component on the left in lign with the others. Any idea how to fix this?

Ruben
  • 61
  • 1
  • 4

1 Answers1

2

You have imported the <Grid> element, but you are not using it. Why not? Surround the items you want to align into a <Grid> element, and pass the justify, alignItems and direction attributes. Example code snippet that will align items both vertically and horizontally:

<Grid justify="center" alignItems="center" direction="row">
     --- your code and components go here---
</Grid>

The complete documentation for the <Grid> element can be found here, on the Material UI website:

https://material-ui.com/components/grid/

You can additionally specify the total relative width of each element by passing the item attribute into the <Grid> element, relative for different viewports (large, medium and small):

<Grid item lg={6} md={12} xs={12}>
      --- your code and components go here---
</Grid>
dmanexe
  • 1,034
  • 4
  • 16
  • 40
  • 1
    after adding the grids to my code and some adjustment to the width en height, i still can't get the component on the left in lign with the others. Any idea how to fix this? ( i updated the question with my current code en image) – Ruben Apr 20 '21 at 15:27
  • It might be helpful to wrap all the `` items into a `` object. – dmanexe Apr 22 '21 at 13:34