Switch 开关
用于表达启用/停用、打开/关闭这类二元状态。
基础用法
已关闭
vue
<template>
<x-switch v-model="enabled" />
</template>
<script setup lang="ts">
import { ref } from 'vue';
const enabled = ref(false);
</script>文案和自定义值
enabled
vue
<template>
<x-switch
v-model="status"
checked-value="enabled"
unchecked-value="disabled"
checked-text="开"
unchecked-text="关"
/>
<span>{{ status }}</span>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const status = ref<'enabled' | 'disabled'>('enabled');
</script>类型
vue
<template>
<x-switch v-model="enabled" type="circle" />
<x-switch v-model="enabled" type="round" />
<x-switch v-model="enabled" type="line" />
</template>尺寸
vue
<template>
<x-switch v-model="enabled" size="medium" checked-text="开" unchecked-text="关" />
<x-switch v-model="enabled" size="medium" checked-text="启用" unchecked-text="停用" />
<x-switch v-model="enabled" size="small" />
<x-switch v-model="enabled" size="small" checked-text="开" unchecked-text="关" />
</template>禁用
vue
<template>
<x-switch disabled />
<x-switch :model-value="true" disabled />
</template>加载中
vue
<template>
<x-switch loading />
<x-switch :model-value="true" loading />
</template>自定义颜色
vue
<template>
<x-switch
v-model="enabled"
checked-color="#0f766e"
unchecked-color="#94a3b8"
checked-text="启用"
unchecked-text="停用"
/>
<x-switch
v-model="enabled"
type="line"
checked-color="#0f766e"
unchecked-color="#94a3b8"
/>
</template>切换前校验
异步校验期间会自动显示 loading,只允许切换到开启
vue
<template>
<x-switch v-model="enabled" :before-change="beforeChange" />
</template>
<script setup lang="ts">
import { ref } from 'vue';
const enabled = ref(false);
const beforeChange = async (nextValue: boolean | string | number) => {
await new Promise((resolve) => window.setTimeout(resolve, 600));
return nextValue === true;
};
</script>自定义图标
vue
<template>
<x-switch v-model="enabled">
<template #checked-icon>✓</template>
<template #unchecked-icon>×</template>
</x-switch>
</template>事件回调
等待操作
vue
<template>
<x-switch v-model="enabled" @change="handleChange" @click="handleClick" />
<span>{{ eventLog }}</span>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const enabled = ref(false);
const eventLog = ref('等待操作');
const clickCount = ref(0);
const handleChange = (value: boolean | string | number) => {
eventLog.value = `当前值:${value}`;
};
const handleClick = (value: boolean | string | number) => {
clickCount.value += 1;
eventLog.value = `点击第 ${clickCount.value} 次,目标值:${value}`;
};
</script>原生属性
支持 id、name、tabindex、aria-* 等原生 button 属性透传
vue
<template>
<x-switch
id="notification-switch"
name="notification"
autofocus
aria-label="通知开关"
/>
</template>方法
vue
<template>
<x-switch ref="switchRef" v-model="enabled" />
<x-button @click="switchRef?.focus()">聚焦</x-button>
<x-button @click="switchRef?.blur()">失焦</x-button>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const enabled = ref(false);
const switchRef = ref();
</script>按需导入
ts
import { Switch } from 'x-next';Props
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
modelValue | 绑定值 | string | number | boolean | undefined |
defaultChecked | 默认选中状态,非受控模式 | boolean | false |
disabled | 是否禁用 | boolean | false |
loading | 是否加载中 | boolean | false |
autofocus | 是否自动获取焦点 | boolean | false |
type | 开关类型 | 'circle' | 'round' | 'line' | 'circle' |
size | 开关尺寸 | 'small' | 'medium' | 跟随全局配置 |
checkedValue | 选中时的值 | string | number | boolean | true |
uncheckedValue | 未选中时的值 | string | number | boolean | false |
checkedColor | 选中时颜色 | string | - |
uncheckedColor | 未选中时颜色 | string | - |
beforeChange | 切换前钩子,返回 false 或 Promise reject 时阻止切换 | (value) => boolean | void | Promise<boolean | void> | - |
checkedText | 选中时文案,type="line" 时不显示 | string | - |
uncheckedText | 未选中时文案,type="line" 时不显示 | string | - |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
update:modelValue | 值更新时触发 | value |
change | 值变化时触发 | value, event |
click | 点击时触发,返回本次尝试切换的目标值 | value, event |
focus | 获取焦点时触发 | event |
blur | 失去焦点时触发 | event |
Slots
| 插槽名 | 说明 |
|---|---|
checked | 选中时文案 |
unchecked | 未选中时文案 |
checked-icon | 选中时按钮图标 |
unchecked-icon | 未选中时按钮图标 |
Methods
| 方法名 | 说明 |
|---|---|
focus() | 聚焦开关 |
blur() | 取消聚焦 |