12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { VantComponent } from '../common/component';
  2. VantComponent({
  3. classes: ['title-class', 'content-class'],
  4. relation: {
  5. name: 'collapse',
  6. type: 'ancestor',
  7. linked(parent) {
  8. this.parent = parent;
  9. }
  10. },
  11. props: {
  12. name: null,
  13. title: null,
  14. value: null,
  15. icon: String,
  16. label: String,
  17. disabled: Boolean,
  18. border: {
  19. type: Boolean,
  20. value: true
  21. },
  22. isLink: {
  23. type: Boolean,
  24. value: true
  25. }
  26. },
  27. data: {
  28. contentHeight: 0,
  29. expanded: false
  30. },
  31. beforeCreate() {
  32. this.animation = wx.createAnimation({
  33. duration: 300,
  34. timingFunction: 'ease-in-out'
  35. });
  36. },
  37. methods: {
  38. updateExpanded() {
  39. if (!this.parent) {
  40. return null;
  41. }
  42. const { value, accordion, items } = this.parent.data;
  43. const { name } = this.data;
  44. const index = items.indexOf(this);
  45. const currentName = name == null ? index : name;
  46. const expanded = accordion
  47. ? value === currentName
  48. : value.some(name => name === currentName);
  49. if (expanded !== this.data.expanded) {
  50. this.updateStyle(expanded);
  51. }
  52. this.set({ expanded });
  53. },
  54. updateStyle(expanded) {
  55. this.getRect('.van-collapse-item__content').then(res => {
  56. const animationData = this.animation
  57. .height(expanded ? res.height : 0)
  58. .step()
  59. .export();
  60. if (expanded) {
  61. this.set({ animationData });
  62. }
  63. else {
  64. this.set({
  65. contentHeight: res.height + 'px'
  66. }, () => {
  67. setTimeout(() => {
  68. this.set({ animationData });
  69. }, 20);
  70. });
  71. }
  72. });
  73. },
  74. onClick() {
  75. if (this.data.disabled) {
  76. return;
  77. }
  78. const { name, expanded } = this.data;
  79. const index = this.parent.data.items.indexOf(this);
  80. const currentName = name == null ? index : name;
  81. this.parent.switch(currentName, !expanded);
  82. },
  83. onTransitionEnd() {
  84. if (this.data.expanded) {
  85. this.set({
  86. contentHeight: 'auto'
  87. });
  88. }
  89. }
  90. }
  91. });