|
| 1 | +/** |
| 2 | + * ✔ percent 进度百分比 0-100 |
| 3 | + * ✘ show-info 在进度条右侧显示百分比 |
| 4 | + * ✘ border-radius 圆角大小 |
| 5 | + * ✘ font-size 右侧百分比字体大小 |
| 6 | + * ✔ stroke-width 进度条线的宽度 |
| 7 | + * ✔ color 进度条颜色(请使用activeColor) |
| 8 | + * ✔ activeColor 已选择的进度条的颜色 |
| 9 | + * ✔ backgroundColor 未选择的进度条的颜色 |
| 10 | + * ✔ active 进度条从左往右的动画 |
| 11 | + * ✔ active-mode backwards: 动画从头播;forwards:动画从上次结束点接着播 |
| 12 | + * ✔ duration 进度增加1%所需毫秒数 |
| 13 | + * ✔ bindactiveend 动画完成事件 |
| 14 | + */ |
| 15 | +import { |
| 16 | + JSX, |
| 17 | + useRef, |
| 18 | + forwardRef, |
| 19 | + useEffect, |
| 20 | + useState, |
| 21 | + createElement, |
| 22 | + ForwardedRef |
| 23 | +} from 'react' |
| 24 | +import { |
| 25 | + View, |
| 26 | + ViewStyle |
| 27 | +} from 'react-native' |
| 28 | +import Animated, { |
| 29 | + useSharedValue, |
| 30 | + useAnimatedStyle, |
| 31 | + withTiming, |
| 32 | + Easing, |
| 33 | + runOnJS |
| 34 | +} from 'react-native-reanimated' |
| 35 | + |
| 36 | +import useInnerProps from './getInnerListeners' |
| 37 | +import useNodesRef, { HandlerRef } from './useNodesRef' |
| 38 | +import { useLayout, useTransformStyle, extendObject } from './utils' |
| 39 | +import Portal from './mpx-portal' |
| 40 | + |
| 41 | +export interface ProgressProps { |
| 42 | + percent?: number |
| 43 | + 'stroke-width'?: number | string |
| 44 | + color?: string |
| 45 | + activeColor?: string |
| 46 | + backgroundColor?: string |
| 47 | + active?: boolean |
| 48 | + 'active-mode'?: 'backwards' | 'forwards' |
| 49 | + duration?: number |
| 50 | + bindactiveend?: (event: any) => void |
| 51 | + style?: ViewStyle & Record<string, any> |
| 52 | + 'enable-offset'?: boolean |
| 53 | + 'enable-var'?: boolean |
| 54 | + 'external-var-context'?: Record<string, any> |
| 55 | + 'parent-font-size'?: number |
| 56 | + 'parent-width'?: number |
| 57 | + 'parent-height'?: number |
| 58 | +} |
| 59 | + |
| 60 | +const Progress = forwardRef< |
| 61 | + HandlerRef<View, ProgressProps>, |
| 62 | + ProgressProps |
| 63 | +>((props: ProgressProps, ref: ForwardedRef<HandlerRef<View, ProgressProps>>): JSX.Element => { |
| 64 | + const { |
| 65 | + percent = 0, |
| 66 | + 'stroke-width': strokeWidth = 6, |
| 67 | + color, |
| 68 | + activeColor = color || '#09BB07', |
| 69 | + backgroundColor = '#EBEBEB', |
| 70 | + active = false, |
| 71 | + 'active-mode': activeMode = 'backwards', |
| 72 | + duration = 30, |
| 73 | + bindactiveend, |
| 74 | + style = {}, |
| 75 | + 'enable-var': enableVar, |
| 76 | + 'external-var-context': externalVarContext, |
| 77 | + 'parent-font-size': parentFontSize, |
| 78 | + 'parent-width': parentWidth, |
| 79 | + 'parent-height': parentHeight |
| 80 | + } = props |
| 81 | + |
| 82 | + const nodeRef = useRef(null) |
| 83 | + const propsRef = useRef({}) |
| 84 | + propsRef.current = props |
| 85 | + |
| 86 | + // 进度值状态 |
| 87 | + const [lastPercent, setLastPercent] = useState(0) |
| 88 | + const progressWidth = useSharedValue(0) |
| 89 | + |
| 90 | + const { |
| 91 | + normalStyle, |
| 92 | + hasSelfPercent, |
| 93 | + setWidth, |
| 94 | + setHeight, |
| 95 | + hasPositionFixed |
| 96 | + } = useTransformStyle(style, { |
| 97 | + enableVar, |
| 98 | + externalVarContext, |
| 99 | + parentFontSize, |
| 100 | + parentWidth, |
| 101 | + parentHeight |
| 102 | + }) |
| 103 | + |
| 104 | + const { layoutRef, layoutStyle, layoutProps } = useLayout({ |
| 105 | + props, |
| 106 | + hasSelfPercent, |
| 107 | + setWidth, |
| 108 | + setHeight, |
| 109 | + nodeRef |
| 110 | + }) |
| 111 | + |
| 112 | + useNodesRef(props, ref, nodeRef, { |
| 113 | + style: normalStyle |
| 114 | + }) |
| 115 | + |
| 116 | + // 进度条动画函数 |
| 117 | + const startProgressAnimation = (targetPercent: number, startPercent: number, animationDuration: number, onFinished?: () => void) => { |
| 118 | + // 根据 active-mode 设置起始位置 |
| 119 | + progressWidth.value = startPercent |
| 120 | + progressWidth.value = withTiming( |
| 121 | + targetPercent, |
| 122 | + { |
| 123 | + duration: animationDuration, |
| 124 | + easing: Easing.linear |
| 125 | + }, |
| 126 | + (finished) => { |
| 127 | + if (finished && onFinished) { |
| 128 | + // 在动画回调中,需要使用runOnJS回到主线程 |
| 129 | + runOnJS(onFinished)() |
| 130 | + } |
| 131 | + } |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + // 创建在主线程执行的事件回调函数 |
| 136 | + const triggerActiveEnd = (percent: number) => { |
| 137 | + if (bindactiveend) { |
| 138 | + bindactiveend({ |
| 139 | + type: 'activeend', |
| 140 | + detail: { |
| 141 | + percent: percent |
| 142 | + } |
| 143 | + }) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // 进度变化时的动画效果 |
| 148 | + useEffect(() => { |
| 149 | + const targetPercent = Math.max(0, Math.min(100, percent)) |
| 150 | + if (active) { |
| 151 | + // 根据 active-mode 确定起始位置 |
| 152 | + let startPercent |
| 153 | + if (activeMode === 'backwards') { |
| 154 | + startPercent = 0 |
| 155 | + } else { |
| 156 | + // forwards 模式:使用上次记录的百分比作为起始位置 |
| 157 | + startPercent = lastPercent |
| 158 | + } |
| 159 | + |
| 160 | + // 计算动画持续时间 |
| 161 | + const percentDiff = Math.abs(targetPercent - startPercent) |
| 162 | + const animationDuration = percentDiff * duration |
| 163 | + |
| 164 | + // 创建动画完成回调 |
| 165 | + const onAnimationFinished = () => { |
| 166 | + triggerActiveEnd(targetPercent) |
| 167 | + } |
| 168 | + |
| 169 | + // 执行动画 |
| 170 | + startProgressAnimation(targetPercent, startPercent, animationDuration, onAnimationFinished) |
| 171 | + } else { |
| 172 | + progressWidth.value = targetPercent |
| 173 | + } |
| 174 | + |
| 175 | + setLastPercent(targetPercent) |
| 176 | + }, [percent, active, activeMode, duration, bindactiveend]) |
| 177 | + |
| 178 | + // 初始化时设置进度值 |
| 179 | + useEffect(() => { |
| 180 | + if (!active) { |
| 181 | + progressWidth.value = Math.max(0, Math.min(100, percent)) |
| 182 | + } |
| 183 | + }, []) |
| 184 | + |
| 185 | + // 进度条动画样式 |
| 186 | + const animatedProgressStyle = useAnimatedStyle(() => { |
| 187 | + return { |
| 188 | + width: `${progressWidth.value}%` |
| 189 | + } |
| 190 | + }) |
| 191 | + |
| 192 | + // 确保数值类型正确 |
| 193 | + const strokeWidthNum = typeof strokeWidth === 'number' ? strokeWidth : parseInt(strokeWidth as string, 10) || 6 |
| 194 | + |
| 195 | + // 容器样式 |
| 196 | + const containerStyle: ViewStyle = extendObject({} as ViewStyle, { |
| 197 | + flexDirection: 'row' as const, |
| 198 | + alignItems: 'center' as const, |
| 199 | + width: '100%', |
| 200 | + minHeight: Math.max(strokeWidthNum, 20) |
| 201 | + }, normalStyle, layoutStyle) |
| 202 | + |
| 203 | + // 进度条背景样式 |
| 204 | + const progressBgStyle: ViewStyle = { |
| 205 | + width: '100%', |
| 206 | + height: strokeWidthNum, |
| 207 | + backgroundColor, |
| 208 | + overflow: 'hidden' |
| 209 | + } |
| 210 | + |
| 211 | + // 进度条填充样式 |
| 212 | + const progressFillStyle: ViewStyle = { |
| 213 | + height: '100%', |
| 214 | + backgroundColor: activeColor |
| 215 | + } |
| 216 | + |
| 217 | + const innerProps = useInnerProps( |
| 218 | + extendObject({}, props, layoutProps, { |
| 219 | + ref: nodeRef |
| 220 | + }), |
| 221 | + [ |
| 222 | + 'percent', |
| 223 | + 'stroke-width', |
| 224 | + 'color', |
| 225 | + 'activeColor', |
| 226 | + 'backgroundColor', |
| 227 | + 'active', |
| 228 | + 'active-mode', |
| 229 | + 'duration', |
| 230 | + 'bindactiveend' |
| 231 | + ], |
| 232 | + { layoutRef } |
| 233 | + ) |
| 234 | + |
| 235 | + const progressComponent = createElement( |
| 236 | + View, |
| 237 | + extendObject({}, innerProps, { style: containerStyle }), |
| 238 | + // 进度条背景 |
| 239 | + createElement( |
| 240 | + View, |
| 241 | + { style: progressBgStyle }, |
| 242 | + // 进度条填充 |
| 243 | + createElement(Animated.View, { |
| 244 | + style: [progressFillStyle, animatedProgressStyle] |
| 245 | + }) |
| 246 | + ) |
| 247 | + ) |
| 248 | + |
| 249 | + if (hasPositionFixed) { |
| 250 | + return createElement(Portal, null, progressComponent) |
| 251 | + } |
| 252 | + |
| 253 | + return progressComponent |
| 254 | +}) |
| 255 | + |
| 256 | +Progress.displayName = 'MpxProgress' |
| 257 | +export default Progress |
0 commit comments