From b12fdad91642f2fbdf80639b777a860f0483bdeb Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Wed, 26 Aug 2020 15:07:55 +0800 Subject: [PATCH] refactor(Progress): refactor with composition api --- src/progress/index.js | 118 ++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 55 deletions(-) diff --git a/src/progress/index.js b/src/progress/index.js index f12ca19a9..2c5799310 100644 --- a/src/progress/index.js +++ b/src/progress/index.js @@ -1,3 +1,4 @@ +import { ref, watch, computed, nextTick, reactive, onMounted } from 'vue'; import { createNamespace, isDef, addUnit } from '../utils'; const [createComponent, bem] = createNamespace('progress'); @@ -22,63 +23,70 @@ export default createComponent({ }, }, - data() { - return { + setup(props) { + const rootRef = ref(null); + const pivotRef = ref(null); + + const state = reactive({ pivotWidth: 0, - progressWidth: 0, - }; - }, + rootWidth: 0, + }); - mounted() { - this.setWidth(); - }, - - watch: { - showPivot: 'setWidth', - pivotText: 'setWidth', - }, - - methods: { - setWidth() { - this.$nextTick(() => { - this.progressWidth = this.$el.offsetWidth; - this.pivotWidth = this.$refs.pivot ? this.$refs.pivot.offsetWidth : 0; - }); - }, - }, - - render() { - 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, - left: `${((this.progressWidth - this.pivotWidth) * percentage) / 100}px`, - background: this.pivotColor || background, - }; - - const portionStyle = { - background, - width: (this.progressWidth * percentage) / 100 + 'px', - }; - - const wrapperStyle = { - background: this.trackColor, - height: addUnit(this.strokeWidth), - }; - - return ( -
- - {showPivot && ( - - {text} - - )} - -
+ const background = computed(() => + props.inactive ? '#cacaca' : props.color ); + + const setWidth = () => { + nextTick(() => { + state.pivotWidth = pivotRef.value ? pivotRef.value.offsetWidth : 0; + state.rootWidth = rootRef.value.offsetWidth; + }); + }; + + const renderPivot = () => { + const { rootWidth, pivotWidth } = state; + const { textColor, pivotText, pivotColor, percentage } = props; + const text = isDef(pivotText) ? pivotText : percentage + '%'; + const show = props.showPivot && text; + + if (show) { + const left = ((rootWidth - pivotWidth) * percentage) / 100; + const style = { + color: textColor, + left: `${left}px`, + background: pivotColor || background.value, + }; + + return ( + + {text} + + ); + } + }; + + watch([() => props.showPivot, () => props.pivotText], setWidth); + + onMounted(setWidth); + + return () => { + const { trackColor, percentage, strokeWidth } = props; + const rootStyle = { + background: trackColor, + height: addUnit(strokeWidth), + }; + const portionStyle = { + background: background.value, + width: (state.rootWidth * percentage) / 100 + 'px', + }; + + return ( +
+ + {renderPivot()} + +
+ ); + }; }, });