index.js 1.7 KB

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