index.vue 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <uni-shadow-root class="vant-dist-checkbox-index"><view :class="(utils.bem('checkbox', [{ horizontal: direction === 'horizontal' }]))+' custom-class'">
  3. <view v-if="labelPosition === 'left'" :class="'label-class '+(utils.bem('checkbox__label', [labelPosition, { disabled: disabled || parentDisabled }]))" @click="onClickLabel">
  4. <slot></slot>
  5. </view>
  6. <view class="van-checkbox__icon-wrap" @click="toggle">
  7. <slot v-if="useIconSlot" name="icon"></slot>
  8. <van-icon v-else name="success" size="0.8em" :class="utils.bem('checkbox__icon', [shape, { disabled: disabled || parentDisabled, checked: value }])" :style="computed.iconStyle(checkedColor, value, disabled, parentDisabled, iconSize)" custom-class="icon-class" custom-style="line-height: 1.25em;"></van-icon>
  9. </view>
  10. <view v-if="labelPosition === 'right'" :class="'label-class '+(utils.bem('checkbox__label', [labelPosition, { disabled: disabled || parentDisabled }]))" @click="onClickLabel">
  11. <slot></slot>
  12. </view>
  13. </view></uni-shadow-root>
  14. </template>
  15. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="computed"></wxs>
  16. <script>
  17. import VanIcon from '../icon/index.vue'
  18. global['__wxVueOptions'] = {components:{'van-icon': VanIcon}}
  19. global['__wxRoute'] = 'vant/dist/checkbox/index'
  20. import { useParent } from '../common/relation';
  21. import { VantComponent } from '../common/component';
  22. function emit(target, value) {
  23. target.$emit('input', value);
  24. target.$emit('change', value);
  25. }
  26. VantComponent({
  27. field: true,
  28. relation: useParent('checkbox-group'),
  29. classes: ['icon-class', 'label-class'],
  30. props: {
  31. value: Boolean,
  32. disabled: Boolean,
  33. useIconSlot: Boolean,
  34. checkedColor: String,
  35. labelPosition: {
  36. type: String,
  37. value: 'right',
  38. },
  39. labelDisabled: Boolean,
  40. shape: {
  41. type: String,
  42. value: 'round',
  43. },
  44. iconSize: {
  45. type: null,
  46. value: 20,
  47. },
  48. },
  49. data: {
  50. parentDisabled: false,
  51. direction: 'vertical',
  52. },
  53. methods: {
  54. emitChange(value) {
  55. if (this.parent) {
  56. this.setParentValue(this.parent, value);
  57. }
  58. else {
  59. emit(this, value);
  60. }
  61. },
  62. toggle() {
  63. const { parentDisabled, disabled, value } = this.data;
  64. if (!disabled && !parentDisabled) {
  65. this.emitChange(!value);
  66. }
  67. },
  68. onClickLabel() {
  69. const { labelDisabled, parentDisabled, disabled, value } = this.data;
  70. if (!disabled && !labelDisabled && !parentDisabled) {
  71. this.emitChange(!value);
  72. }
  73. },
  74. setParentValue(parent, value) {
  75. const parentValue = parent.data.value.slice();
  76. const { name } = this.data;
  77. const { max } = parent.data;
  78. if (value) {
  79. if (max && parentValue.length >= max) {
  80. return;
  81. }
  82. if (parentValue.indexOf(name) === -1) {
  83. parentValue.push(name);
  84. emit(parent, parentValue);
  85. }
  86. }
  87. else {
  88. const index = parentValue.indexOf(name);
  89. if (index !== -1) {
  90. parentValue.splice(index, 1);
  91. emit(parent, parentValue);
  92. }
  93. }
  94. },
  95. },
  96. });
  97. export default global['__wxComponents']['vant/dist/checkbox/index']
  98. </script>
  99. <style platform="mp-weixin">
  100. @import '../common/index.css';.van-checkbox{display:flex;align-items:center;overflow:hidden;-webkit-user-select:none;user-select:none}.van-checkbox--horizontal{margin-right:12px}.van-checkbox__icon-wrap,.van-checkbox__label{line-height:20px;line-height:var(--checkbox-size,20px)}.van-checkbox__icon-wrap{flex:none}.van-checkbox__icon{display:flex;align-items: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;padding-left:10px;padding-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}
  101. </style>