index.js 3.2 KB

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