index.js 1.4 KB

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