index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { VantComponent } from '../common/component';
  2. VantComponent({
  3. relation: {
  4. name: 'sidebar-item',
  5. type: 'descendant',
  6. linked(target) {
  7. this.children.push(target);
  8. this.setActive(this.data.activeKey);
  9. },
  10. unlinked(target) {
  11. this.children = this.children.filter((item) => item !== target);
  12. this.setActive(this.data.activeKey);
  13. }
  14. },
  15. props: {
  16. activeKey: {
  17. type: Number,
  18. value: 0,
  19. observer: 'setActive'
  20. }
  21. },
  22. beforeCreate() {
  23. this.children = [];
  24. this.currentActive = -1;
  25. },
  26. methods: {
  27. setActive(activeKey) {
  28. const { children, currentActive } = this;
  29. if (!children.length) {
  30. return Promise.resolve();
  31. }
  32. this.currentActive = activeKey;
  33. const stack = [];
  34. if (currentActive !== activeKey && children[currentActive]) {
  35. stack.push(children[currentActive].setActive(false));
  36. }
  37. if (children[activeKey]) {
  38. stack.push(children[activeKey].setActive(true));
  39. }
  40. return Promise.all(stack);
  41. }
  42. }
  43. });