储能智慧云小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

checkbox-group.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 } = 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. const isLabel = ['number', 'string'].includes(typeof item);
  118. return isLabel
  119. ? {
  120. label: `${item}`,
  121. value: item,
  122. checked: value === null || value === void 0 ? void 0 : value.includes(item),
  123. }
  124. : Object.assign(Object.assign({}, item), { checked: value === null || value === void 0 ? void 0 : value.includes(item.value) });
  125. });
  126. this.setData({
  127. checkboxOptions,
  128. });
  129. },
  130. handleInnerChildChange(e) {
  131. var _a;
  132. const { item } = e.target.dataset;
  133. const { checked } = e.detail;
  134. const rect = {};
  135. if (item.checkAll) {
  136. rect.indeterminate = (_a = this.$checkAll) === null || _a === void 0 ? void 0 : _a.data.indeterminate;
  137. }
  138. this.updateValue(Object.assign(Object.assign(Object.assign({}, item), { checked, item }), rect));
  139. },
  140. setCheckall() {
  141. const items = this.getChildren();
  142. if (!this.$checkAll) {
  143. this.$checkAll = items.find((item) => item.data.checkAll);
  144. }
  145. if (!this.$checkAll)
  146. return;
  147. const { value } = this.data;
  148. const valueSet = new Set(value === null || value === void 0 ? void 0 : value.filter((val) => val !== this.$checkAll.data.value));
  149. const isCheckall = items.every((item) => (item.data.checkAll ? true : valueSet.has(item.data.value)));
  150. this.$checkAll.setData({
  151. checked: valueSet.size > 0,
  152. indeterminate: !isCheckall,
  153. });
  154. },
  155. };
  156. }
  157. };
  158. CheckBoxGroup = __decorate([
  159. wxComponent()
  160. ], CheckBoxGroup);
  161. export default CheckBoxGroup;