index.js 1.8 KB

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