index.js 1.0KB

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