index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { use, isDef } from '../utils';
  2. import { BLUE, WHITE } from '../utils/color';
  3. var _use = use('progress'),
  4. sfc = _use[0],
  5. bem = _use[1];
  6. export default sfc({
  7. props: {
  8. inactive: Boolean,
  9. pivotText: String,
  10. pivotColor: String,
  11. percentage: {
  12. type: Number,
  13. required: true,
  14. validator: function validator(value) {
  15. return value >= 0 && value <= 100;
  16. }
  17. },
  18. showPivot: {
  19. type: Boolean,
  20. "default": true
  21. },
  22. color: {
  23. type: String,
  24. "default": BLUE
  25. },
  26. textColor: {
  27. type: String,
  28. "default": WHITE
  29. }
  30. },
  31. data: function data() {
  32. return {
  33. pivotWidth: 0,
  34. progressWidth: 0
  35. };
  36. },
  37. mounted: function mounted() {
  38. this.getWidth();
  39. },
  40. watch: {
  41. showPivot: function showPivot() {
  42. this.getWidth();
  43. },
  44. pivotText: function pivotText() {
  45. this.getWidth();
  46. }
  47. },
  48. methods: {
  49. getWidth: function getWidth() {
  50. var _this = this;
  51. this.$nextTick(function () {
  52. _this.progressWidth = _this.$el.offsetWidth;
  53. _this.pivotWidth = _this.$refs.pivot ? _this.$refs.pivot.offsetWidth : 0;
  54. });
  55. }
  56. },
  57. render: function render(h) {
  58. var pivotText = this.pivotText,
  59. percentage = this.percentage;
  60. var text = isDef(pivotText) ? pivotText : percentage + '%';
  61. var showPivot = this.showPivot && text;
  62. var background = this.inactive ? '#cacaca' : this.color;
  63. var pivotStyle = {
  64. color: this.textColor,
  65. background: this.pivotColor || background
  66. };
  67. var portionStyle = {
  68. background: background,
  69. width: (this.progressWidth - this.pivotWidth) * percentage / 100 + 'px'
  70. };
  71. return h("div", {
  72. "class": bem()
  73. }, [h("span", {
  74. "class": bem('portion', {
  75. 'with-pivot': showPivot
  76. }),
  77. "style": portionStyle
  78. }, [showPivot && h("span", {
  79. "ref": "pivot",
  80. "style": pivotStyle,
  81. "class": bem('pivot')
  82. }, [text])])]);
  83. }
  84. });