From 377635cb9a84e7586d40bf3079e1094f8ccc0a9a Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 25 Jan 2019 20:51:02 +0800 Subject: [PATCH] [improvement] Progress: jsx (#2615) --- packages/progress/index.js | 86 +++++++++++++++ packages/progress/index.vue | 102 ------------------ .../test/__snapshots__/demo.spec.js.snap | 10 +- .../test/__snapshots__/index.spec.js.snap | 4 +- 4 files changed, 93 insertions(+), 109 deletions(-) create mode 100644 packages/progress/index.js delete mode 100644 packages/progress/index.vue diff --git a/packages/progress/index.js b/packages/progress/index.js new file mode 100644 index 000000000..3f9d2850c --- /dev/null +++ b/packages/progress/index.js @@ -0,0 +1,86 @@ +import { use, isDef } from '../utils'; +import { BLUE, WHITE } from '../utils/color'; + +const [sfc, bem] = use('progress'); + +export default sfc({ + props: { + inactive: Boolean, + pivotText: String, + pivotColor: String, + percentage: { + type: Number, + required: true, + validator: value => value >= 0 && value <= 100 + }, + showPivot: { + type: Boolean, + default: true + }, + color: { + type: String, + default: BLUE + }, + textColor: { + type: String, + default: WHITE + } + }, + + data() { + return { + pivotWidth: 0, + progressWidth: 0 + }; + }, + + mounted() { + this.getWidth(); + }, + + watch: { + showPivot() { + this.getWidth(); + }, + + pivotText() { + this.getWidth(); + } + }, + + methods: { + getWidth() { + this.progressWidth = this.$el.offsetWidth; + this.pivotWidth = this.$refs.pivot ? this.$refs.pivot.offsetWidth : 0; + } + }, + + render(h) { + const { pivotText, percentage } = this; + const text = isDef(pivotText) ? pivotText : percentage + '%'; + const showPivot = this.showPivot && text; + const background = this.inactive ? '#cacaca' : this.color; + + const pivotStyle = { + color: this.textColor, + background: this.pivotColor || background + }; + + const portionStyle = { + background, + width: ((this.progressWidth - this.pivotWidth) * percentage) / 100 + 'px' + }; + + return ( +
+ + {showPivot && ( + + {text} + + )} + +
+ ); + } +}); diff --git a/packages/progress/index.vue b/packages/progress/index.vue deleted file mode 100644 index dd8d0c140..000000000 --- a/packages/progress/index.vue +++ /dev/null @@ -1,102 +0,0 @@ - - - diff --git a/packages/progress/test/__snapshots__/demo.spec.js.snap b/packages/progress/test/__snapshots__/demo.spec.js.snap index 5260086da..12c10274d 100644 --- a/packages/progress/test/__snapshots__/demo.spec.js.snap +++ b/packages/progress/test/__snapshots__/demo.spec.js.snap @@ -3,15 +3,15 @@ exports[`renders demo correctly 1`] = `
-
50%
+
50%
-
50%
+
50%
-
橙色
-
红色
-
紫色
+
橙色
+
红色
+
紫色
`; diff --git a/packages/progress/test/__snapshots__/index.spec.js.snap b/packages/progress/test/__snapshots__/index.spec.js.snap index 2b20d1053..e54cd5fd8 100644 --- a/packages/progress/test/__snapshots__/index.spec.js.snap +++ b/packages/progress/test/__snapshots__/index.spec.js.snap @@ -1,5 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`calc width 1`] = `
`; +exports[`calc width 1`] = `
`; -exports[`calc width 2`] = `
test
`; +exports[`calc width 2`] = `
test
`;