1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { VantComponent } from '../common/component';
  2. import { BLUE } from '../common/color';
  3. VantComponent({
  4. props: {
  5. inactive: Boolean,
  6. percentage: Number,
  7. pivotText: String,
  8. pivotColor: String,
  9. showPivot: {
  10. type: Boolean,
  11. value: true
  12. },
  13. color: {
  14. type: String,
  15. value: BLUE
  16. },
  17. textColor: {
  18. type: String,
  19. value: '#fff'
  20. }
  21. },
  22. data: {
  23. pivotWidth: 0,
  24. progressWidth: 0
  25. },
  26. watch: {
  27. pivotText: 'getWidth',
  28. showPivot: 'getWidth'
  29. },
  30. computed: {
  31. portionStyle() {
  32. const width = (this.data.progressWidth - this.data.pivotWidth) * this.data.percentage / 100 + 'px';
  33. const background = this.getCurrentColor();
  34. return `width: ${width}; background: ${background}; `;
  35. },
  36. pivotStyle() {
  37. const color = this.data.textColor;
  38. const background = this.data.pivotColor || this.getCurrentColor();
  39. return `color: ${color}; background: ${background}`;
  40. },
  41. text() {
  42. return this.data.pivotText || this.data.percentage + '%';
  43. }
  44. },
  45. mounted() {
  46. this.getWidth();
  47. },
  48. methods: {
  49. getCurrentColor() {
  50. return this.data.inactive ? '#cacaca' : this.data.color;
  51. },
  52. getWidth() {
  53. this.getRect('.van-progress').then(rect => {
  54. this.set({
  55. progressWidth: rect.width
  56. });
  57. });
  58. this.getRect('.van-progress__pivot').then(rect => {
  59. this.set({
  60. pivotWidth: rect.width || 0
  61. });
  62. });
  63. }
  64. }
  65. });