Toggle Button 切换按钮
Toggle Buttons 可用于对相关选项进行分组。
为了强调组合之间的关联,每一组 Toggle buttons 应该共享一个容器。
The ToggleButtonGroup
will control the selected state of its child buttons when given its own value
prop.
Exclusive Selection
Text justification toggle buttons present options for left, right, center, full, and justified text with only one item available for selection at a time. Selecting one option deselects any other.
Multiple Selection
Logically-grouped options, like Bold, Italic, and Underline, allow multiple options to be selected.
Enforce value set
If you want to enforce at least one button to be active, you can adapt your handleChange function.
const handleFormat = (event, newFormats) => {
if (newFormats.length) {
setFormats(newFormats);
}
};
const handleAlignment = (event, newAlignment) => {
if (newAlignment !== null) {
setAlignment(newAlignment);
}
};
<ToggleButton
value="check"
selected={selected}
onChange={() => {
setSelected(!selected);
}}
>
<CheckIcon />
</ToggleButton>
Customized toggle button
以下是自定义组件的一个示例。 您可以在重写文档页中了解有关此内容的更多信息。