index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <view>
  3. <sjs src="../wxs/utils.sjs" module="utils" />
  4. <view class="van-checkbox custom-class">
  5. <view class="van-checkbox__icon-wrap" @tap="toggle">
  6. <slot :wx:if="useIconSlot" name="icon" />
  7. <van-icon
  8. wx:else
  9. name="success"
  10. size="0.8em"
  11. :class="utils.bem('checkbox__icon', [shape, { disabled, checked: value }])"
  12. :style="'font-size: ' + sizeWithUnit + ';' + (checkedColor && value && !disabled ? 'border-color:' + checkedColor + '; background-color:' + checkedColor : '')"
  13. custom-class="icon-class"
  14. custom-style="line-height: 1.25em;"
  15. />
  16. </view>
  17. <view :class="'label-class ' + utils.bem('checkbox__label', [labelPosition, { disabled }])" @tap="onClickLabel"><slot /></view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. import { VantComponent } from '../common/component';
  23. import { addUnit } from '../common/utils';
  24. function emit(target, value) {
  25. target.$emit('input', value);
  26. target.$emit('change', value);
  27. }
  28. export default {
  29. data() {
  30. return {
  31. sizeWithUnit: '20px'
  32. };
  33. },
  34. field: true,
  35. '../checkbox-group/index': {
  36. name: 'checkbox-group',
  37. type: 'ancestor',
  38. linked(target) {
  39. this.parent = target;
  40. },
  41. unlinked() {
  42. this.parent = null;
  43. }
  44. },
  45. classes: ['icon-class', 'label-class'],
  46. props: {
  47. value: Boolean,
  48. disabled: Boolean,
  49. useIconSlot: Boolean,
  50. checkedColor: String,
  51. labelPosition: String,
  52. labelDisabled: Boolean,
  53. shape: {
  54. type: String,
  55. default: 'round'
  56. },
  57. iconSize: {
  58. type: null
  59. }
  60. },
  61. methods: {
  62. emitChange(value) {
  63. if (this.parent) {
  64. this.setParentValue(this.parent, value);
  65. } else {
  66. emit(this, value);
  67. }
  68. },
  69. toggle() {
  70. const { disabled, value } = this;
  71. if (!disabled) {
  72. this.emitChange(!value);
  73. }
  74. },
  75. onClickLabel() {
  76. const { labelDisabled, disabled, value } = this;
  77. if (!disabled && !labelDisabled) {
  78. this.emitChange(!value);
  79. }
  80. },
  81. setParentValue(parent, value) {
  82. const parentValue = parent.data.value.slice();
  83. const { name } = this;
  84. const { max } = parent.data;
  85. if (value) {
  86. if (max && parentValue.length >= max) {
  87. return;
  88. }
  89. if (parentValue.indexOf(name) === -1) {
  90. parentValue.push(name);
  91. emit(parent, parentValue);
  92. }
  93. } else {
  94. const index = parentValue.indexOf(name);
  95. if (index !== -1) {
  96. parentValue.splice(index, 1);
  97. emit(parent, parentValue);
  98. }
  99. }
  100. },
  101. setSizeWithUnit(size) {
  102. this.set({
  103. sizeWithUnit: addUnit(size)
  104. });
  105. }
  106. },
  107. watch: {
  108. iconSize: {
  109. handler: function (size) {
  110. this.set({
  111. sizeWithUnit: addUnit(size)
  112. });
  113. },
  114. immediate: true
  115. }
  116. }
  117. };
  118. </script>
  119. <style>
  120. @import '../common/index.ttss';
  121. .van-checkbox {
  122. display: -webkit-flex;
  123. display: flex;
  124. overflow: hidden;
  125. -webkit-user-select: none;
  126. user-select: none;
  127. }
  128. .van-checkbox__icon-wrap,
  129. .van-checkbox__label {
  130. line-height: 20px;
  131. line-height: var(--checkbox-size, 20px);
  132. }
  133. .van-checkbox__icon-wrap {
  134. -webkit-flex: none;
  135. flex: none;
  136. }
  137. .van-checkbox__icon {
  138. display: -webkit-flex;
  139. display: flex;
  140. -webkit-align-items: center;
  141. align-items: center;
  142. -webkit-justify-content: center;
  143. justify-content: center;
  144. box-sizing: border-box;
  145. width: 1em;
  146. height: 1em;
  147. color: transparent;
  148. text-align: center;
  149. transition-property: color, border-color, background-color;
  150. font-size: 20px;
  151. font-size: var(--checkbox-size, 20px);
  152. border: 1px solid #c8c9cc;
  153. border: 1px solid var(--checkbox-border-color, #c8c9cc);
  154. transition-duration: 0.2s;
  155. transition-duration: var(--checkbox-transition-duration, 0.2s);
  156. }
  157. .van-checkbox__icon--round {
  158. border-radius: 100%;
  159. }
  160. .van-checkbox__icon--checked {
  161. color: #fff;
  162. color: var(--white, #fff);
  163. background-color: #a98f76;
  164. background-color: var(--checkbox-checked-icon-color, #a98f76);
  165. border-color: #a98f76;
  166. border-color: var(--checkbox-checked-icon-color, #a98f76);
  167. }
  168. .van-checkbox__icon--disabled {
  169. background-color: #ebedf0;
  170. background-color: var(--checkbox-disabled-background-color, #ebedf0);
  171. border-color: #c8c9cc;
  172. border-color: var(--checkbox-disabled-icon-color, #c8c9cc);
  173. }
  174. .van-checkbox__icon--disabled.van-checkbox__icon--checked {
  175. color: #c8c9cc;
  176. color: var(--checkbox-disabled-icon-color, #c8c9cc);
  177. }
  178. .van-checkbox__label {
  179. word-wrap: break-word;
  180. margin-left: 10px;
  181. margin-left: var(--checkbox-label-margin, 10px);
  182. color: #323233;
  183. color: var(--checkbox-label-color, #323233);
  184. }
  185. .van-checkbox__label--left {
  186. float: left;
  187. margin: 0 10px 0 0;
  188. margin: 0 var(--checkbox-label-margin, 10px) 0 0;
  189. }
  190. .van-checkbox__label--disabled {
  191. color: #c8c9cc;
  192. color: var(--checkbox-disabled-label-color, #c8c9cc);
  193. }
  194. .van-checkbox__label:empty {
  195. margin: 0;
  196. }
  197. </style>