【发布时间】:2022-07-08 02:09:18
【问题描述】:
我有一个用例需要处理列表中的多个复选框
一次只能选择一个复选框
例如。如果我打开Checkbox 1 然后单击 Checkbox 2 - 那么我需要关闭 Checkbox 1 因为我打开on @ 987654325@
下面是我的代码
const ProductList = (props: InterdentalBrushProps) => {
const [isChecked, setIsChecked] = useState(false);
const { onEmit, brush_size, color_name, color_hex } = props;
const handleClick = () => {
setIsChecked(current => !current);
onEmit({
isChecked,
onEmit,
});
};
useEffect(() => {
console.log('isChecked is: ', isChecked);
}, [isChecked]);
return (
<List component="div" disablePadding>
<ListItemButton className="product-outline" onClick={handleClick}>
<CircleIcon sx={{ color: color_hex }} />
<ListItemText sx={{ ml: 2 }} primary={brush_size} />
<ListItemText sx={{ ml: -15 }} primary={color_name} />
<Checkbox
icon={<CircleIcon sx={{ color: '#ffffff' }} />}
checkedIcon={<CheckCircleIcon />}
checked={isChecked}
color="primary"
onChange={e => setIsChecked(e.target.checked)}
/>
</ListItemButton>
</List>
);
};
export default ProductList;
父组件
const Products = (props: InterdentalBrushProps) => {
const [open, setOpen] = useState(false);
const handleClick = () => {
setOpen(!open);
};
const { onEmit } = props;
return (
<List component="nav" aria-labelledby="nested-list-subheader">
<ListItemButton
disableRipple
onClick={handleClick}
style={{ backgroundColor: 'transparent' }}
>
<Orange />
<ListItemText
sx={{ ml: 2, mt: 3 }}
primary={
<Typography className="regular-20">
Tepe Interdental Brush ™
</Typography>
}
/>
{open ? (
<ExpandLess className="icon-color" />
) : (
<ExpandMore className="icon-color" />
)}
</ListItemButton>
<Collapse
className="product-scroll"
in={open}
timeout="auto"
unmountOnExit
>
{open &&
InterdentalBrush.regular.map((brush, index) => (
<ProductsList
onEmit={onEmit}
key={index}
brush_size={brush.brush_size}
color_name={brush.color_name}
color_hex={brush.color_hex}
/>
))}
</Collapse>
</List>
);
};
export default Products;
我已经为上下文添加了父组件,希望可以修复。
请帮我解决这个问题
谢谢
【问题讨论】:
-
标准是为此使用收音机,而不是复选框。这对可访问性更好。是否有充分的理由使用复选框?
-
也就是说,这是一个带有一个复选框的组件。如果你有父母,那就是取消选中其余复选框的责任。
-
单选按钮需要正确成对使用。所以我试图只有一个复选框
-
如果你能帮我解决这个问题,我可以发布我的父组件
-
不,同名的单选按钮是组的一部分,根据html standard。然后浏览器负责检查状态的排他性。但是,如果组件的拆分方式使其成为 PITA,请发布您的父母,我们可以看看。
标签: javascript reactjs typescript material-ui jsx