Skip to content

Commit eb46924

Browse files
committed
fix: 鸿蒙下异常问题跟进
1 parent b32c7b3 commit eb46924

File tree

1 file changed

+19
-37
lines changed

1 file changed

+19
-37
lines changed

src/packages/range/range.taro.tsx

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React, {
88
} from 'react'
99
import classNames from 'classnames'
1010
import { Text, View } from '@tarojs/components'
11+
import { pxTransform } from '@/utils/taro/px-transform'
1112
import { useTouch } from '@/hooks/use-touch'
1213
import { ComponentDefaults } from '@/utils/typings'
1314
import { usePropsValue } from '@/hooks/use-props-value'
@@ -74,13 +75,11 @@ export const Range: FunctionComponent<
7475
const [dragStatus, setDragStatus] = useState('start')
7576
const touch = useTouch()
7677
const root = useRef<HTMLDivElement>(null)
77-
const rootRect = useRef<any>(null)
7878
const [marksList, setMarksList] = useState<number[]>([])
7979
const [startValue, setStartValue] = useState<any>(0)
8080
const scope = useMemo(() => {
81-
if (max <= min) {
81+
if (max < min || max === min) {
8282
console.log('max 的值需要大于 min的值')
83-
return 0
8483
}
8584
return max - min
8685
}, [max, min])
@@ -94,12 +93,9 @@ export const Range: FunctionComponent<
9493
finalValue: 0,
9594
onChange: handleChange,
9695
})
97-
9896
const exactValueRef = useRef<RangeValue>(value || defaultValue || 0)
9997
const marksRef = useRef<{ [key: string]: any }>({})
100-
10198
useEffect(() => {
102-
console.log('set marklist')
10399
if (marks) {
104100
if (Array.isArray(marks)) {
105101
const list = marks
@@ -120,13 +116,11 @@ export const Range: FunctionComponent<
120116
}
121117
}
122118
}, [marks, max, min])
123-
124119
const classes = classNames(classPrefix, {
125120
[`${classPrefix}-disabled`]: disabled,
126121
[verticalClassPrefix]: vertical,
127122
[`${classPrefix}-native`]: isHm,
128123
})
129-
130124
const containerClasses = classNames(
131125
`${classPrefix}-container`,
132126
{
@@ -135,7 +129,6 @@ export const Range: FunctionComponent<
135129
},
136130
className
137131
)
138-
139132
const markClassName = useCallback(
140133
(mark: any) => {
141134
const classPrefix = 'nut-range-mark'
@@ -256,23 +249,23 @@ export const Range: FunctionComponent<
256249
const handleClick = useCallback(
257250
async (event: any) => {
258251
if (disabled || !root.current) return
259-
252+
// TODO 鸿蒙获取clientX的数据有误,windowX 也变成了 undefined。暂不支持,待上游支持。
253+
if (isHm) return
260254
setDragStatus('')
261255
const rect = await getRectInMultiPlatform(root.current)
262-
const x = event.detail?.x ?? event.clientX
263-
256+
let x = event.detail?.x ?? event.clientX
257+
if (isHm) x = parseFloat(pxTransform(event.windowX || x))
264258
let delta = x - rect.left
265259
let total = rect.width
266260

267261
if (vertical) {
268-
const y = event.detail?.y ?? event.clientY
262+
let y = event.detail?.y ?? event.clientY
263+
if (isHm) y = parseFloat(pxTransform(event.windowY || y))
269264
delta = y - rect.top
270265
total = rect.height
271266
}
272-
273267
const value = min + (delta / total) * scope
274268
exactValueRef.current = innerValue
275-
276269
if (isRange(innerValue)) {
277270
const [left, right] = innerValue as any
278271
const middle = (left + right) / 2
@@ -288,16 +281,6 @@ export const Range: FunctionComponent<
288281
[innerValue, disabled, isRange, min, scope, updateValue, vertical]
289282
)
290283

291-
useEffect(() => {
292-
const getRootRect = async () => {
293-
if (root.current) {
294-
const rect = await getRectInMultiPlatform(root.current)
295-
rootRect.current = rect
296-
}
297-
}
298-
getRootRect()
299-
}, [root])
300-
301284
const onTouchStart = useCallback(
302285
(event: any) => {
303286
if (disabled) return
@@ -308,34 +291,35 @@ export const Range: FunctionComponent<
308291
} else {
309292
setStartValue(format(innerValue as number))
310293
}
311-
312294
setDragStatus('start')
313295
},
314296
[innerValue, disabled, format, isRange, touch]
315297
)
316298

317299
const onTouchMove = useCallback(
318300
async (event: any) => {
301+
// @TODO RN、鸿蒙端垂直滑动时,页面会一同滑动,待解决
319302
if (disabled || !root.current) return
320-
321303
if (dragStatus === 'start') {
322304
onStart && onStart()
323305
setDragStatus('draging')
324306
}
325307
touch.move(event)
326308

327309
const handleMove = async () => {
328-
if (!rootRect.current) return
329-
console.log('touch move000033', rootRect.current.width)
330-
331-
let delta = touch.deltaX.current
332-
let total = rootRect.current.width
310+
const rect = await getRectInMultiPlatform(root.current)
311+
if (!rect) return
312+
let delta = isHm
313+
? parseFloat(pxTransform(touch.deltaX.current))
314+
: touch.deltaX.current
315+
let total = rect.width
333316
let diff = (delta / total) * scope
334317
diff = rtl ? -diff : diff
335-
336318
if (vertical) {
337-
delta = touch.deltaY.current
338-
total = rootRect.current.height
319+
delta = isHm
320+
? parseFloat(pxTransform(touch.deltaY.current))
321+
: touch.deltaY.current
322+
total = rect.height
339323
diff = (delta / total) * scope
340324
}
341325

@@ -347,7 +331,6 @@ export const Range: FunctionComponent<
347331
exactValueRef.current = newValue
348332
updateValue(newValue)
349333
}
350-
351334
requestAnimationFrame(handleMove)
352335
},
353336
[
@@ -380,7 +363,6 @@ export const Range: FunctionComponent<
380363
},
381364
[innerValue]
382365
)
383-
384366
const buttonNumberTransform = useMemo(() => {
385367
return vertical ? 'translate(100%, -50%)' : 'translate(-50%, -100%)'
386368
}, [vertical])

0 commit comments

Comments
 (0)