index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { VantComponent } from '../common/component';
  2. import { setContentAnimate } from './animate';
  3. VantComponent({
  4. classes: ['title-class', 'content-class'],
  5. relation: {
  6. name: 'collapse',
  7. type: 'ancestor',
  8. current: 'collapse-item',
  9. },
  10. props: {
  11. name: null,
  12. title: null,
  13. value: null,
  14. icon: String,
  15. label: String,
  16. disabled: Boolean,
  17. clickable: Boolean,
  18. border: {
  19. type: Boolean,
  20. value: true,
  21. },
  22. isLink: {
  23. type: Boolean,
  24. value: true,
  25. },
  26. },
  27. data: {
  28. expanded: false,
  29. },
  30. mounted() {
  31. this.updateExpanded();
  32. this.mounted = true;
  33. },
  34. methods: {
  35. updateExpanded() {
  36. if (!this.parent) {
  37. return;
  38. }
  39. const { value, accordion } = this.parent.data;
  40. const { children = [] } = this.parent;
  41. const { name } = this.data;
  42. const index = children.indexOf(this);
  43. const currentName = name == null ? index : name;
  44. const expanded = accordion
  45. ? value === currentName
  46. : (value || []).some((name) => name === currentName);
  47. if (expanded !== this.data.expanded) {
  48. setContentAnimate(this, expanded, this.mounted);
  49. }
  50. this.setData({ index, expanded });
  51. },
  52. onClick() {
  53. if (this.data.disabled) {
  54. return;
  55. }
  56. const { name, expanded } = this.data;
  57. const index = this.parent.children.indexOf(this);
  58. const currentName = name == null ? index : name;
  59. this.parent.switch(currentName, !expanded);
  60. },
  61. },
  62. });