【问题标题】:How to align several different Material UI select components?如何对齐几个不同的 Material UI 选择组件?
【发布时间】:2021-04-20 12:54:25
【问题描述】:

我正在尝试在我的应用程序中对齐不同的 Material UI 选择组件。

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

我怎样才能给这些组件相同的样式,使它们彼此相邻排列。

这是我用于按钮的代码,它与其他组件不在同一行(见图):

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>

这是我用于与其他按钮在一行中的代码(见图):

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

将网格添加到代码后更新:

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

我的组件现在看起来像这样:(见图)

在对宽度和高度进行了一些调整后,我仍然无法让左侧的组件与其他组件对齐。知道如何解决这个问题吗?

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    您已导入 &lt;Grid&gt; 元素,但您没有使用它。为什么不?将要对齐的项目包围成&lt;Grid&gt; 元素,并传递justifyalignItemsdirection 属性。示例代码 sn-p 将垂直和水平对齐项目:

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

    &lt;Grid&gt; 元素的完整文档可以在 Material UI 网站上找到:

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

    您可以通过将item 属性传递给&lt;Grid&gt; 元素来额外指定每个元素的总相对宽度,相对于不同的视口(大、中和小):

    <Grid item lg={6} md={12} xs={12}>
          --- your code and components go here---
    </Grid>
    

    【讨论】:

    • 在我的代码中添加了网格并对宽度和高度进行了一些调整后,我仍然无法让左侧的组件与其他组件对齐。知道如何解决这个问题吗? (我用我当前的代码更新了问题)
    • 将所有 &lt;Grid&gt; 项目包装到 &lt;Container&gt; 对象中可能会有所帮助。
    猜你喜欢
    • 2021-05-23
    • 1970-01-01
    • 2021-04-03
    • 2020-07-06
    • 1970-01-01
    • 2020-07-05
    • 2021-02-01
    • 2016-10-11
    • 2021-02-15
    相关资源
    最近更新 更多