index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { use } from '../utils';
  2. import Info from '../info';
  3. const [sfc, bem] = use('badge');
  4. export default sfc({
  5. props: {
  6. url: String,
  7. info: [String, Number],
  8. title: String
  9. },
  10. inject: ['vanBadgeGroup'],
  11. created() {
  12. this.parent.badges.push(this);
  13. },
  14. beforeDestroy() {
  15. this.parent.badges = this.parent.badges.filter(item => item !== this);
  16. },
  17. computed: {
  18. parent() {
  19. if (process.env.NODE_ENV !== 'production' && !this.vanBadgeGroup) {
  20. console.error('[Vant] Badge needs to be child of BadgeGroup');
  21. }
  22. return this.vanBadgeGroup;
  23. },
  24. select() {
  25. return this.parent.badges.indexOf(this) === +this.parent.activeKey;
  26. }
  27. },
  28. methods: {
  29. onClick() {
  30. const index = this.parent.badges.indexOf(this);
  31. this.$emit('click', index);
  32. this.parent.$emit('change', index);
  33. }
  34. },
  35. render(h) {
  36. return (
  37. <a
  38. href={this.url}
  39. class={[bem({ select: this.select }), 'van-hairline']}
  40. onClick={this.onClick}
  41. >
  42. <div class={bem('text')}>
  43. {this.title}
  44. <Info info={this.info} class={bem('info')} />
  45. </div>
  46. </a>
  47. );
  48. }
  49. });