【发布时间】:2021-02-15 11:55:59
【问题描述】:
我已阅读文档,但我在 Material UI 中如何使用样式时遇到了困难。
基本上我有以下无线电组组件:
import React from 'react'
import Radio from '@material-ui/core/Radio'
import RadioGroup from '@material-ui/core/RadioGroup'
import FormControlLabel from '@material-ui/core/FormControlLabel'
import FormControl from '@material-ui/core/FormControl'
import FormLabel from '@material-ui/core/FormLabel'
import { makeStyles } from '@material-ui/core/styles'
const useStyles = makeStyles((theme) => ({
root: {
'& .MuiFormLabel-root': {
color: 'red',
},
},
formControl: {
margin: theme.spacing(3),
},
}))
const RadioInput = (props) => {
const classes = useStyles()
const { label, value, setValue, name, inputs } = props
return (
<FormControl component="fieldset" className={classes.root}>
<FormLabel component="legend">{label}</FormLabel>
<RadioGroup
aria-label={name}
name={name}
value={value}
onChange={(e) => setValue(e.target.value)}
row
>
{inputs.map((x, index) => {
return (
<FormControlLabel
key={index}
value={x.toLowerCase()}
control={<Radio />}
label={x}
/>
)
})}
</RadioGroup>
</FormControl>
)
}
export default RadioInput
现在,我还有一个用于文本字段的组件,如下所示:
import React from 'react'
import { TextField } from '@material-ui/core'
import { makeStyles } from '@material-ui/core/styles'
const useStyles = makeStyles((theme) => ({
root: {
'& .MuiTextField-root': {
margin: theme.spacing(1),
width: 300,
},
},
}))
const TextInput = (props) => {
const classes = useStyles()
const { label, value, setValue, error, type, helperText } = props
return (
<div className={classes.root}>
<TextField
label={label}
error={!!error ? true : false}
value={value}
onChange={(e) => setValue(e.target.value)}
helperText={!!error ? error : helperText}
type={type}
/>
</div>
)
}
export default TextInput
问题是,将这些放在一起我得到以下丑陋的显示:
如您所见,单选按钮位于上方文本字段的右侧。我想要的是要对齐的标签,以及与标签对齐的单选按钮。
有人可以帮我吗?我花了一些时间查看文档,但没有任何进展。
【问题讨论】:
-
RadioGroup 的边距为 24px (
theme.spacing(3)),TextField 的边距为 8px (theme.spacing(1))。 -
谢谢@RyanCogswell,知道如何解决这个问题吗?我似乎无法从文档中弄清楚。将
theme.spacing(3)更改为theme.spacing(1)无效。 -
请提供code sandbox 重现您的问题。
-
感谢@RyanCogswell,我在这里创建了它。我似乎已经修复了标签的对齐方式,但单选按钮仍然略微缩进:codesandbox.io/s/young-lake-6he87
标签: css reactjs material-ui textfield