You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.9 KiB

5 months ago
  1. <template>
  2. <view
  3. class="u-switch cursor-pointer"
  4. :class="[disabled && 'u-switch--disabled']"
  5. :style="[switchStyle, addStyle(customStyle)]"
  6. @tap="clickHandler"
  7. >
  8. <view
  9. class="u-switch__bg"
  10. :style="[bgStyle]"
  11. >
  12. </view>
  13. <view
  14. class="u-switch__node"
  15. <!-- #ifdef VUE3 -->
  16. :class="[modelValue && 'u-switch__node--on']"
  17. <!-- #endif -->
  18. <!-- #ifdef VUE2 -->
  19. :class="[value && 'u-switch__node--on']"
  20. <!-- #endif -->
  21. :style="[nodeStyle]"
  22. ref="u-switch__node"
  23. >
  24. <u-loading-icon
  25. :show="loading"
  26. mode="circle"
  27. timingFunction='linear'
  28. <!-- #ifdef VUE3 -->
  29. :color="modelValue ? activeColor : '#AAABAD'"
  30. <!-- #endif -->
  31. <!-- #ifdef VUE2 -->
  32. :color="value ? activeColor : '#AAABAD'"
  33. <!-- #endif -->
  34. :size="size * 0.6"
  35. />
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import { props } from './props';
  41. import { mpMixin } from '../../libs/mixin/mpMixin';
  42. import { mixin } from '../../libs/mixin/mixin';
  43. import { addStyle, addUnit, error } from '../../libs/function/index';
  44. /**
  45. * switch 开关选择器
  46. * @description 选择开关一般用于只有两个选择且只能选其一的场景
  47. * @tutorial https://ijry.github.io/uview-plus/components/switch.html
  48. * @property {Boolean} loading 是否处于加载中默认 false
  49. * @property {Boolean} disabled 是否禁用默认 false
  50. * @property {String | Number} size 开关尺寸单位px 默认 25
  51. * @property {String} activeColor 打开时的背景色 默认 '#2979ff'
  52. * @property {String} inactiveColor 关闭时的背景色 默认 '#ffffff'
  53. * @property {Boolean | String | Number} value 通过v-model双向绑定的值 默认 false
  54. * @property {Boolean | String | Number} activeValue 打开选择器时通过change事件发出的值 默认 true
  55. * @property {Boolean | String | Number} inactiveValue 关闭选择器时通过change事件发出的值 默认 false
  56. * @property {Boolean} asyncChange 是否开启异步变更开启后需要手动控制输入值 默认 false
  57. * @property {String | Number} space 圆点与外边框的距离 默认 0
  58. * @property {Object} customStyle 定义需要用到的外部样式
  59. *
  60. * @event {Function} change 在switch打开或关闭时触发
  61. * @example <u-switch v-model="checked" active-color="red" inactive-color="#eee"></u-switch>
  62. */
  63. export default {
  64. name: "u-switch",
  65. mixins: [mpMixin, mixin,props],
  66. watch: {
  67. // #ifdef VUE3
  68. modelValue: {
  69. immediate: true,
  70. handler(n) {
  71. if(n !== this.inactiveValue && n !== this.activeValue) {
  72. error('v-model绑定的值必须为inactiveValue、activeValue二者之一')
  73. }
  74. }
  75. },
  76. // #endif
  77. // #ifdef VUE2
  78. value: {
  79. immediate: true,
  80. handler(n) {
  81. if(n !== this.inactiveValue && n !== this.activeValue) {
  82. error('v-model绑定的值必须为inactiveValue、activeValue二者之一')
  83. }
  84. }
  85. }
  86. // #endif
  87. },
  88. data() {
  89. return {
  90. bgColor: '#ffffff'
  91. }
  92. },
  93. computed: {
  94. isActive(){
  95. // #ifdef VUE3
  96. return this.modelValue === this.activeValue;
  97. // #endif
  98. // #ifdef VUE2
  99. return this.value === this.activeValue;
  100. // #endif
  101. },
  102. switchStyle() {
  103. let style = {}
  104. // 这里需要加2,是为了腾出边框的距离,否则圆点node会和外边框紧贴在一起
  105. style.width = addUnit(this.size * 2 + 2)
  106. style.height = addUnit(Number(this.size) + 2)
  107. // style.borderColor = this.value ? 'rgba(0, 0, 0, 0)' : 'rgba(0, 0, 0, 0.12)'
  108. // 如果自定义了“非激活”演示,name边框颜色设置为透明(跟非激活颜色一致)
  109. // 这里不能简单的设置为非激活的颜色,否则打开状态时,会有边框,所以需要透明
  110. if(this.customInactiveColor) {
  111. style.borderColor = 'rgba(0, 0, 0, 0)'
  112. }
  113. style.backgroundColor = this.isActive ? this.activeColor : this.inactiveColor
  114. return style;
  115. },
  116. nodeStyle() {
  117. let style = {}
  118. // 如果自定义非激活颜色,将node圆点的尺寸减少两个像素,让其与外边框距离更大一点
  119. style.width = addUnit(this.size - this.space)
  120. style.height = addUnit(this.size - this.space)
  121. const translateX = this.isActive ? addUnit(this.space) : addUnit(this.size);
  122. style.transform = `translateX(-${translateX})`
  123. return style
  124. },
  125. bgStyle() {
  126. let style = {}
  127. // 这里配置一个多余的元素在HTML中,是为了让switch切换时,有更良好的背景色扩充体验(见实际效果)
  128. style.width = addUnit(Number(this.size) * 2 - this.size / 2)
  129. style.height = addUnit(this.size)
  130. style.backgroundColor = this.inactiveColor
  131. // 打开时,让此元素收缩,否则反之
  132. style.transform = `scale(${this.isActive ? 0 : 1})`
  133. return style
  134. },
  135. customInactiveColor() {
  136. // 之所以需要判断是否自定义了“非激活”颜色,是为了让node圆点离外边框更宽一点的距离
  137. return this.inactiveColor !== '#fff' && this.inactiveColor !== '#ffffff'
  138. }
  139. },
  140. // #ifdef VUE3
  141. emits: ['update:modelValue', 'change'],
  142. // #endif
  143. methods: {
  144. addStyle,
  145. clickHandler() {
  146. if (!this.disabled && !this.loading) {
  147. const oldValue = this.isActive ? this.inactiveValue : this.activeValue
  148. if(!this.asyncChange) {
  149. // #ifdef VUE3
  150. this.$emit("update:modelValue", oldValue);
  151. // #endif
  152. // #ifdef VUE2
  153. this.$emit("input", oldValue);
  154. // #endif
  155. }
  156. // 放到下一个生命周期,因为双向绑定的value修改父组件状态需要时间,且是异步的
  157. this.$nextTick(() => {
  158. this.$emit('change', oldValue)
  159. })
  160. }
  161. }
  162. }
  163. };
  164. </script>
  165. <style lang="scss" scoped>
  166. @import "../../libs/css/components.scss";
  167. .u-switch {
  168. @include flex(row);
  169. /* #ifndef APP-NVUE */
  170. box-sizing: border-box;
  171. /* #endif */
  172. position: relative;
  173. background-color: #fff;
  174. border-width: 1px;
  175. border-radius: 100px;
  176. transition: background-color 0.4s;
  177. border-color: rgba(0, 0, 0, 0.12);
  178. border-style: solid;
  179. justify-content: flex-end;
  180. align-items: center;
  181. // 由于weex为阿里逗着玩的KPI项目,导致bug奇多,这必须要写这一行,
  182. // 否则在iOS上,点击页面任意地方,都会触发switch的点击事件
  183. overflow: hidden;
  184. &__node {
  185. @include flex(row);
  186. align-items: center;
  187. justify-content: center;
  188. border-radius: 100px;
  189. background-color: #fff;
  190. border-radius: 100px;
  191. box-shadow: 1px 1px 1px 0 rgba(0, 0, 0, 0.25);
  192. transition-property: transform;
  193. transition-duration: 0.4s;
  194. transition-timing-function: cubic-bezier(0.3, 1.05, 0.4, 1.05);
  195. }
  196. &__bg {
  197. position: absolute;
  198. border-radius: 100px;
  199. background-color: #FFFFFF;
  200. transition-property: transform;
  201. transition-duration: 0.4s;
  202. border-top-left-radius: 0;
  203. border-bottom-left-radius: 0;
  204. transition-timing-function: ease;
  205. }
  206. &--disabled {
  207. opacity: 0.6;
  208. }
  209. }
  210. </style>