checkbox-group.js 6.1 KB

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