| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <template>
- <view class="l-switch">
- <view
- class="l-switch__rail"
- ref="rootRef"
- :hover-start-time="20"
- :hover-stay-time="70"
- :hover-class="rubberBand && !disabled && !loading ? 'l-switch--hover' : ''"
- :class="{
- 'l-switch--checked' : innerValue,
- 'l-switch--unchecked' : !innerValue,
- 'l-switch--disabled' : disabled,
- 'l-switch--round' : round,
- 'l-switch--square' : !round,
- }"
- :style="[styles]"
- aria-role="switch"
- @click="handleClick">
- <view class="l-switch__children-placeholder" v-if="placeholder.length > 0">
- <view class="l-switch__rail-placeholder" v-for="item in placeholder" :key="item">
- {{item}}
- </view>
- </view>
- <view class="l-switch__dot"
- :class="{
- 'l-switch__dot--disabled' : disabled,
- 'l-switch__dot--round' : round,
- 'l-switch__dot--square' : !round,
- }"
- ref="dotRef">
- <text class="l-switch__placeholder l-switch__placeholder--checked"
- ref="checkedRef" v-if="placeholder.length > 0">
- {{placeholder[0]}}
- </text>
- <text class="l-switch__placeholder l-switch__placeholder--unchecked"
- ref="uncheckedRef" v-if="placeholder.length > 1">
- {{placeholder[1]}}
- </text>
- <l-loading v-if="loading && $slots['icon'] == null"></l-loading>
- <slot name="icon" :checked="innerValue"></slot>
- </view>
- </view>
- </view>
- </template>
-
- <script lang="ts">
- // @ts-nocheck
- import { defineComponent, ref, computed } from '@/uni_modules/lime-shared/vue';
- import switchProps from './props';
-
-
- /**
- * LimeSwitch 图片
- * @description 用于控制某个功能的开启和关闭。可嵌套内容、禁用以及颜色配置,兼容uniapp/uniappx
- * @tutorial https://ext.dcloud.net.cn/plugin?id=155222
- * @property {Boolean} value 选中时对应的值
- * @property {Boolean} defaultValue 默认值
- * @property {Boolean} disabled 是否禁用
- * @property {Boolean} loading 是否加载
- * @property {Boolean} round 是否为圆形
- * @property {Boolean} rubberBand 是否开启橡皮效果
- * @property {Array} placeholder 占位内容
- * @property {String} src 图片地址
- * @property {String} fontSize 字体大小
- * @property {String} width 最小宽度
- * @property {String} height 高度
- * @property {String} dotSize 按钮大小
- * @property {String} dotPressedSize 按下按钮大小
- * @property {String} checkedColor 激活背景色
- * @property {String} disabledColor 禁用背景色
- * @property {String} checkedDisabledColor 激活禁用背景色
- * @property {String} uncheckedColor 背景色
- * @event {Function} change 切换触发
- */
- export default defineComponent({
- name: 'l-switch',
- props: switchProps,
- emits: ['change', 'update:modelValue', 'input'],
- setup(props, {emit}) {
- // 设置默认值
- const modelValue = ref((props.modelValue ?? false) || (props.defaultValue ?? false))
- const innerValue = computed({
- set(value: boolean){
- if(value == modelValue.value) return
- modelValue.value = value;
- emit('change', value)
- emit('update:modelValue', value)
- // #ifdef VUE2
- emit('input', value)
- // #endif
- },
- get():boolean {
- return (props.value ?? false) || modelValue.value
- },
- } as WritableComputedOptions<boolean>)
-
- const styles = computed(()=>{
- const style: Record<string, any> = {}
-
- if(props.width) {
- style['--l-switch-width'] = props.width!
- }
- if(props.height) {
- style['--l-switch-height'] = props.height!
- }
- if(props.fontSize) {
- style['--l-swtich-font-size'] = props.fontSize!
- }
- if(props.dotSize) {
- style['--l-switch-dot-size'] = props.dotSize!
- }
- if(props.dotPressedSize) {
- style['--l-switch-dot-size-pressed'] = props.dotPressedSize!
- }
- if(props.checkedColor) {
- style['--l-switch-checked-color'] = props.checkedColor!
- }
- if(props.checkedDisabledColor) {
- style['--l-switch-checked-disabled-color'] = props.checkedDisabledColor!
- }
- if(props.disabledColor) {
- style['--l-switch-unchecked-disabled-color'] = props.disabledColor!
- }
- if(props.uncheckedColor) {
- style['--l-switch-unchecked-color'] = props.uncheckedColor!
- }
- return style
- })
- const handleClick = (e: UniPointerEvent) => {
- if(props.disabled || props.loading) return
- innerValue.value = !innerValue.value;
- }
-
-
- return {
- innerValue,
- styles,
- handleClick
- }
- }
- })
-
- </script>
- <style lang="scss">
- @import './index-u';
- </style>
|