我正在使用material-ui构建一个React应用程序。当发生某个事件并且该事件消失时,我想禁用RadioGroup
中的所有单选按钮,并在事件消失时重新启用它们。(例如,当我点击一个按钮时,所有单选按钮都被禁用,当我再次点击同一个按钮时,所有单选按钮都会重新启用。) 我有以下条件渲染片段和三元运算符,可以完成工作,但看起来非常冗余。有没有更好的方法来完成这个功能?即,是否有一种方法可以将Material-ui组件的属性(disable
在这里)变成一个变量?谢谢!
const radioDisabled = false;
// Some mechanism here that could potentially
// change the value of radioDisabled
{ radioDisabled
?
<RadioGroup row
value={value}
onChange={(e)=>{setValue(e.target.value)}}
>
<FormControlLabel
value='1'
checked={value === '1'}
control={<Radio/>}
label='1'
/>
<FormControlLabel
value='2'
checked={value === '2'}
control={<Radio/>}
label='2'
/>
...
<FormControlLabel
value='n'
checked={value === 'n'}
control={<Radio/>}
label='n'
/>
</RadioGroup>
:
<RadioGroup row
value={value}
onChange={(e)=>{setValue(e.target.value)}}
>
<FormControlLabel
disabled // the only difference from above
value='1'
checked={value === '1'}
control={<Radio/>}
label='1'
/>
<FormControlLabel
disabled // the only difference from above
value='2'
checked={value === '2'}
control={<Radio/>}
label='2'
/>
...
<FormControlLabel
disabled // the only difference from above
value='n'
checked={value === 'n'}
control={<Radio/>}
label='n'
/>
</RadioGroup>