index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. import styleToCssString from '../helpers/styleToCssString'
  4. import { colors } from '../helpers/colors'
  5. const defaultColors = {
  6. normal: colors.positive,
  7. progress: colors.positive,
  8. error: colors.assertive,
  9. success: colors.balanced,
  10. }
  11. baseComponent({
  12. properties: {
  13. prefixCls: {
  14. type: String,
  15. value: 'wux-progress',
  16. },
  17. percent: {
  18. type: Number,
  19. value: 0,
  20. observer: 'updateStyle',
  21. },
  22. strokeWidth: {
  23. type: Number,
  24. value: 10,
  25. observer: 'updateStyle',
  26. },
  27. activeColor: {
  28. type: String,
  29. value: '',
  30. observer: 'updateStyle',
  31. },
  32. backgroundColor: {
  33. type: String,
  34. value: '#f3f3f3',
  35. },
  36. status: {
  37. type: String,
  38. value: 'normal',
  39. observer: 'updateStyle',
  40. },
  41. shape: {
  42. type: String,
  43. value: 'round',
  44. },
  45. barStyle: {
  46. type: [String, Object],
  47. value: '',
  48. observer(newVal) {
  49. this.setData({
  50. extStyle: styleToCssString(newVal),
  51. })
  52. },
  53. },
  54. showInfo: {
  55. type: Boolean,
  56. value: false,
  57. },
  58. },
  59. data: {
  60. width: 0,
  61. style: '',
  62. extStyle: '',
  63. },
  64. computed: {
  65. classes: ['prefixCls, shape, status', function(prefixCls, shape, status) {
  66. const wrap = classNames(prefixCls, {
  67. [`${prefixCls}--${shape}`]: shape,
  68. [`${prefixCls}--${status}`]: status,
  69. })
  70. const outer = `${prefixCls}__outer`
  71. const inner = `${prefixCls}__inner`
  72. const bar = `${prefixCls}__bar`
  73. const text = `${prefixCls}__text`
  74. return {
  75. wrap,
  76. outer,
  77. inner,
  78. bar,
  79. text,
  80. }
  81. }],
  82. },
  83. methods: {
  84. updateStyle(opts = {}) {
  85. const { percent, strokeWidth, activeColor, status } = Object.assign({}, this.data, opts)
  86. const width = percent < 0 ? 0 : percent > 100 ? 100 : percent
  87. const height = strokeWidth > 0 ? strokeWidth : 10
  88. const backgroundColor = activeColor ? activeColor : (defaultColors[status] || defaultColors['normal'])
  89. const style = `background-color: ${backgroundColor}; width: ${width}%; height: ${height}px;`
  90. this.setData({
  91. width,
  92. style,
  93. })
  94. },
  95. },
  96. attached() {
  97. this.updateStyle()
  98. },
  99. })