Skip to content

Switch 开关

手机扫码预览
23:59

组件名:rice-switch

用于在打开和关闭状态之间进行切换。

平台兼容性

uni-app x

AndroidiOS鸿蒙Next微信小程序h5

基础用法

通过 v-model 绑定开关的选中状态,默认 true 表示开,false 表示关。

html
<rice-switch v-model="value"></rice-switch>

开关状态

通过 disabled属性将 Switch 设置为禁用,通过 loading 属性设置为加载中。

html
<rice-switch v-model="value" disabled />
<rice-switch v-model="value" loading />

带描述开关

  • 使用 active-textinactive-text 属性来设置开关的文字描述;
  • 使用 active-iconinactive-icon 属性来设置开关的图标
  • 使用 prompt-position 属性来控制文本/图标 是否显示在点内,默认为 outball,可设置为 inball
html
<rice-switch v-model="value2" active-icon="checked" inactive-icon="cross"/>
<!-- 描述位置 -->
<rice-switch v-model="value2" prompt-position="inball" active-icon="checked"inactive-icon="cross"/>

自定义value 类型

通过 active-valueinactive-value 属性自定义打开/关闭时对应的值,它们接受 BooleanStringNumber 类型的值。

html
<rice-switch v-model="value3" active-value="N" inactive-value="Y" active-text="Y" inactive-text="N"></rice-switch>

自定义颜色

通过 active-color 属性设置打开时的背景颜色,通过 inactive-color 属性设置关闭时的背景颜色。

html
<rice-switch v-model="value4" active-color="#07c160" inactive-color="#f56c6c"></rice-switch>

自定义大小

  • 通过 width 属性可以设置 Switch 的宽度,单位支持 pxrpx
  • 也可以通过 cssscale 方法进行调节,通过 custom-style 属性进行传递。
html
<rice-switch v-model="value4" :custom-style="{transform:'scale(0.82)'}"></rice-switch>
<rice-switch v-model="value4" width="45px"></rice-switch>

异步控制

设置 before-change 属性,若返回 false 或者返回 Promise 且被 rejectresolve 但返回值不为 true, 则停止切换。

VUE
<template>
  <rice-switch v-model="statusAsync1" :loading="loadingAsync1" :before-change="beforeChange1"/>
</template>

<script setup lang="ts">
//请求成功示例1
const statusAsync1 = ref(true)
const loadingAsync1 = ref(false)

const beforeChange1 = () : Promise<boolean> => {
  loadingAsync1.value = true
  return new Promise((resolve) => {
    setTimeout(() => {
      loadingAsync1.value = false
      //resolve 里面必须为 true 才能切换
      resolve(true)
      //如返回reject 则不会切换
      //reject(false)
    }, 1000)
  })
}
</script>
vue
<template>
  <rice-switch v-model="statusAsync2" :loading="loadingAsync2" :before-change="beforeChange2"/>
</template>

<script setup>
//请求成功示例2
const sleep = (flag : boolean) : Promise<boolean> => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
         flag ? resolve(true) : reject(false)
      }, 1000)
    })
}

const statusAsync2 = ref(true)
const loadingAsync2 = ref(false)

const beforeChange2 = async () : Promise<boolean> => {
    loadingAsync2.value = true
    await sleep(true)
    loadingAsync2.value = false
    //不要忘记返回 true
    return true
}
</script>
vue
<template>
  <rice-switch v-model="statusAsyncFail" :loading="loadingAsyncFail" :before-change="beforeChangeFail"/>
</template>
<script setup>
const statusAsyncFail = ref(false)
const loadingAsyncFail = ref(false)

const beforeChangeFail = async () => {
    try {
      loadingAsyncFail.value = true
      await sleep(false)
      //不要忘记返回 true
      return true
    } catch (e) {
      loadingAsyncFail.value = false
      uni.showToast({
        title: '修改失败',
        icon: 'error'
      })
      //不要忘记返回 false
      return false
    }

}
</script>

API

Props

属性名类型说明默认值
v-model/model-valueString|Number|Boolean开关选中状态-
active-valueString|Number|Boolean打开时对应的值 默认true-
inactive-valueString|Number|Boolean关闭时对应的值 默认false-
loadingBoolean是否处于加载状态-
disabledBoolean是否禁用-
readonlyBoolean是否只读-
widthString|Number宽度,单位支持 px rpx-
icon-sizeString|Numbericon 图标大小,单位支持 px rpx-
text-sizeString|Number字体大小,单位支持 px rpx-
active-colorString打开时的背景颜色-
inactive-colorString关闭时的背景颜色-
active-textString打开时的文字描述-
inactive-textString关闭时的文字描述-
active-iconString打开时显示的图标,设置此项会忽略active-text-
inactive-iconString关闭时显示的图标,设置此项会忽略inactive-text-
prompt-positionString图标或文本显示的位置,可选值:inball、outballoutball
before-changeFunctionswitch 状态改变前的钩子, 返回 false 或者返回 Promise 且被 reject 则停止切换-
vibrateBoolean打开时是否开启震动效果,目前仅小程序支持-
custom-styleObject自定义style-

Events

事件名说明回调参数
changeSwitch 状态发生变化时触发value:string|number|boolean

类型定义

组件导出如下类型

ts
import {SwitchValueType} from '@/uni_modules/rice-ui'
// 组件类型
const switchRef = ref<RiceSwitchComponentPublicInstance | null>(null)