index.js 959 B

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