checkbox-group.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 name = `${prefix}-checkbox-group`;
  12. let CheckBoxGroup = class CheckBoxGroup extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.externalClasses = [`${prefix}-class`];
  16. this.relations = {
  17. '../checkbox/checkbox': {
  18. type: 'descendant',
  19. },
  20. };
  21. this.data = {
  22. prefix,
  23. classPrefix: name,
  24. checkboxOptions: [],
  25. };
  26. this.properties = Object.assign(Object.assign({}, props), { borderless: {
  27. type: Boolean,
  28. value: false,
  29. } });
  30. this.observers = {
  31. value() {
  32. this.updateChildren();
  33. },
  34. options() {
  35. this.initWithOptions();
  36. },
  37. };
  38. this.lifetimes = {
  39. ready() {
  40. this.setCheckall();
  41. },
  42. };
  43. this.controlledProps = [
  44. {
  45. key: 'value',
  46. event: 'change',
  47. },
  48. ];
  49. this.$checkAll = null;
  50. this.methods = {
  51. getChilds() {
  52. let items = this.$children;
  53. if (!items.length) {
  54. items = this.selectAllComponents(`.${prefix}-checkbox-option`);
  55. }
  56. return items || [];
  57. },
  58. updateChildren() {
  59. const items = this.getChilds();
  60. const { value } = this.data;
  61. if (items.length > 0) {
  62. items.forEach((item) => {
  63. !item.data.checkAll &&
  64. item.setData({
  65. checked: value === null || value === void 0 ? void 0 : value.includes(item.data.value),
  66. });
  67. });
  68. if (items.some((item) => item.data.checkAll)) {
  69. this.setCheckall();
  70. }
  71. }
  72. },
  73. updateValue({ value, checked, checkAll, indeterminate }) {
  74. let { value: newValue } = this.data;
  75. const { max } = this.data;
  76. const keySet = new Set(this.getChilds().map((item) => item.data.value));
  77. newValue = newValue.filter((value) => keySet.has(value));
  78. if (max && checked && newValue.length === max)
  79. return;
  80. if (checkAll) {
  81. const items = this.getChilds();
  82. newValue =
  83. !checked && indeterminate
  84. ? items
  85. .filter(({ data }) => !(data.disabled && !newValue.includes(data.value)))
  86. .map((item) => item.data.value)
  87. : items
  88. .filter(({ data }) => {
  89. if (data.disabled) {
  90. return newValue.includes(data.value);
  91. }
  92. return checked && !data.checkAll;
  93. })
  94. .map(({ data }) => data.value);
  95. }
  96. else if (checked) {
  97. newValue = newValue.concat(value);
  98. }
  99. else {
  100. const index = newValue.findIndex((v) => v === value);
  101. newValue.splice(index, 1);
  102. }
  103. this._trigger('change', { value: newValue });
  104. },
  105. initWithOptions() {
  106. const { options, value } = this.data;
  107. if (!(options === null || options === void 0 ? void 0 : options.length) || !Array.isArray(options))
  108. return;
  109. const checkboxOptions = options.map((item) => {
  110. const isLabel = ['number', 'string'].includes(typeof item);
  111. return isLabel
  112. ? {
  113. label: `${item}`,
  114. value: item,
  115. checked: value === null || value === void 0 ? void 0 : value.includes(item),
  116. }
  117. : Object.assign(Object.assign({}, item), { checked: value === null || value === void 0 ? void 0 : value.includes(item.value) });
  118. });
  119. this.setData({
  120. checkboxOptions,
  121. });
  122. },
  123. handleInnerChildChange(e) {
  124. var _a;
  125. const { item } = e.target.dataset;
  126. const { checked } = e.detail;
  127. const rect = {};
  128. if (item.checkAll) {
  129. rect.indeterminate = (_a = this.$checkAll) === null || _a === void 0 ? void 0 : _a.data.indeterminate;
  130. }
  131. this.updateValue(Object.assign(Object.assign(Object.assign({}, item), { checked }), rect));
  132. },
  133. setCheckall() {
  134. const items = this.getChilds();
  135. if (!this.$checkAll) {
  136. this.$checkAll = items.find((item) => item.data.checkAll);
  137. }
  138. if (!this.$checkAll)
  139. return;
  140. const { value } = this.data;
  141. const valueSet = new Set(value.filter((val) => val !== this.$checkAll.data.value));
  142. const isCheckall = items.every((item) => (item.data.checkAll ? true : valueSet.has(item.data.value)));
  143. this.$checkAll.setData({
  144. checked: valueSet.size > 0,
  145. indeterminate: !isCheckall,
  146. });
  147. },
  148. };
  149. }
  150. };
  151. CheckBoxGroup = __decorate([
  152. wxComponent()
  153. ], CheckBoxGroup);
  154. export default CheckBoxGroup;