【发布时间】:2020-09-24 22:08:46
【问题描述】:
我是 React js Material UI 设计的新手。 我想要的是如下所示。
如果我单击了第一个单选按钮,其值 1 应显示在所选单选按钮内。
我只能使用以下实现独立单选按钮。
import React from 'react';
import { withStyles } from '@material-ui/core/styles';
import { green } from '@material-ui/core/colors';
import Radio from '@material-ui/core/Radio';
const GreenRadio = withStyles({
root: {
color: green[400],
'&$checked': {
color: green[600],
},
},
checked: {},
})((props) => <Radio color="default" {...props} />);
export default function RadioButtons() {
const [selectedValue, setSelectedValue] = React.useState('a');
const handleChange = (event) => {
setSelectedValue(event.target.value);
};
return (
<div>
<Radio
checked={selectedValue === 'a'}
onChange={handleChange}
value="a"
name="radio-button-demo"
inputProps={{ 'aria-label': 'A' }}
/>
<Radio
checked={selectedValue === 'b'}
onChange={handleChange}
value="b"
name="radio-button-demo"
inputProps={{ 'aria-label': 'B' }}
/>
<GreenRadio
checked={selectedValue === 'c'}
onChange={handleChange}
value="c"
name="radio-button-demo"
inputProps={{ 'aria-label': 'C' }}
/>
<Radio
checked={selectedValue === 'd'}
onChange={handleChange}
value="d"
color="default"
name="radio-button-demo"
inputProps={{ 'aria-label': 'D' }}
/>
<Radio
checked={selectedValue === 'e'}
onChange={handleChange}
value="e"
color="default"
name="radio-button-demo"
inputProps={{ 'aria-label': 'E' }}
size="small"
/>
</div>
);
}
如何获取所选按钮内的值?
【问题讨论】:
-
我认为这是不可能的,因为
Radio组件使用了 input type="radio" HTML 原生元素。对于你想要的,你需要创建一个像收音机但呈现自定义元素的组件
标签: reactjs material-ui