Switch 开关组件 #
基本用法 #
jsx
import { Switch } from '@chakra-ui/react'
function Example() {
return <Switch />
}
带标签 #
jsx
<FormControl display="flex" alignItems="center">
<FormLabel mb="0">启用通知</FormLabel>
<Switch />
</FormControl>
开关状态 #
默认选中 #
jsx
<Switch defaultIsChecked />
禁用状态 #
jsx
<Switch isDisabled />
<Switch isDisabled defaultIsChecked />
无效状态 #
jsx
<Switch isInvalid />
只读状态 #
jsx
<Switch isReadOnly />
开关尺寸 #
jsx
<Switch size="sm" /> 小
<Switch size="md" /> 中等(默认)
<Switch size="lg" /> 大
开关颜色 #
jsx
<Switch colorScheme="red" />
<Switch colorScheme="green" />
<Switch colorScheme="blue" />
<Switch colorScheme="orange" />
<Switch colorScheme="purple" />
受控开关 #
jsx
function ControlledSwitch() {
const [isChecked, setIsChecked] = useState(false)
return (
<Switch
isChecked={isChecked}
onChange={(e) => setIsChecked(e.target.checked)}
/>
)
}
配合 FormControl #
jsx
import { FormControl, FormLabel, FormHelperText } from '@chakra-ui/react'
function FormSwitch() {
return (
<FormControl>
<FormLabel>深色模式</FormLabel>
<Switch />
<FormHelperText>启用深色模式以保护眼睛</FormHelperText>
</FormControl>
)
}
实用示例 #
设置开关列表 #
jsx
function SettingsList() {
const [settings, setSettings] = useState({
notifications: true,
sounds: false,
vibrations: true,
})
const toggleSetting = (key) => {
setSettings({
...settings,
[key]: !settings[key],
})
}
return (
<VStack align="stretch" spacing={4}>
<FormControl display="flex" alignItems="center" justifyContent="space-between">
<Box>
<Text fontWeight="medium">通知</Text>
<Text fontSize="sm" color="gray.500">接收推送通知</Text>
</Box>
<Switch
isChecked={settings.notifications}
onChange={() => toggleSetting('notifications')}
/>
</FormControl>
<FormControl display="flex" alignItems="center" justifyContent="space-between">
<Box>
<Text fontWeight="medium">声音</Text>
<Text fontSize="sm" color="gray.500">播放提示音</Text>
</Box>
<Switch
isChecked={settings.sounds}
onChange={() => toggleSetting('sounds')}
/>
</FormControl>
<FormControl display="flex" alignItems="center" justifyContent="space-between">
<Box>
<Text fontWeight="medium">振动</Text>
<Text fontSize="sm" color="gray.500">振动反馈</Text>
</Box>
<Switch
isChecked={settings.vibrations}
onChange={() => toggleSetting('vibrations')}
/>
</FormControl>
</VStack>
)
}
深色模式切换 #
jsx
import { useColorMode } from '@chakra-ui/react'
function ColorModeSwitch() {
const { colorMode, toggleColorMode } = useColorMode()
return (
<FormControl display="flex" alignItems="center">
<FormLabel mb="0">
{colorMode === 'light' ? '浅色模式' : '深色模式'}
</FormLabel>
<Switch
isChecked={colorMode === 'dark'}
onChange={toggleColorMode}
colorScheme="blue"
/>
</FormControl>
)
}
确认开关 #
jsx
function ConfirmSwitch({ onConfirm }) {
const [isChecked, setIsChecked] = useState(false)
const handleChange = (e) => {
if (e.target.checked) {
setIsChecked(true)
onConfirm?.(true)
}
}
return (
<FormControl>
<HStack>
<Switch
isChecked={isChecked}
onChange={handleChange}
colorScheme="green"
/>
<Text>我确认以上信息准确无误</Text>
</HStack>
</FormControl>
)
}
功能开关卡片 #
jsx
function FeatureToggle({ title, description, isEnabled, onToggle }) {
return (
<Box p={4} borderWidth="1px" borderRadius="md">
<HStack justify="space-between">
<Box>
<Text fontWeight="medium">{title}</Text>
<Text fontSize="sm" color="gray.500">{description}</Text>
</Box>
<Switch
isChecked={isEnabled}
onChange={onToggle}
colorScheme="blue"
/>
</HStack>
</Box>
)
}
批量操作开关 #
jsx
function BatchSwitch() {
const [features, setFeatures] = useState({
analytics: true,
tracking: false,
marketing: true,
})
const allEnabled = Object.values(features).every(Boolean)
const someEnabled = Object.values(features).some(Boolean) && !allEnabled
const toggleAll = () => {
const newState = !allEnabled
setFeatures({
analytics: newState,
tracking: newState,
marketing: newState,
})
}
return (
<VStack align="stretch" spacing={4}>
<FormControl display="flex" alignItems="center" justifyContent="space-between">
<Text fontWeight="bold">全部功能</Text>
<Switch
isChecked={allEnabled}
isIndeterminate={someEnabled}
onChange={toggleAll}
/>
</FormControl>
<Divider />
{Object.entries(features).map(([key, value]) => (
<FormControl key={key} display="flex" alignItems="center" justifyContent="space-between">
<Text textTransform="capitalize">{key}</Text>
<Switch
isChecked={value}
onChange={() => setFeatures({ ...features, [key]: !value })}
/>
</FormControl>
))}
</VStack>
)
}
Switch 属性速查表 #
| 属性 | 说明 | 值 |
|---|---|---|
| isChecked | 是否选中 | true, false |
| defaultIsChecked | 默认选中 | true, false |
| isDisabled | 禁用 | true, false |
| isInvalid | 无效 | true, false |
| isReadOnly | 只读 | true, false |
| size | 尺寸 | sm, md, lg |
| colorScheme | 颜色方案 | blue, green, red… |
下一步 #
现在你已经掌握了 Switch 组件,接下来学习 Form 表单,了解如何构建完整的表单!
最后更新:2026-03-28