checkbox-group.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. linked() {
  20. this.updateChildren();
  21. },
  22. },
  23. };
  24. this.data = {
  25. classPrefix: name,
  26. checkboxOptions: [],
  27. };
  28. this.properties = Object.assign(Object.assign({}, Props), { defaultValue: {
  29. type: null,
  30. value: undefined,
  31. } });
  32. this.observers = {
  33. value: function () {
  34. this.updateChildren();
  35. },
  36. };
  37. this.lifetimes = {
  38. attached() {
  39. this.handleCreateMulCheckbox();
  40. },
  41. };
  42. this.controlledProps = [
  43. {
  44. key: 'value',
  45. event: 'change',
  46. },
  47. ];
  48. this.methods = {
  49. getChilds() {
  50. let items = this.getRelationNodes('../checkbox/checkbox');
  51. if (!items.length) {
  52. items = this.selectAllComponents('.t-checkbox-option');
  53. }
  54. return items || [];
  55. },
  56. // slot插入选项
  57. updateChildren() {
  58. const items = this.getChilds();
  59. const { value, disabled } = this.data;
  60. if (items.length > 0) {
  61. items.forEach((item) => {
  62. !item.data.checkAll &&
  63. item.setData({
  64. checked: (value === null || value === void 0 ? void 0 : value.indexOf(item.data.value)) > -1,
  65. });
  66. item.setDisabled(disabled);
  67. });
  68. // 关联可全选项
  69. if (items.findIndex((item) => item.data.checkAll) > -1) {
  70. items.forEach((item) => {
  71. item.setOptionLinked(true);
  72. });
  73. this.handleHalfCheck(items.length);
  74. }
  75. }
  76. },
  77. updateValue({ name, checked }) {
  78. const { value, max } = this.data;
  79. let newValue = value;
  80. if (max && checked && newValue.length === max) {
  81. return;
  82. }
  83. if (checked) {
  84. newValue = newValue.concat(name);
  85. }
  86. else {
  87. const index = newValue.findIndex((v) => v === name);
  88. newValue.splice(index, 1);
  89. }
  90. this._trigger('change', { value: newValue });
  91. },
  92. // 支持自定义options
  93. handleCreateMulCheckbox() {
  94. const { options } = this.data;
  95. // 数字数组|字符串数组|对像数组
  96. if (!(options === null || options === void 0 ? void 0 : options.length) || !Array.isArray(options)) {
  97. return;
  98. }
  99. const optionsValue = [];
  100. try {
  101. options.forEach((element) => {
  102. const typeName = typeof element;
  103. if (typeName === 'number' || typeName === 'string') {
  104. optionsValue.push({
  105. label: `${element}`,
  106. value: element,
  107. });
  108. }
  109. else if (typeName === 'object') {
  110. optionsValue.push(Object.assign({}, element));
  111. }
  112. });
  113. this.setData({
  114. checkboxOptions: optionsValue,
  115. });
  116. this.updateChildren();
  117. }
  118. catch (error) {
  119. console.error('error', error);
  120. }
  121. },
  122. // 处理全选
  123. handleCheckAll(e) {
  124. const { checked, option, name } = e.detail || e;
  125. const items = this.getChilds();
  126. if (!option) {
  127. if (!(items === null || items === void 0 ? void 0 : items.length)) {
  128. return;
  129. }
  130. this._trigger('change', {
  131. value: items
  132. .map((item) => {
  133. if (item.data.disabled) {
  134. return this.data.value.includes(item.data.value) ? item.data.value : '';
  135. }
  136. return checked && !item.data.checkAll ? item.data.value : '';
  137. })
  138. .filter((val) => val),
  139. });
  140. }
  141. else {
  142. this.updateValue({ name, checked });
  143. }
  144. },
  145. // 处理options半选
  146. handleHalfCheck(len) {
  147. var _a;
  148. const items = this.getChilds();
  149. const checkboxOptions = items.filter((i) => !i.data.checkAll);
  150. const all = checkboxOptions.map((item) => item.data.value);
  151. const enableValue = checkboxOptions
  152. .filter((i) => !i.data.disabled)
  153. .map((item) => item.data.value);
  154. const currentVal = Array.from(new Set((_a = this.data.value) === null || _a === void 0 ? void 0 : _a.filter((i) => all.indexOf(i) > -1)));
  155. const element = items.find((item) => item.data.checkAll);
  156. if (currentVal.length) {
  157. element === null || element === void 0 ? void 0 : element.setData({ checked: true });
  158. element === null || element === void 0 ? void 0 : element.changeCheckAllHalfStatus(currentVal.length !== len - 1);
  159. // 取消全选
  160. element === null || element === void 0 ? void 0 : element.setCancel(enableValue.every((val) => currentVal.includes(val)));
  161. }
  162. else {
  163. element === null || element === void 0 ? void 0 : element.setData({ checked: false });
  164. }
  165. },
  166. // 设置可全选option选项
  167. handleOptionLinked() {
  168. const items = this.selectAllComponents('.t-checkbox-option');
  169. if (this.data.checkboxOptions.length) {
  170. items.forEach((item) => {
  171. item.setOptionLinked(true);
  172. });
  173. }
  174. },
  175. };
  176. }
  177. };
  178. CheckBoxGroup = __decorate([
  179. wxComponent()
  180. ], CheckBoxGroup);
  181. export default CheckBoxGroup;
  182. //# sourceMappingURL=checkbox-group.js.map