checkbox-group.js 7.1 KB

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