index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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" size="0.8em" :class="utils.bem('checkbox__icon', [shape, { disabled, checked: value }])" :style="'font-size: '+(sizeWithUnit)+';'+(checkedColor && value && !disabled ? 'border-color:' + checkedColor + '; background-color:' + checkedColor : '')" custom-class="icon-class" custom-style="line-height: 1.25em;"></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. import { addUnit } from '../common/utils';
  19. function emit(target, value) {
  20. target.$emit('input', value);
  21. target.$emit('change', value);
  22. }
  23. VantComponent({
  24. field: true,
  25. relation: {
  26. name: 'checkbox-group',
  27. type: 'ancestor',
  28. linked(target) {
  29. this.parent = target;
  30. },
  31. unlinked() {
  32. this.parent = null;
  33. }
  34. },
  35. classes: ['icon-class', 'label-class'],
  36. props: {
  37. value: Boolean,
  38. disabled: Boolean,
  39. useIconSlot: Boolean,
  40. checkedColor: String,
  41. labelPosition: String,
  42. labelDisabled: Boolean,
  43. shape: {
  44. type: String,
  45. value: 'round'
  46. },
  47. iconSize: {
  48. type: null,
  49. observer: 'setSizeWithUnit'
  50. }
  51. },
  52. data: {
  53. sizeWithUnit: '20px'
  54. },
  55. methods: {
  56. emitChange(value) {
  57. if (this.parent) {
  58. this.setParentValue(this.parent, value);
  59. }
  60. else {
  61. emit(this, value);
  62. }
  63. },
  64. toggle() {
  65. const { disabled, value } = this.data;
  66. if (!disabled) {
  67. this.emitChange(!value);
  68. }
  69. },
  70. onClickLabel() {
  71. const { labelDisabled, disabled, value } = this.data;
  72. if (!disabled && !labelDisabled) {
  73. this.emitChange(!value);
  74. }
  75. },
  76. setParentValue(parent, value) {
  77. const parentValue = parent.data.value.slice();
  78. const { name } = this.data;
  79. const { max } = parent.data;
  80. if (value) {
  81. if (max && parentValue.length >= max) {
  82. return;
  83. }
  84. if (parentValue.indexOf(name) === -1) {
  85. parentValue.push(name);
  86. emit(parent, parentValue);
  87. }
  88. }
  89. else {
  90. const index = parentValue.indexOf(name);
  91. if (index !== -1) {
  92. parentValue.splice(index, 1);
  93. emit(parent, parentValue);
  94. }
  95. }
  96. },
  97. setSizeWithUnit(size) {
  98. this.set({
  99. sizeWithUnit: addUnit(size)
  100. });
  101. },
  102. }
  103. });
  104. export default global['__wxComponents']['vant/checkbox/index']
  105. </script>
  106. <style platform="mp-weixin">
  107. @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;line-height:var(--checkbox-size,20px)}.van-checkbox__icon-wrap{-webkit-flex:none;flex:none}.van-checkbox__icon{display:-webkit-flex;display:flex;-webkit-align-items:center;align-items:center;-webkit-justify-content:center;justify-content:center;box-sizing:border-box;width:1em;height:1em;color:transparent;text-align:center;transition-property:color,border-color,background-color;font-size:20px;font-size:var(--checkbox-size,20px);border:1px solid #c8c9cc;border:1px solid var(--checkbox-border-color,#c8c9cc);transition-duration:.2s;transition-duration:var(--checkbox-transition-duration,.2s)}.van-checkbox__icon--round{border-radius:100%}.van-checkbox__icon--checked{color:#fff;color:var(--white,#fff);background-color:#1989fa;background-color:var(--checkbox-checked-icon-color,#1989fa);border-color:#1989fa;border-color:var(--checkbox-checked-icon-color,#1989fa)}.van-checkbox__icon--disabled{background-color:#ebedf0;background-color:var(--checkbox-disabled-background-color,#ebedf0);border-color:#c8c9cc;border-color:var(--checkbox-disabled-icon-color,#c8c9cc)}.van-checkbox__icon--disabled.van-checkbox__icon--checked{color:#c8c9cc;color:var(--checkbox-disabled-icon-color,#c8c9cc)}.van-checkbox__label{word-wrap:break-word;margin-left:10px;margin-left:var(--checkbox-label-margin,10px);color:#323233;color:var(--checkbox-label-color,#323233)}.van-checkbox__label--left{float:left;margin:0 10px 0 0;margin:0 var(--checkbox-label-margin,10px) 0 0}.van-checkbox__label--disabled{color:#c8c9cc;color:var(--checkbox-disabled-label-color,#c8c9cc)}.van-checkbox__label:empty{margin:0}
  108. </style>