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.

177 lines
6.0 KiB

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