[英]How do I make a Grid component clickable to check/uncheck checkbox?
I have a Grid component as a row among other grid rows within the grid container.我有一个 Grid 组件作为网格容器内其他网格行中的一行。 The grid row contains a description and a checkbox.网格行包含描述和复选框。 How do i make it so I can click anywhere on the row to be able to check/uncheck the checkbox?我如何做到这一点,以便我可以单击行上的任意位置以选中/取消选中复选框?
<label>
<Grid container xs={12} direction= "row" onClick={handleChange}>
<Grid item xs={6} md={8}>
<h4>Reverse The Online Gambling Ban</h4>
<p>The MPA comes with three different sizes of foam and silicone tips and carrying pouch.</p>
</Grid>
<Grid item xs={6} md={4} style={{textAlign: 'center', }}>
<Checkbox
style={{marginTop: "10%"}}
type="checkbox"
name="newsletter-1"
disabled={formData.optOut == true}
onChange={handleChange}/>
</Grid>
</Grid>
</label>
I am not sure why you want to use Grid But If you want to activate checkbox with by clicking on any text then you can use FormControlLabel我不确定你为什么要使用 Grid 但是如果你想通过单击任何文本来激活复选框,那么你可以使用 FormControlLabel
<FormControlLabel
control={<Checkbox checked={gilad} onChange={handleChange} name="gilad" />}
label="Gilad Gray"
/>
You can update the Checkbox component from uncontrolled to controlled.您可以将 Checkbox 组件从不受控制更新为受控制。
First, create a state and call checkboxState
as Boolean
.首先,创建一个 state 并调用checkboxState
作为Boolean
。
Now, inside your 'handleChange` function toggle the state.现在,在您的“handleChange”中 function 切换 state。
Second, add checked={checkboxState}
as a prop to the Checkbox component.其次,将checked={checkboxState}
作为属性添加到 Checkbox 组件中。
eg例如
<Checkbox checked={checkboxState} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.