index.js 3.2 KB

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