index.vue 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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" size="50%" 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. VantComponent({
  15. field: true,
  16. classes: ['node-class'],
  17. props: {
  18. checked: null,
  19. loading: Boolean,
  20. disabled: Boolean,
  21. activeColor: String,
  22. inactiveColor: String,
  23. size: {
  24. type: String,
  25. value: '30px'
  26. },
  27. activeValue: {
  28. type: null,
  29. value: true
  30. },
  31. inactiveValue: {
  32. type: null,
  33. value: false
  34. }
  35. },
  36. watch: {
  37. checked(value) {
  38. this.set({ value });
  39. }
  40. },
  41. created() {
  42. this.set({ value: this.data.checked });
  43. },
  44. methods: {
  45. onClick() {
  46. const { activeValue, inactiveValue } = this.data;
  47. if (!this.data.disabled && !this.data.loading) {
  48. const checked = this.data.checked === activeValue;
  49. const value = checked ? inactiveValue : activeValue;
  50. this.$emit('input', value);
  51. this.$emit('change', value);
  52. }
  53. }
  54. }
  55. });
  56. export default global['__wxComponents']['vant/switch/index']
  57. </script>
  58. <style platform="mp-weixin">
  59. @import '../common/index.css';.van-switch{display:inline-block;position:relative;width:2em;border:1px solid rgba(0,0,0,.1);border-radius:1em;box-sizing:initial;transition:background-color .3s}.van-switch,.van-switch__node{height:1em;background-color:#fff}.van-switch__node{top:0;left:0;position:absolute;border-radius:100%;width:1em;z-index:1;transition:.3s;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)}.van-switch__loading{top:25%;left:25%;position:absolute!important}.van-switch--on{background-color:#1989fa}.van-switch--on .van-switch__node{-webkit-transform:translateX(1em);transform:translateX(1em)}.van-switch--disabled{opacity:.4}
  60. </style>