mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 02:16:12 +08:00
refactor(Progress): refactor with composition api
This commit is contained in:
parent
463174e735
commit
b12fdad916
@ -1,3 +1,4 @@
|
|||||||
|
import { ref, watch, computed, nextTick, reactive, onMounted } from 'vue';
|
||||||
import { createNamespace, isDef, addUnit } from '../utils';
|
import { createNamespace, isDef, addUnit } from '../utils';
|
||||||
|
|
||||||
const [createComponent, bem] = createNamespace('progress');
|
const [createComponent, bem] = createNamespace('progress');
|
||||||
@ -22,63 +23,70 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
data() {
|
setup(props) {
|
||||||
return {
|
const rootRef = ref(null);
|
||||||
|
const pivotRef = ref(null);
|
||||||
|
|
||||||
|
const state = reactive({
|
||||||
pivotWidth: 0,
|
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 background = computed(() =>
|
||||||
const { pivotText, percentage } = this;
|
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 text = isDef(pivotText) ? pivotText : percentage + '%';
|
||||||
const showPivot = this.showPivot && text;
|
const show = props.showPivot && text;
|
||||||
const background = this.inactive ? '#cacaca' : this.color;
|
|
||||||
|
|
||||||
const pivotStyle = {
|
if (show) {
|
||||||
color: this.textColor,
|
const left = ((rootWidth - pivotWidth) * percentage) / 100;
|
||||||
left: `${((this.progressWidth - this.pivotWidth) * percentage) / 100}px`,
|
const style = {
|
||||||
background: this.pivotColor || background,
|
color: textColor,
|
||||||
};
|
left: `${left}px`,
|
||||||
|
background: pivotColor || background.value,
|
||||||
const portionStyle = {
|
|
||||||
background,
|
|
||||||
width: (this.progressWidth * percentage) / 100 + 'px',
|
|
||||||
};
|
|
||||||
|
|
||||||
const wrapperStyle = {
|
|
||||||
background: this.trackColor,
|
|
||||||
height: addUnit(this.strokeWidth),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class={bem()} style={wrapperStyle}>
|
<span ref={pivotRef} style={style} class={bem('pivot')}>
|
||||||
<span class={bem('portion')} style={portionStyle}>
|
|
||||||
{showPivot && (
|
|
||||||
<span ref="pivot" style={pivotStyle} class={bem('pivot')}>
|
|
||||||
{text}
|
{text}
|
||||||
</span>
|
</span>
|
||||||
)}
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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 (
|
||||||
|
<div ref={rootRef} class={bem()} style={rootStyle}>
|
||||||
|
<span class={bem('portion')} style={portionStyle}>
|
||||||
|
{renderPivot()}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user