Skip to content

开关 Switch

这个组件叫做 Switch,听起来就像某款游戏机一样,好想玩......

基础使用

传入 modelValue 进入受控模式。不传或者为 undefined 则为非受控模式,此时可以传入 defaultValue 属性作为默认值。

<template>
	<px-space>
		<px-switch v-model="value"></px-switch>
		<px-switch :default-value="true"></px-switch>
	</px-space>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value = ref(false)
</script>

只读 & 禁用

readonly 设置只读,disabled 设置禁用,此时内部 <input> 都会被设置 disabled。它们之间几乎只有样式不一样。

Disabled:
Readonly:
<template>
	<px-space>
		Disabled:
		<px-switch disabled></px-switch>
		<px-switch disabled :default-value="true"></px-switch>
		Readonly:
		<px-switch readonly></px-switch>
		<px-switch readonly :default-value="true"></px-switch>
	</px-space>
</template>

加载状态

loading 设置加载状态,会展示加载图标。

<template>
	<px-space>
		<px-switch loading></px-switch>
		<px-switch loading :default-value="true"></px-switch>
		<px-switch loading disabled></px-switch>
		<px-switch loading disabled :default-value="true"></px-switch>
	</px-space>
</template>

提示信息

activeTipinactiveTip 设置组件内部的文本提示,activeLabelinactiveLabel 设置组件两侧的文本提示,也支持插槽的形式:active-tipinactive-tipactive-labelinactive-label

active-iconinactive-icon 设置开关的图标。

<template>
	<px-space direction="vertical">
		<px-switch size="small">
			<template #active-icon><IconCheckSolid></IconCheckSolid></template>
			<template #inactive-icon><IconTimesSolid></IconTimesSolid></template>
			<template #active-label>Active</template>
			<template #inactive-label>Inactive</template>
			<template #active-tip>active</template>
			<template #inactive-tip>inactive</template>
		</px-switch>
		<px-switch
			active-label="Active"
			active-tip="active"
			inactive-label="Inactive"
			inactive-tip="inactive"
		>
			<template #active-icon><IconCheckSolid></IconCheckSolid></template>
			<template #inactive-icon><IconTimesSolid></IconTimesSolid></template>
		</px-switch>
		<px-switch size="large">
			<template #active-icon><IconCheckSolid></IconCheckSolid></template>
			<template #inactive-icon><IconTimesSolid></IconTimesSolid></template>
			<template #active-label>Active</template>
			<template #inactive-label>Inactive</template>
			<template #active-tip>active</template>
			<template #inactive-tip>inactive</template>
		</px-switch>
	</px-space>
</template>

<script setup lang="ts">
import { IconCheckSolid, IconTimesSolid } from '@pixelium/web-vue/icon-hn/es'
</script>

背景颜色

通过 activeColorinactiveColor 设置背景颜色。

<template>
	<px-space>
		<px-switch
			active-color="#13ce66"
			inactive-color="#ff4949"
			active-label="Active"
			active-tip="active"
			inactive-label="Inactive"
			inactive-tip="inactive"
		>
			<template #active-icon>T</template>
			<template #inactive-icon>F</template>
		</px-switch>
	</px-space>
</template>

开关尺寸

size 属性设置开关的尺寸。

<template>
	<px-space>
		<px-switch size="small"></px-switch>
		<px-switch></px-switch>
		<px-switch size="large"></px-switch>
	</px-space>
</template>

开关形状

开关有 'round''rect' 两种形状,通过 shape 设置。

<template>
	<px-space>
		<px-switch size="small"></px-switch>
		<px-switch size="small" shape="normal"></px-switch>
		<px-switch></px-switch>
		<px-switch shape="normal"></px-switch>
		<px-switch size="large"></px-switch>
		<px-switch size="large" shape="normal"></px-switch>
	</px-space>
</template>

API

SwitchProps

属性类型可选默认值描述版本
modelValueboolean | nullundefined开关的值(受控模式),支持 v-model0.0.3
defaultValueboolean | nullundefined开关的默认值(非受控模式)。0.0.3
shape'round' | 'rect''round'开关的形状。0.0.3
size'small' | 'medium' | 'large''medium'开关的尺寸。0.0.3
readonlybooleanfalse是否只读。0.0.3
disabledbooleanfalse是否禁用。0.0.3
loadingbooleanfalse是否为加载状态。0.0.3
activeTipstring选中时的文本提示,位于开关内侧。0.0.3
inactiveTipstring未选中时的文本提示,位于开关内侧。0.0.3
activeLabelstring选中时的文本标签,位于开关外侧。0.0.3
inactiveLabelstring未选中时的文本标签,位于开关外侧。0.0.3
activeColorstring选中时标签颜色,支持类似 CSS 的 'rgb(r, g, b)''rgba(r, g, b, a)' 字符串和 3、4、6、8 位长度的十六位数字表示。0.0.3
inactiveColorstring未选中时标签颜色,支持类似 CSS 的 'rgb(r, g, b)''rgba(r, g, b, a)' 字符串和 3、4、6、8 位长度的十六位数字表示。0.0.3

SwitchEvents

事件参数描述版本
update:modelValuevalue: boolean更新 modelValue 的回调。0.0.3
inputvalue: boolean, event: InputEvent触发开关的回调。0.0.3
changevalue: boolean, event: Event开关选中状态改变的回调。0.0.3
focusevent: FocusEvent聚焦开关的回调。0.0.3
blurevent: FocusEvent开关失焦的回调。0.0.3

SwitchSlots

插槽参数描述版本
active-tip选中时的提示,位于开关内侧。0.0.3
inactive-tip未选中时的提示,位于开关内侧。0.0.3
active-label选中时的文本标签,位于开关外侧。0.0.3
inactive-label未选中时的文本标签,位于开关外侧。0.0.3
active-icon选中时的图标。0.0.3
inactive-icon未选中时的图标。0.0.3