collapse.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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}-collapse`;
  12. let Collapse = class Collapse extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.options = {
  16. addGlobalClass: true,
  17. };
  18. this.externalClasses = [`${prefix}-class`];
  19. this.relations = {
  20. './collapse-panel': {
  21. type: 'descendant',
  22. linked() {
  23. this.updateExpanded();
  24. },
  25. },
  26. };
  27. this.controlledProps = [
  28. {
  29. key: 'value',
  30. event: 'change',
  31. },
  32. ];
  33. this.properties = props;
  34. this.data = {
  35. prefix,
  36. classPrefix: name,
  37. };
  38. this.observers = {
  39. 'value, expandMutex '() {
  40. this.updateExpanded();
  41. },
  42. };
  43. this.methods = {
  44. updateExpanded() {
  45. const panels = this.getRelationNodes('./collapse-panel');
  46. if (panels.length === 0)
  47. return;
  48. panels.forEach((child) => {
  49. child.updateExpanded(this.properties.value);
  50. });
  51. },
  52. switch(panelValue) {
  53. const { expandMutex, value: activeValues } = this.properties;
  54. let value = [];
  55. const hit = activeValues.indexOf(panelValue);
  56. if (hit > -1) {
  57. value = activeValues.filter((item) => item !== panelValue);
  58. }
  59. else {
  60. value = expandMutex ? [panelValue] : activeValues.concat(panelValue);
  61. }
  62. this._trigger('change', { value });
  63. },
  64. };
  65. }
  66. };
  67. Collapse = __decorate([
  68. wxComponent()
  69. ], Collapse);
  70. export default Collapse;