设计思路
本质上switch就是一个button,通过改变其button样式,就达到开关的效果,最终成果如下
简化代码
swtich.tsx
import React,{useState} from "react";
function Switch(){
// 创建本地的checked 的state,用于控制开关关闭或开始
const [checked, setChecked] = useState<boolean>(false)
// Switch点击事件
const onHandleClick: React.MouseEventHandler<HTMLButtonElement> = (event => {
setChecked(!checked);
})
// 通过checked给予classname
const className = checked ? 'arco-switch-checked' : ''
return (
<button
className={'arco-switch ' + className}
onClick={onHandleClick}
role="switch"
type="button"
aria-checked={checked}
>
<div className&