跳到主要內容

Switch 開關

在兩個互斥狀態間切換,渲染 role="switch" 自訂控制項。

基礎用法​

受控開關
關

當前狀態:關

import { useState } from 'react'
import { LhSwitch } from '@lh-ui/react'

export default function Demo() {
const [active, setActive] = useState(false)

return (
<>
<LhSwitch checked={active} onChange={(val) => setActive(val)} />
<LhSwitch
checked={active}
activeText="开"
inactiveText="关"
onChange={(val) => setActive(val)}
/>
<LhSwitch disabled />
<LhSwitch loading />
</>
)
}

Switch Props​

屬性說明型別可選值預設值
checked受控開關狀態boolean——
defaultChecked非受控預設狀態boolean—false
disabled停用boolean—false
loading載入中boolean—false
activeText開啟時的文字string—語言包 switch.on
inactiveText關閉時的文字string—語言包 switch.off
size尺寸'large' | 'middle' | 'small'—跟隨 ConfigProvider
themeVars元件級主題變數Record<string, string>——
sx快速版面配置屬性SxProps——

Switch 事件​

事件說明回呼簽名Vue 對照
onChange狀態變化(雙參數)(checked: boolean, event: MouseEvent | KeyboardEvent) => void@change
鍵盤互動

開關回應 Enter / Space 切換,與原生 checkbox 行為一致;aria-checked 同步當前狀態。

樣式​

import '@lh-ui/react/style/switch'