index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { isDef, createNamespace } from '../utils';
  2. import { isNumeric } from '../utils/validate/number';
  3. var _createNamespace = createNamespace('badge'),
  4. createComponent = _createNamespace[0],
  5. bem = _createNamespace[1];
  6. export default createComponent({
  7. props: {
  8. dot: Boolean,
  9. max: [Number, String],
  10. color: String,
  11. content: [Number, String],
  12. tag: {
  13. type: String,
  14. default: 'div'
  15. }
  16. },
  17. methods: {
  18. hasContent: function hasContent() {
  19. return !!(this.$scopedSlots.content || isDef(this.content) && this.content !== '');
  20. },
  21. renderContent: function renderContent() {
  22. var dot = this.dot,
  23. max = this.max,
  24. content = this.content;
  25. if (!dot && this.hasContent()) {
  26. if (this.$scopedSlots.content) {
  27. return this.$scopedSlots.content();
  28. }
  29. if (isDef(max) && isNumeric(content) && +content > max) {
  30. return max + "+";
  31. }
  32. return content;
  33. }
  34. },
  35. renderBadge: function renderBadge() {
  36. var h = this.$createElement;
  37. if (this.hasContent() || this.dot) {
  38. return h("div", {
  39. "class": bem({
  40. dot: this.dot,
  41. fixed: !!this.$scopedSlots.default
  42. }),
  43. "style": {
  44. background: this.color
  45. }
  46. }, [this.renderContent()]);
  47. }
  48. }
  49. },
  50. render: function render() {
  51. var h = arguments[0];
  52. if (this.$scopedSlots.default) {
  53. var tag = this.tag;
  54. return h(tag, {
  55. "class": bem('wrapper')
  56. }, [this.$scopedSlots.default(), this.renderBadge()]);
  57. }
  58. return this.renderBadge();
  59. }
  60. });