12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { VantComponent } from '../common/component';
  2. import { isNumber } from '../common/utils';
  3. VantComponent({
  4. relation: {
  5. name: 'badge',
  6. type: 'descendant',
  7. linked(target) {
  8. this.badges.push(target);
  9. this.setActive();
  10. },
  11. unlinked(target) {
  12. this.badges = this.badges.filter(item => item !== target);
  13. this.setActive();
  14. }
  15. },
  16. props: {
  17. active: {
  18. type: Number,
  19. value: 0
  20. }
  21. },
  22. watch: {
  23. active: 'setActive'
  24. },
  25. beforeCreate() {
  26. this.badges = [];
  27. this.currentActive = -1;
  28. },
  29. methods: {
  30. setActive(badge) {
  31. let { active } = this.data;
  32. const { badges } = this;
  33. if (badge && !isNumber(badge)) {
  34. active = badges.indexOf(badge);
  35. }
  36. if (active === this.currentActive) {
  37. return;
  38. }
  39. if (this.currentActive !== -1 && badges[this.currentActive]) {
  40. this.$emit('change', active);
  41. badges[this.currentActive].setActive(false);
  42. }
  43. if (badges[active]) {
  44. badges[active].setActive(true);
  45. this.currentActive = active;
  46. }
  47. }
  48. }
  49. });