index.js 2.3 KB

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