铝交易,微信公众号
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.

191 lines
6.7 KiB

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