|
|
@@ -2,33 +2,26 @@
|
|
|
<div class="nut-progress">
|
|
|
<div
|
|
|
class="nut-progress-outer"
|
|
|
+ :id="'nut-progress-outer-taro-' + randRef"
|
|
|
ref="progressOuter"
|
|
|
- :class="[
|
|
|
- showText && !textInside ? 'nut-progress-outer-part' : '',
|
|
|
- size ? 'nut-progress-' + size : ''
|
|
|
- ]"
|
|
|
+ :class="[showText && !textInside ? 'nut-progress-outer-part' : '', size ? 'nut-progress-' + size : '']"
|
|
|
:style="{ height: height }"
|
|
|
>
|
|
|
- <div
|
|
|
- :class="['nut-progress-inner', status == 'active' ? 'nut-active' : '']"
|
|
|
- :style="bgStyle"
|
|
|
- >
|
|
|
+ <div :class="['nut-progress-inner', status == 'active' ? 'nut-active' : '']" :style="bgStyle">
|
|
|
<div
|
|
|
class="nut-progress-text nut-progress-insidetext"
|
|
|
+ ref="insideText"
|
|
|
+ :id="'nut-progress-insidetext-taro-' + randRef"
|
|
|
:style="{ lineHeight: height, left: left }"
|
|
|
v-if="showText && textInside"
|
|
|
>
|
|
|
- <span :style="textStyle">{{ percentage }}%</span>
|
|
|
+ <span :style="textStyle">{{ percentage }}{{ isShowPercentage ? '%' : '' }}</span>
|
|
|
</div>
|
|
|
</div>
|
|
|
</div>
|
|
|
- <div
|
|
|
- class="nut-progress-text"
|
|
|
- :style="{ lineHeight: height }"
|
|
|
- v-if="showText && !textInside"
|
|
|
- >
|
|
|
+ <div class="nut-progress-text" :style="{ lineHeight: height }" v-if="showText && !textInside">
|
|
|
<template v-if="status == 'text' || status == 'active'">
|
|
|
- <span :style="textStyle">{{ percentage }}%</span>
|
|
|
+ <span :style="textStyle">{{ percentage }}{{ isShowPercentage ? '%' : '' }} </span>
|
|
|
</template>
|
|
|
<template v-else-if="status == 'icon'">
|
|
|
<nut-icon size="16px" :name="iconName" :color="iconColor"></nut-icon>
|
|
|
@@ -38,18 +31,10 @@
|
|
|
</template>
|
|
|
|
|
|
<script lang="ts">
|
|
|
-import {
|
|
|
- computed,
|
|
|
- onMounted,
|
|
|
- provide,
|
|
|
- reactive,
|
|
|
- nextTick,
|
|
|
- ref,
|
|
|
- getCurrentInstance,
|
|
|
- watch
|
|
|
-} from 'vue';
|
|
|
+import { computed, onMounted, provide, reactive, nextTick, ref, watch } from 'vue';
|
|
|
import { createComponent } from '../../utils/create';
|
|
|
-import Taro, { eventCenter } from '@tarojs/taro';
|
|
|
+import Taro, { eventCenter, getCurrentInstance } from '@tarojs/taro';
|
|
|
+import { log } from 'lzutf8';
|
|
|
const { create } = createComponent('progress');
|
|
|
export default create({
|
|
|
props: {
|
|
|
@@ -93,12 +78,19 @@ export default create({
|
|
|
iconColor: {
|
|
|
type: String,
|
|
|
default: '#439422'
|
|
|
+ },
|
|
|
+ isShowPercentage: {
|
|
|
+ type: Boolean,
|
|
|
+ default: true
|
|
|
}
|
|
|
},
|
|
|
setup(props, { emit }) {
|
|
|
const height = ref(props.strokeWidth + 'px');
|
|
|
const progressOuter = ref<any>();
|
|
|
const left = ref();
|
|
|
+ const insideText = ref();
|
|
|
+ const refRandomId = Math.random().toString(36).slice(-8);
|
|
|
+ const randRef = ref(refRandomId);
|
|
|
const bgStyle = computed(() => {
|
|
|
return {
|
|
|
width: props.percentage + '%',
|
|
|
@@ -110,38 +102,51 @@ export default create({
|
|
|
color: props.textColor || ''
|
|
|
};
|
|
|
});
|
|
|
- const slideLeft = async (values: String | Number) => {
|
|
|
+ const slideLeft = async (values: string | number) => {
|
|
|
if (Taro.getEnv() === 'WEB') {
|
|
|
- setTimeout(() => {
|
|
|
- left.value =
|
|
|
- progressOuter.value.offsetWidth * Number(values) * 0.01 - 4 + 'px';
|
|
|
- }, 200);
|
|
|
+ if (!props.textInside) return;
|
|
|
+ let offsetWidth = progressOuter.value.offsetWidth;
|
|
|
+ let percentageWidth = progressOuter.value.offsetWidth * Number(values) * 0.01;
|
|
|
+ let insideTextWidth = insideText.value.offsetWidth;
|
|
|
+ left.value = percentageWidth - 5 + 'px';
|
|
|
+ if (offsetWidth == percentageWidth) {
|
|
|
+ left.value = percentageWidth - insideTextWidth + 'px';
|
|
|
+ }
|
|
|
} else {
|
|
|
- setTimeout(() => {
|
|
|
- const query = Taro.createSelectorQuery() as any;
|
|
|
- query
|
|
|
- .select('.nut-progress-outer')
|
|
|
- .boundingClientRect((rec: any) => {
|
|
|
- left.value = rec.width * Number(values) * 0.01 - 4 + 'px';
|
|
|
- })
|
|
|
- .exec();
|
|
|
- }, 200);
|
|
|
+ if (!props.textInside) return;
|
|
|
+ const query = Taro.createSelectorQuery() as any;
|
|
|
+ query.select('#nut-progress-outer-taro-' + randRef.value).boundingClientRect();
|
|
|
+ query.select('#nut-progress-insidetext-taro-' + randRef.value).boundingClientRect();
|
|
|
+ query.exec((res: any) => {
|
|
|
+ let offsetWidth = res[0].width;
|
|
|
+ let percentageWidth = res[0].width * Number(values) * 0.01;
|
|
|
+ let insideTextWidth = res[1].width;
|
|
|
+ left.value = percentageWidth - 4 + 'px';
|
|
|
+ if (offsetWidth == percentageWidth) {
|
|
|
+ left.value = percentageWidth - insideTextWidth + 'px';
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
};
|
|
|
watch(
|
|
|
() => props.percentage,
|
|
|
(values) => {
|
|
|
slideLeft(values);
|
|
|
- },
|
|
|
- { immediate: true }
|
|
|
+ }
|
|
|
);
|
|
|
- onMounted(() => {});
|
|
|
+ onMounted(() => {
|
|
|
+ eventCenter.once((getCurrentInstance() as any).router.onReady, () => {
|
|
|
+ slideLeft(props.percentage);
|
|
|
+ });
|
|
|
+ });
|
|
|
return {
|
|
|
height,
|
|
|
bgStyle,
|
|
|
textStyle,
|
|
|
progressOuter,
|
|
|
- left
|
|
|
+ left,
|
|
|
+ insideText,
|
|
|
+ randRef
|
|
|
};
|
|
|
}
|
|
|
});
|