index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { VantComponent } from '../common/component';
  2. VantComponent({
  3. relation: {
  4. name: 'collapse-item',
  5. type: 'descendant',
  6. linked(child) {
  7. this.children.push(child);
  8. },
  9. unlinked(child) {
  10. this.children = this.children.filter((item) => item !== child);
  11. }
  12. },
  13. props: {
  14. value: {
  15. type: null,
  16. observer: 'updateExpanded'
  17. },
  18. accordion: {
  19. type: Boolean,
  20. observer: 'updateExpanded'
  21. },
  22. border: {
  23. type: Boolean,
  24. value: true
  25. }
  26. },
  27. beforeCreate() {
  28. this.children = [];
  29. },
  30. methods: {
  31. updateExpanded() {
  32. this.children.forEach((child) => {
  33. child.updateExpanded();
  34. });
  35. },
  36. switch(name, expanded) {
  37. const { accordion, value } = this.data;
  38. if (!accordion) {
  39. name = expanded
  40. ? (value || []).concat(name)
  41. : (value || []).filter((activeName) => activeName !== name);
  42. }
  43. else {
  44. name = expanded ? name : '';
  45. }
  46. this.$emit('change', name);
  47. this.$emit('input', name);
  48. }
  49. }
  50. });