https://codesandbox.io/s/9zxzj0r664
^在上面的演示中,您可以看到工作代码,无论如何都只是试图水平对齐单选按钮。
在Material-UI中有一种简单的方法吗?或者我必须设置自己的单选按钮,我可以在5分钟内完成。
import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import Radio from "@material-ui/core/Radio";
import RadioGroup from "@material-ui/core/RadioGroup";
import FormHelperText from "@material-ui/core/FormHelperText";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import FormControl from "@material-ui/core/FormControl";
import FormLabel from "@material-ui/core/FormLabel";
const styles = theme => ({
root: {
display: "flex"
},
formControl: {
float: "left",
margin: theme.spacing.unit * 3
},
group: {
margin: `${theme.spacing.unit}px 0`
}
});
class RadioButtonsGroup extends React.Component {
state = {
value: "female"
};
handleChange = event => {
this.setState({ value: event.target.value });
};
render() {
const { classes } = this.props;
return (
<div className={classes.root}>
<FormControl
component="fieldset"
required
className={classes.formControl}
>
<FormLabel component="legend">Gender</FormLabel>
<RadioGroup
aria-label="gender"
name="gender1"
className={classes.group}
value={this.state.value}
onChange={this.handleChange}
>
<FormControlLabel
value="female"
control={<Radio />}
label="Female"
/>
<FormControlLabel value="male" control={<Radio />} label="Male" />
<FormControlLabel value="other" control={<Radio />} label="Other" />
<FormControlLabel
value="disabled"
disabled
control={<Radio />}
label="(Disabled option)"
/>
</RadioGroup>
</FormControl>
<FormControl
component="fieldset"
required
error
className={classes.formControl}
>
<FormLabel component="legend">Gender</FormLabel>
<RadioGroup
aria-label="gender"
name="gender2"
className={classes.group}
value={this.state.value}
onChange={this.handleChange}
>
<FormControlLabel
value="male"
control={<Radio color="primary" />}
label="Male"
/>
<FormControlLabel
value="female"
control={<Radio color="primary" />}
label="Female"
/>
<FormControlLabel
value="other"
control={<Radio color="primary" />}
label="Other"
/>
<FormControlLabel
value="disabled"
disabled
control={<Radio />}
label="(Disabled option)"
/>
</RadioGroup>
<FormHelperText>You can display an error</FormHelperText>
</FormControl>
</div>
);
}
}
RadioButtonsGroup.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(RadioButtonsGroup);
答案 0 :(得分:2)
https://codesandbox.io/s/n03mo5nz2m
RadioGroup
元素包含单选按钮。如果你想连续渲染它们,最简单的方法可能是使用flexbox行。
你可以看到我在上面的代码框中做了什么,但它就像添加group
样式一样简单:
flexDirection: 'row',
这是有效的,因为Material-UI已经为组使用了flexbox样式。这也是它无法在那里添加float: 'left'
的原因。
您还可以在论坛上设置display: 'inline-block'
:
display: 'inline-block'
这也将实现行样式,但控制选项更少。
答案 1 :(得分:0)
我不认为有一个&#34;本地人&#34;在材质UI中执行此操作的方法,您必须自己执行此操作。事实上,这是一个更好的方法。
答案 2 :(得分:0)
https://codesandbox.io/s/yp84vnzpq1
import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import Radio from "@material-ui/core/Radio";
import RadioGroup from "@material-ui/core/RadioGroup";
import FormHelperText from "@material-ui/core/FormHelperText";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import FormControl from "@material-ui/core/FormControl";
import FormLabel from "@material-ui/core/FormLabel";
const styles = theme => ({
formControl: {
display: "flex",
float: "left",
margin: theme.spacing.unit * 3
},
group: {
float: "left",
flexDirection: "row",
margin: `${theme.spacing.unit}px 0`
}
});
class RadioButtonsGroup extends React.Component {
state = {
value: "male 1"
};
handleChange = event => {
this.setState({ value: event.target.value });
};
render() {
const { classes } = this.props;
return (
<div className={classes.root}>
<RadioGroup
aria-label="gender"
name="gender1"
className={classes.group}
value={this.state.value}
onChange={this.handleChange}
>
<FormControlLabel value="male 1" control={<Radio />} label="Male 1" />
<FormControlLabel value="male 2" control={<Radio />} label="Male 2" />
<FormControlLabel value="male 3" control={<Radio />} label="Male 3" />
</RadioGroup>
</div>
);
}
}
RadioButtonsGroup.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(RadioButtonsGroup);