跳到内容
+

Switch

开关是 UI 元素,允许用户在两种状态之间进行选择——最常见的是开/关。

useSwitch API

导入

import { useSwitch } from '@mui/base/useSwitch';
// or
import { useSwitch } from '@mui/base';

通过阅读本关于最小化包大小的指南,了解差异。

参数

名称类型描述
checkedboolean

如果 true,则组件被选中。

defaultCheckedboolean

默认选中状态。当组件不受控制时使用。

disabledboolean

如果 true,则组件被禁用。

onBlurReact.FocusEventHandler
onChangeReact.ChangeEventHandler<HTMLInputElement>

状态更改时触发的回调。

onFocusReact.FocusEventHandler
onFocusVisibleReact.FocusEventHandler
readOnlyboolean

如果 true,则组件为只读。

requiredboolean

如果 true,则 input 元素为必填项。

返回值

名称类型描述
checkedboolean

如果 true,组件将被选中。

disabledboolean

如果 true,组件将被禁用。

focusVisibleboolean

如果 true,则表示组件正在使用键盘聚焦。

getInputProps(externalProps?: React.HTMLAttributes<HTMLInputElement>) => UseSwitchInputSlotProps

输入 slot props 的解析器。

inputRefReact.RefCallback<HTMLInputElement> | null

输入 slot 的 DOM 节点的 Ref。

readOnlyboolean

如果 true,则组件为只读。