index.js 1.2 KB

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