index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var component_1 = require('../common/component');
  4. var nextTick = function () {
  5. return new Promise(function (resolve) {
  6. return setTimeout(resolve, 20);
  7. });
  8. };
  9. component_1.VantComponent({
  10. classes: ['title-class', 'content-class'],
  11. relation: {
  12. name: 'collapse',
  13. type: 'ancestor',
  14. current: 'collapse-item',
  15. },
  16. props: {
  17. name: null,
  18. title: null,
  19. value: null,
  20. icon: String,
  21. label: String,
  22. disabled: Boolean,
  23. clickable: Boolean,
  24. border: {
  25. type: Boolean,
  26. value: true,
  27. },
  28. isLink: {
  29. type: Boolean,
  30. value: true,
  31. },
  32. },
  33. data: {
  34. contentHeight: 0,
  35. expanded: false,
  36. transition: false,
  37. },
  38. mounted: function () {
  39. var _this = this;
  40. this.updateExpanded()
  41. .then(nextTick)
  42. .then(function () {
  43. var data = { transition: true };
  44. if (_this.data.expanded) {
  45. data.contentHeight = 'auto';
  46. }
  47. _this.setData(data);
  48. });
  49. },
  50. methods: {
  51. updateExpanded: function () {
  52. if (!this.parent) {
  53. return Promise.resolve();
  54. }
  55. var _a = this.parent.data,
  56. value = _a.value,
  57. accordion = _a.accordion;
  58. var _b = this.parent.children,
  59. children = _b === void 0 ? [] : _b;
  60. var name = this.data.name;
  61. var index = children.indexOf(this);
  62. var currentName = name == null ? index : name;
  63. var expanded = accordion
  64. ? value === currentName
  65. : (value || []).some(function (name) {
  66. return name === currentName;
  67. });
  68. var stack = [];
  69. if (expanded !== this.data.expanded) {
  70. stack.push(this.updateStyle(expanded));
  71. }
  72. stack.push(this.set({ index: index, expanded: expanded }));
  73. return Promise.all(stack);
  74. },
  75. updateStyle: function (expanded) {
  76. var _this = this;
  77. return this.getRect('.van-collapse-item__content')
  78. .then(function (rect) {
  79. return rect.height;
  80. })
  81. .then(function (height) {
  82. if (expanded) {
  83. return _this.set({
  84. contentHeight: height ? height + 'px' : 'auto',
  85. });
  86. }
  87. return _this
  88. .set({ contentHeight: height + 'px' })
  89. .then(nextTick)
  90. .then(function () {
  91. return _this.set({ contentHeight: 0 });
  92. });
  93. });
  94. },
  95. onClick: function () {
  96. if (this.data.disabled) {
  97. return;
  98. }
  99. var _a = this.data,
  100. name = _a.name,
  101. expanded = _a.expanded;
  102. var index = this.parent.children.indexOf(this);
  103. var currentName = name == null ? index : name;
  104. this.parent.switch(currentName, !expanded);
  105. },
  106. onTransitionEnd: function () {
  107. if (this.data.expanded) {
  108. this.setData({
  109. contentHeight: 'auto',
  110. });
  111. }
  112. },
  113. },
  114. });