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. 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. _this.set({ transition: true });
  42. });
  43. },
  44. methods: {
  45. updateExpanded: function () {
  46. if (!this.parent) {
  47. return Promise.resolve();
  48. }
  49. var _a = this.parent.data, value = _a.value, accordion = _a.accordion;
  50. var _b = this.parent.children, children = _b === void 0 ? [] : _b;
  51. var name = this.data.name;
  52. var index = children.indexOf(this);
  53. var currentName = name == null ? index : name;
  54. var expanded = accordion
  55. ? value === currentName
  56. : (value || []).some(function (name) { return name === currentName; });
  57. var stack = [];
  58. if (expanded !== this.data.expanded) {
  59. stack.push(this.updateStyle(expanded));
  60. }
  61. stack.push(this.set({ index: index, expanded: expanded }));
  62. return Promise.all(stack);
  63. },
  64. updateStyle: function (expanded) {
  65. var _this = this;
  66. return this.getRect('.van-collapse-item__content')
  67. .then(function (rect) { return rect.height; })
  68. .then(function (height) {
  69. if (expanded) {
  70. return _this.set({
  71. contentHeight: height ? height + "px" : 'auto'
  72. });
  73. }
  74. else {
  75. return _this.set({ contentHeight: height + "px" })
  76. .then(nextTick)
  77. .then(function () { return _this.set({ contentHeight: 0 }); });
  78. }
  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.set({
  93. contentHeight: 'auto'
  94. });
  95. }
  96. }
  97. }
  98. });