跳转到内容

Switch API

想要学习更多组件属性和 CSS API,可以查阅 React Switch 组件的 API 文档。

演示项目

For examples and details on the usage of this React component, visit the component demo pages:

导入

import Switch from '@mui/material/Switch';
// 或
import { Switch } from '@mui/material';
你可以阅读这个关于最小化捆绑包的指南以了解以上二者的差异。

组件名称

在主题中,名称“MuiSwitch”可用于提供默认属性(props),或者样式覆盖

属性

名称类型默认值描述
checkedboolfalse
If true, the component is checked.
checkedIconnode
The icon to display when the component is checked.
classesobject
Override or extend the styles applied to the component. See CSS API below for more details.
color'default'
| 'primary'
| 'secondary'
| 'error'
| 'info'
| 'success'
| 'warning'
| string
'primary'
The color of the component. It supports both default and custom theme colors, which can be added as shown in the palette customization guide.
defaultCheckedboolfalse
The default checked state. Use when the component is not controlled.
disabledboolfalse
如果被设置为 true,那么该组件将会被禁用。
disableRippleboolfalse
If true, the ripple effect is disabled.
edge'end'
| 'start'
| false
false
If given, uses a negative margin to counteract the padding on one side (this is often helpful for aligning the left or right side of the icon with content above or below, without ruining the border size and shape).
iconnode
The icon to display when the component is unchecked.
idstring
The id of the input element.
inputPropsobject
Attributes applied to the input element.
inputRefref
Pass a ref to the input element.
onChangefunc
Callback fired when the state is changed.

Signature:
function(event: object) => void
event: The event source of the callback. You can pull out the new value by accessing event.target.value (string). You can pull out the new checked state by accessing event.target.checked (boolean).
requiredboolfalse
If true, the input element is required.
size'medium'
| 'small'
| string
'medium'
The size of the switch. small is equivalent to the dense switch styling.
sxArray<func
| object
| bool>
| func
| object
The system prop that allows defining system overrides as well as additional CSS styles. See the `sx` page for more details.
valueany
The value of the component. The DOM API casts this to a string. The browser uses "on" as the default value.

ref 则会被传递到根元素中。

继承

尽管上文没有明文记录, IconButton 组件 的属性(props) 在组件Switch上同样是可用的。 你可以利用这一点来 指向嵌套组件

CSS

规则名称全局类名描述
root.MuiSwitch-rootStyles applied to the root element.
edgeStart.MuiSwitch-edgeStartStyles applied to the root element if edge="start".
edgeEnd.MuiSwitch-edgeEndStyles applied to the root element if edge="end".
switchBase.MuiSwitch-switchBaseStyles applied to the internal `SwitchBase` component's `root` class.
colorPrimary.MuiSwitch-colorPrimaryStyles applied to the internal SwitchBase component's root element if color="primary".
colorSecondary.MuiSwitch-colorSecondaryStyles applied to the internal SwitchBase component's root element if color="secondary".
sizeSmall.MuiSwitch-sizeSmallStyles applied to the root element if size="small".
sizeMedium.MuiSwitch-sizeMediumStyles applied to the root element if size="medium".
checked.Mui-checkedPseudo-class applied to the internal `SwitchBase` component's `checked` class.
disabled.Mui-disabledPseudo-class applied to the internal SwitchBase component's disabled class.
input.MuiSwitch-inputStyles applied to the internal SwitchBase component's input element.
thumb.MuiSwitch-thumbStyles used to create the thumb passed to the internal `SwitchBase` component `icon` prop.
track.MuiSwitch-trackStyles applied to the track element.

您可以使用组件自定义选项对组件进行个性化: