import { Switch, Space, View, Text, Button } from 'taro-uno-ui';
import { useState } from 'react';
const SwitchExample = () => {
const [checked, setChecked] = useState(false);
const [checkedList, setCheckedList] = useState([false, false, false]);
return (
<View>
<Text style={{ fontSize: '20px', fontWeight: 'bold', marginBottom: '12px', display: 'block' }}>基础开关</Text>
<Space direction="vertical" size="large" style={{ marginBottom: '20px' }}>
<Switch />
<Switch defaultChecked />
</Space>
<Text style={{ fontSize: '20px', fontWeight: 'bold', marginBottom: '12px', display: 'block', marginTop: '20px' }}>受控模式</Text>
<Space direction="vertical" size="large" style={{ marginBottom: '20px' }}>
<Space>
<Switch
checked={checked}
onChange={(e) => setChecked(e.detail.checked)}
/>
<Text>{checked ? '已开启' : '已关闭'}</Text>
</Space>
<Button
type="primary"
onClick={() => setChecked(!checked)}
>
{checked ? '关闭开关' : '打开开关'}
</Button>
</Space>
<Text style={{ fontSize: '20px', fontWeight: 'bold', marginBottom: '12px', display: 'block', marginTop: '20px' }}>不同大小</Text>
<Space direction="vertical" size="large" style={{ marginBottom: '20px' }}>
<Space>
<Text>小号:</Text>
<Switch size="small" />
</Space>
<Space>
<Text>中号:</Text>
<Switch size="medium" />
</Space>
<Space>
<Text>大号:</Text>
<Switch size="large" />
</Space>
</Space>
<Text style={{ fontSize: '20px', fontWeight: 'bold', marginBottom: '12px', display: 'block', marginTop: '20px' }}>不同颜色</Text>
<Space direction="vertical" size="large" style={{ marginBottom: '20px' }}>
<Space>
<Text>主要颜色:</Text>
<Switch color="primary" />
</Space>
<Space>
<Text>成功颜色:</Text>
<Switch color="success" />
</Space>
<Space>
<Text>警告颜色:</Text>
<Switch color="warning" />
</Space>
<Space>
<Text>错误颜色:</Text>
<Switch color="error" />
</Space>
<Space>
<Text>自定义颜色:</Text>
<Switch color="#ff6b6b" />
</Space>
</Space>
<Text style={{ fontSize: '20px', fontWeight: 'bold', marginBottom: '12px', display: 'block', marginTop: '20px' }}>禁用状态</Text>
<Space direction="vertical" size="large" style={{ marginBottom: '20px' }}>
<Space>
<Text>禁用关闭:</Text>
<Switch disabled />
</Space>
<Space>
<Text>禁用开启:</Text>
<Switch disabled checked />
</Space>
</Space>
<Text style={{ fontSize: '20px', fontWeight: 'bold', marginBottom: '12px', display: 'block', marginTop: '20px' }}>带文字</Text>
<Space direction="vertical" size="large" style={{ marginBottom: '20px' }}>
<Switch checkedText="开" uncheckedText="关" />
<Switch checkedText="启用" uncheckedText="禁用" defaultChecked />
</Space>
<Text style={{ fontSize: '20px', fontWeight: 'bold', marginBottom: '12px', display: 'block', marginTop: '20px' }}>加载状态</Text>
<Space direction="vertical" size="large" style={{ marginBottom: '20px' }}>
<Space>
<Text>加载关闭:</Text>
<Switch loading />
</Space>
<Space>
<Text>加载开启:</Text>
<Switch loading checked />
</Space>
</Space>
<Text style={{ fontSize: '20px', fontWeight: 'bold', marginBottom: '12px', display: 'block', marginTop: '20px' }}>开关组</Text>
<Space direction="vertical" size="large" style={{ marginBottom: '20px' }}>
<Space>
<Text>选项 1:</Text>
<Switch
checked={checkedList[0]}
onChange={(e) => {
const newList = [...checkedList];
newList[0] = e.detail.checked;
setCheckedList(newList);
}}
/>
</Space>
<Space>
<Text>选项 2:</Text>
<Switch
checked={checkedList[1]}
onChange={(e) => {
const newList = [...checkedList];
newList[1] = e.detail.checked;
setCheckedList(newList);
}}
/>
</Space>
<Space>
<Text>选项 3:</Text>
<Switch
checked={checkedList[2]}
onChange={(e) => {
const newList = [...checkedList];
newList[2] = e.detail.checked;
setCheckedList(newList);
}}
/>
</Space>
<Text style={{ marginTop: '12px' }}>选中项:{checkedList.filter(Boolean).length} 个</Text>
</Space>
</View>
);
};
export default SwitchExample;