index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <template>
  2. <uni-shadow-root class="vant-switch-index"><view :class="'custom-class '+(utils.bem('switch', { on: value === activeValue, disabled }))" :style="'font-size: '+(size)+'; '+((checked ? activeColor : inactiveColor) ? 'background-color: ' + (checked ? activeColor : inactiveColor ) : '')" @click="onClick">
  3. <view class="van-switch__node node-class">
  4. <van-loading v-if="loading" :color="loadingColor" custom-class="van-switch__loading"></van-loading>
  5. </view>
  6. </view></uni-shadow-root>
  7. </template>
  8. <wxs src="../wxs/utils.wxs" module="utils"></wxs>
  9. <script>
  10. import VanLoading from '../loading/index.vue'
  11. global['__wxVueOptions'] = {components:{'van-loading': VanLoading}}
  12. global['__wxRoute'] = 'vant/switch/index'
  13. import { VantComponent } from '../common/component';
  14. import { BLUE, GRAY_DARK } from '../common/color';
  15. VantComponent({
  16. field: true,
  17. classes: ['node-class'],
  18. props: {
  19. checked: null,
  20. loading: Boolean,
  21. disabled: Boolean,
  22. activeColor: String,
  23. inactiveColor: String,
  24. size: {
  25. type: String,
  26. value: '30px'
  27. },
  28. activeValue: {
  29. type: null,
  30. value: true
  31. },
  32. inactiveValue: {
  33. type: null,
  34. value: false
  35. }
  36. },
  37. watch: {
  38. checked(value) {
  39. const loadingColor = this.getLoadingColor(value);
  40. this.setData({ value, loadingColor });
  41. }
  42. },
  43. created() {
  44. const { checked: value } = this.data;
  45. const loadingColor = this.getLoadingColor(value);
  46. this.setData({ value, loadingColor });
  47. },
  48. methods: {
  49. getLoadingColor(checked) {
  50. const { activeColor, inactiveColor } = this.data;
  51. return checked ? activeColor || BLUE : inactiveColor || GRAY_DARK;
  52. },
  53. onClick() {
  54. const { activeValue, inactiveValue } = this.data;
  55. if (!this.data.disabled && !this.data.loading) {
  56. const checked = this.data.checked === activeValue;
  57. const value = checked ? inactiveValue : activeValue;
  58. this.$emit('input', value);
  59. this.$emit('change', value);
  60. }
  61. }
  62. }
  63. });
  64. export default global['__wxComponents']['vant/switch/index']
  65. </script>
  66. <style platform="mp-weixin">
  67. @import '../common/index.css';.van-switch{position:relative;display:inline-block;box-sizing:initial;width:2em;width:var(--switch-width,2em);height:1em;height:var(--switch-height,1em);background-color:#fff;background-color:var(--switch-background-color,#fff);border:1px solid rgba(0,0,0,.1);border:var(--switch-border,1px solid rgba(0,0,0,.1));border-radius:1em;border-radius:var(--switch-node-size,1em);transition:background-color .3s;transition:background-color var(--switch-transition-duration,.3s)}.van-switch__node{position:absolute;top:0;left:0;border-radius:100%;z-index:1;z-index:var(--switch-node-z-index,1);width:1em;width:var(--switch-node-size,1em);height:1em;height:var(--switch-node-size,1em);background-color:#fff;background-color:var(--switch-node-background-color,#fff);box-shadow:0 3px 1px 0 rgba(0,0,0,.05),0 2px 2px 0 rgba(0,0,0,.1),0 3px 3px 0 rgba(0,0,0,.05);box-shadow:var(--switch-node-box-shadow,0 3px 1px 0 rgba(0,0,0,.05),0 2px 2px 0 rgba(0,0,0,.1),0 3px 3px 0 rgba(0,0,0,.05));transition:-webkit-transform .3s cubic-bezier(.3,1.05,.4,1.05);transition:transform .3s cubic-bezier(.3,1.05,.4,1.05);transition:transform .3s cubic-bezier(.3,1.05,.4,1.05),-webkit-transform .3s cubic-bezier(.3,1.05,.4,1.05);transition:-webkit-transform var(--switch-transition-duration,.3s) cubic-bezier(.3,1.05,.4,1.05);transition:transform var(--switch-transition-duration,.3s) cubic-bezier(.3,1.05,.4,1.05);transition:transform var(--switch-transition-duration,.3s) cubic-bezier(.3,1.05,.4,1.05),-webkit-transform var(--switch-transition-duration,.3s) cubic-bezier(.3,1.05,.4,1.05)}.van-switch__loading{position:absolute!important;top:25%;left:25%;width:50%;height:50%}.van-switch--on{background-color:#1989fa;background-color:var(--switch-on-background-color,#1989fa)}.van-switch--on .van-switch__node{-webkit-transform:translateX(1em);transform:translateX(1em);-webkit-transform:translateX(calc(var(--switch-width, 2em) - var(--switch-node-size, 1em)));transform:translateX(calc(var(--switch-width, 2em) - var(--switch-node-size, 1em)))}.van-switch--disabled{opacity:.4;opacity:var(--switch-disabled-opacity,.4)}
  68. </style>