【发布时间】:2021-10-21 05:50:17
【问题描述】:
我是新手,请帮助我澄清这个问题。 我想在选择另一个部分时取消选中部分的收音机。我正在使用材料 ui radioGroup。请使用代码沙箱链接进行代码更改。 https://codesandbox.io/s/5sdne?file=/demo.js
import * as React from 'react';
import PropTypes from 'prop-types';
import { styled } from '@mui/material/styles';
import RadioGroup, { useRadioGroup } from '@mui/material/RadioGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import Radio from '@mui/material/Radio';
const StyledFormControlLabel = styled((props) => <FormControlLabel {...props} />)(
({ theme, checked }) => ({
'.MuiFormControlLabel-label': checked && {
color: theme.palette.primary.main,
},
}),
);
function MyFormControlLabel(props) {
const radioGroup = useRadioGroup();
let checked = false;
if (radioGroup) {
checked = radioGroup.value === props.value;
}
return <StyledFormControlLabel checked={checked} {...props} />;
}
MyFormControlLabel.propTypes = {
/**
* The value of the component.
*/
value: PropTypes.any,
};
export default function UseRadioGroup() {
return (
<>
<p>group A</p>
<RadioGroup name="use-radio-group" defaultValue="first">
<MyFormControlLabel value="first" label="First" control={<Radio />} />
<MyFormControlLabel value="second" label="Second" control={<Radio />} />
</RadioGroup>
<p>group B </p>
<RadioGroup name="use-radio-group" defaultValue="first">
<MyFormControlLabel value="three" label="three" control={<Radio />} />
<MyFormControlLabel value="four" label="four" control={<Radio />} />
</RadioGroup>
</>
);
【问题讨论】:
-
我在您的代码沙箱中只看到一个无线电组(RadioGroup)。你想有两个电台组,然后在点击A组的电台时,清除B组的所有电台吗?
-
是的,当点击 A 组上的收音机时,我想要两个收音机组并清除组 B 中的收音机
标签: reactjs material-ui react-hook-form