index.vue 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <uni-shadow-root class="vant-checkbox-index"><view class="van-checkbox custom-class">
  3. <view class="van-checkbox__icon-wrap" @click="toggle">
  4. <slot v-if="useIconSlot" name="icon"></slot>
  5. <van-icon v-else name="success" :class="utils.bem('checkbox__icon', [shape, { disabled, checked: value }])" :style="checkedColor && value && !disabled ? 'border-color:' + checkedColor + '; background-color:' + checkedColor : ''" custom-class="icon-class" custom-style="line-height: 20px;"></van-icon>
  6. </view>
  7. <view :class="'label-class '+(utils.bem('checkbox__label', [labelPosition, { disabled }]))" @click="onClickLabel">
  8. <slot></slot>
  9. </view>
  10. </view></uni-shadow-root>
  11. </template>
  12. <wxs src="../wxs/utils.wxs" module="utils"></wxs>
  13. <script>
  14. import VanIcon from '../icon/index.vue'
  15. global['__wxVueOptions'] = {components:{'van-icon': VanIcon}}
  16. global['__wxRoute'] = 'vant/checkbox/index'
  17. import { VantComponent } from '../common/component';
  18. function emit(target, value) {
  19. target.$emit('input', value);
  20. target.$emit('change', value);
  21. }
  22. VantComponent({
  23. field: true,
  24. relation: {
  25. name: 'checkbox-group',
  26. type: 'ancestor',
  27. linked(target) {
  28. this.parent = target;
  29. },
  30. unlinked() {
  31. this.parent = null;
  32. }
  33. },
  34. classes: ['icon-class', 'label-class'],
  35. props: {
  36. value: Boolean,
  37. disabled: Boolean,
  38. useIconSlot: Boolean,
  39. checkedColor: String,
  40. labelPosition: String,
  41. labelDisabled: Boolean,
  42. shape: {
  43. type: String,
  44. value: 'round'
  45. }
  46. },
  47. methods: {
  48. emitChange(value) {
  49. if (this.parent) {
  50. this.setParentValue(this.parent, value);
  51. }
  52. else {
  53. emit(this, value);
  54. }
  55. },
  56. toggle() {
  57. const { disabled, value } = this.data;
  58. if (!disabled) {
  59. this.emitChange(!value);
  60. }
  61. },
  62. onClickLabel() {
  63. const { labelDisabled, disabled, value } = this.data;
  64. if (!disabled && !labelDisabled) {
  65. this.emitChange(!value);
  66. }
  67. },
  68. setParentValue(parent, value) {
  69. const parentValue = parent.data.value.slice();
  70. const { name } = this.data;
  71. const { max } = parent.data;
  72. if (value) {
  73. if (max && parentValue.length >= max) {
  74. return;
  75. }
  76. if (parentValue.indexOf(name) === -1) {
  77. parentValue.push(name);
  78. emit(parent, parentValue);
  79. }
  80. }
  81. else {
  82. const index = parentValue.indexOf(name);
  83. if (index !== -1) {
  84. parentValue.splice(index, 1);
  85. emit(parent, parentValue);
  86. }
  87. }
  88. }
  89. }
  90. });
  91. export default global['__wxComponents']['vant/checkbox/index']
  92. </script>
  93. <style platform="mp-weixin">
  94. @import '../common/index.css';.van-checkbox{display:-webkit-flex;display:flex;overflow:hidden;-webkit-user-select:none;user-select:none}.van-checkbox__icon-wrap,.van-checkbox__label{line-height:20px}.van-checkbox__icon-wrap{-webkit-flex:none;flex:none}.van-checkbox__icon{display:block;width:20px;height:20px;font-size:14px;color:transparent;text-align:center;border:1px solid #e5e5e5;box-sizing:border-box;transition:.2s}.van-checkbox__icon--round{border-radius:100%}.van-checkbox__icon--checked{color:#fff;background-color:#1989fa;border-color:#1989fa}.van-checkbox__icon--disabled{background-color:#eee;border-color:#c9c9c9}.van-checkbox__icon--disabled.van-checkbox__icon--checked{color:#c9c9c9}.van-checkbox__label{margin-left:10px;color:#333;word-break:break-all}.van-checkbox__label--left{float:left;margin:0 10px 0 0}.van-checkbox__label--disabled{color:#c9c9c9}.van-checkbox__label:empty{margin:0}
  95. </style>