Skip to content

Commit

Permalink
fix: progress组件传入不合法字符串作为百分比时应按0处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangyaqi committed Mar 7, 2024
1 parent 4df12ea commit c71647a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/uni-components/src/vue/progress/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,19 @@ function useProgressState(props: ProgressProps) {
return `width: ${currentPercent.value}%;background-color: ${backgroundColor}`
})
const realPercent = computed(() => {
if (
typeof props.percent === 'string' &&
!/^-?\d*\.?\d*$/.test(props.percent)
) {
return 0
}
// 确保最终计算时使用的是 Number 类型的值,并且在有效范围内。
let realValue = parseFloat(props.percent as string)
realValue < 0 && (realValue = 0)
realValue > 100 && (realValue = 100)
if (Number.isNaN(realValue) || realValue < 0) {
realValue = 0
} else if (realValue > 100) {
realValue = 100
}
return realValue
})

Expand Down

0 comments on commit c71647a

Please sign in to comment.