Material-ui 有条件禁用无线电

You*_*ung 6 javascript material-ui

我正在使用 Material-ui 构建一个 React 应用程序。我想在RadioGroup某个事件发生时禁用所有单选按钮,并在事件消失时重新启用它们。(假设当我单击一个按钮时,所有无线电都被禁用,当我再次单击同一个按钮时,所有无线电都被重新启用。)我有以下带有三元运算符的条件渲染片段,它可以完成这项工作,但它看起来确实多余。有一个更好的方法吗?又名。有没有办法将disableMaterial-ui 组件的 prop(此处)变成变量?谢谢!

                        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>
Run Code Online (Sandbox Code Playgroud)

95f*_*973 5

以下是我消除冗余的两个选择:

第一个选项是您可以选择删除三元条件渲染,而是disabled根据条件渲染道具,例如disabled={radioDisabled}

const [radioDisabled, setRadioDisabled] = React.useState(false);

<FormControlLabel
  disabled={radioDisabled}
  value="1"
  checked={value === "1"}
  control={<Radio />}
  label="1"
/>
Run Code Online (Sandbox Code Playgroud)

第二个选项是您可以迭代无线电输入的值/标签,然后根据条件再次评估是否需要禁用

const [radioDisabled, setRadioDisabled] = React.useState(false);

const radioInputs = [
  {
    value: 1,
    label: 1
  },
  {
    value: 2,
    label: 2
  },
  {
    value: 3,
    label: 3
  }
];

{radioInputs.map((radioInput) => {
  return (
     <FormControlLabel
       disabled={radioDisabled}
       value={radioInput.value}
       checked={value == radioInput.value}
       control={<Radio />}
       label={radioInput.label}
     />
  );
})}
Run Code Online (Sandbox Code Playgroud)

CodeSandBox:https://codesandbox.io/s/patent-worker-8mrq3? file=/src/App.js:1717-2041