云链智安app
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

l-switch.vue 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <view class="l-switch">
  3. <view
  4. class="l-switch__rail"
  5. ref="rootRef"
  6. :hover-start-time="20"
  7. :hover-stay-time="70"
  8. :hover-class="rubberBand && !disabled && !loading ? 'l-switch--hover' : ''"
  9. :class="{
  10. 'l-switch--checked' : innerValue,
  11. 'l-switch--unchecked' : !innerValue,
  12. 'l-switch--disabled' : disabled,
  13. 'l-switch--round' : round,
  14. 'l-switch--square' : !round,
  15. }"
  16. :style="[styles]"
  17. aria-role="switch"
  18. @click="handleClick">
  19. <view class="l-switch__children-placeholder" v-if="placeholder.length > 0">
  20. <view class="l-switch__rail-placeholder" v-for="item in placeholder" :key="item">
  21. {{item}}
  22. </view>
  23. </view>
  24. <view class="l-switch__dot"
  25. :class="{
  26. 'l-switch__dot--disabled' : disabled,
  27. 'l-switch__dot--round' : round,
  28. 'l-switch__dot--square' : !round,
  29. }"
  30. ref="dotRef">
  31. <text class="l-switch__placeholder l-switch__placeholder--checked"
  32. ref="checkedRef" v-if="placeholder.length > 0">
  33. {{placeholder[0]}}
  34. </text>
  35. <text class="l-switch__placeholder l-switch__placeholder--unchecked"
  36. ref="uncheckedRef" v-if="placeholder.length > 1">
  37. {{placeholder[1]}}
  38. </text>
  39. <l-loading v-if="loading && $slots['icon'] == null"></l-loading>
  40. <slot name="icon" :checked="innerValue"></slot>
  41. </view>
  42. </view>
  43. </view>
  44. </template>
  45. <script lang="ts">
  46. // @ts-nocheck
  47. import { defineComponent, ref, computed } from '@/uni_modules/lime-shared/vue';
  48. import switchProps from './props';
  49. /**
  50. * LimeSwitch 图片
  51. * @description 用于控制某个功能的开启和关闭。可嵌套内容、禁用以及颜色配置,兼容uniapp/uniappx
  52. * @tutorial https://ext.dcloud.net.cn/plugin?id=155222
  53. * @property {Boolean} value 选中时对应的值
  54. * @property {Boolean} defaultValue 默认值
  55. * @property {Boolean} disabled 是否禁用
  56. * @property {Boolean} loading 是否加载
  57. * @property {Boolean} round 是否为圆形
  58. * @property {Boolean} rubberBand 是否开启橡皮效果
  59. * @property {Array} placeholder 占位内容
  60. * @property {String} src 图片地址
  61. * @property {String} fontSize 字体大小
  62. * @property {String} width 最小宽度
  63. * @property {String} height 高度
  64. * @property {String} dotSize 按钮大小
  65. * @property {String} dotPressedSize 按下按钮大小
  66. * @property {String} checkedColor 激活背景色
  67. * @property {String} disabledColor 禁用背景色
  68. * @property {String} checkedDisabledColor 激活禁用背景色
  69. * @property {String} uncheckedColor 背景色
  70. * @event {Function} change 切换触发
  71. */
  72. export default defineComponent({
  73. name: 'l-switch',
  74. props: switchProps,
  75. emits: ['change', 'update:modelValue', 'input'],
  76. setup(props, {emit}) {
  77. // 设置默认值
  78. const modelValue = ref((props.modelValue ?? false) || (props.defaultValue ?? false))
  79. const innerValue = computed({
  80. set(value: boolean){
  81. if(value == modelValue.value) return
  82. modelValue.value = value;
  83. emit('change', value)
  84. emit('update:modelValue', value)
  85. // #ifdef VUE2
  86. emit('input', value)
  87. // #endif
  88. },
  89. get():boolean {
  90. return (props.value ?? false) || modelValue.value
  91. },
  92. } as WritableComputedOptions<boolean>)
  93. const styles = computed(()=>{
  94. const style: Record<string, any> = {}
  95. if(props.width) {
  96. style['--l-switch-width'] = props.width!
  97. }
  98. if(props.height) {
  99. style['--l-switch-height'] = props.height!
  100. }
  101. if(props.fontSize) {
  102. style['--l-swtich-font-size'] = props.fontSize!
  103. }
  104. if(props.dotSize) {
  105. style['--l-switch-dot-size'] = props.dotSize!
  106. }
  107. if(props.dotPressedSize) {
  108. style['--l-switch-dot-size-pressed'] = props.dotPressedSize!
  109. }
  110. if(props.checkedColor) {
  111. style['--l-switch-checked-color'] = props.checkedColor!
  112. }
  113. if(props.checkedDisabledColor) {
  114. style['--l-switch-checked-disabled-color'] = props.checkedDisabledColor!
  115. }
  116. if(props.disabledColor) {
  117. style['--l-switch-unchecked-disabled-color'] = props.disabledColor!
  118. }
  119. if(props.uncheckedColor) {
  120. style['--l-switch-unchecked-color'] = props.uncheckedColor!
  121. }
  122. return style
  123. })
  124. const handleClick = (e: UniPointerEvent) => {
  125. if(props.disabled || props.loading) return
  126. innerValue.value = !innerValue.value;
  127. }
  128. return {
  129. innerValue,
  130. styles,
  131. handleClick
  132. }
  133. }
  134. })
  135. </script>
  136. <style lang="scss">
  137. @import './index-u';
  138. </style>