checkbox.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import Props from './props';
  10. const { prefix } = config;
  11. const classPrefix = `${prefix}-checkbox`;
  12. let CheckBox = class CheckBox extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.externalClasses = [
  16. `${prefix}-class`,
  17. `${prefix}-class-label`,
  18. `${prefix}-class-icon`,
  19. `${prefix}-class-content`,
  20. `${prefix}-class-border`,
  21. ];
  22. this.behaviors = ['wx://form-field'];
  23. this.relations = {
  24. '../checkbox-group/checkbox-group': {
  25. type: 'ancestor',
  26. },
  27. };
  28. this.options = {
  29. multipleSlots: true,
  30. };
  31. this.properties = Object.assign(Object.assign({}, Props), { defaultChecked: {
  32. type: null,
  33. value: undefined,
  34. } });
  35. // 组件的内部数据
  36. this.data = {
  37. classPrefix,
  38. prefix,
  39. active: false,
  40. halfChecked: false,
  41. optionLinked: false,
  42. canCancel: false,
  43. };
  44. this.lifetimes = {
  45. attached() {
  46. this.initStatus();
  47. },
  48. };
  49. this.observers = {
  50. checked: function (isChecked) {
  51. this.initStatus();
  52. this.setData({
  53. active: isChecked,
  54. });
  55. },
  56. };
  57. this.controlledProps = [
  58. {
  59. key: 'checked',
  60. event: 'change',
  61. },
  62. ];
  63. /* Methods */
  64. this.methods = {
  65. onChange(e) {
  66. const { disabled, readonly } = this.data;
  67. if (disabled || readonly)
  68. return;
  69. const { target } = e.currentTarget.dataset;
  70. const { contentDisabled } = this.data;
  71. if (target === 'text' && contentDisabled) {
  72. return;
  73. }
  74. const { value, active, checkAll, optionLinked, canCancel } = this.data;
  75. const item = { name: value, checked: !active, checkAll };
  76. const [parent] = this.getRelationNodes('../checkbox-group/checkbox-group');
  77. if (parent) {
  78. if (checkAll || optionLinked) {
  79. parent.handleCheckAll({
  80. type: 'slot',
  81. checked: !active || (this.data.halfChecked && !canCancel),
  82. option: !checkAll,
  83. name: value,
  84. });
  85. }
  86. else {
  87. parent.updateValue(item);
  88. }
  89. }
  90. else if (checkAll || optionLinked) {
  91. this.triggerEvent('toggleAll', {
  92. type: 'not-slot',
  93. checked: !active || (this.data.halfChecked && !canCancel),
  94. option: !checkAll,
  95. name: value,
  96. });
  97. }
  98. else {
  99. this._trigger('change', { checked: !active });
  100. // this.triggerEvent('change', !active);
  101. // this.toggle();
  102. }
  103. },
  104. initStatus() {
  105. const { optionLinked, indeterminate } = this.data;
  106. if (!optionLinked) {
  107. this.setData({
  108. halfChecked: indeterminate,
  109. });
  110. }
  111. },
  112. setCancel(cancel) {
  113. this.setData({
  114. canCancel: cancel,
  115. });
  116. },
  117. setDisabled(disabled) {
  118. this.setData({
  119. disabled: this.data.disabled || disabled,
  120. });
  121. },
  122. // 半选
  123. changeCheckAllHalfStatus(active) {
  124. this.setData({
  125. halfChecked: active,
  126. });
  127. },
  128. // group option
  129. setOptionLinked(linked) {
  130. this.setData({
  131. optionLinked: linked,
  132. });
  133. },
  134. };
  135. }
  136. };
  137. CheckBox = __decorate([
  138. wxComponent()
  139. ], CheckBox);
  140. export default CheckBox;
  141. //# sourceMappingURL=checkbox.js.map