Skip to content

Commit fe50c4b

Browse files
authored
fix(elevator): harmony适配 (#2836)
* fix: 解决微信小程序滚动报错问题 * fix(elevator): harmony适配 * fix: harmony height
1 parent 3bfa151 commit fe50c4b

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/packages/elevator/elevator.scss

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,23 @@
5151
}
5252

5353
&-fixed {
54+
display: flex;
55+
align-items: center;
5456
width: 100%;
5557
position: absolute;
5658
top: 0;
5759
left: 0;
5860
z-index: 1;
5961
padding: $elevator-list-item-padding;
6062
height: $elevator-list-item-code-height;
61-
line-height: $elevator-list-item-code-line-height;
62-
font-size: $elevator-list-item-code-font-size;
63-
color: $elevator-list-fixed-color;
64-
font-weight: $elevator-list-item-code-font-weight;
6563
background-color: $elevator-list-fixed-bg-color;
6664
box-sizing: border-box;
6765
box-shadow: $elevator-list-fixed-box-shadow;
66+
&-title {
67+
font-size: $elevator-list-item-code-font-size;
68+
color: $elevator-list-fixed-color;
69+
font-weight: $elevator-list-item-code-font-weight;
70+
}
6871
}
6972
}
7073

src/packages/elevator/elevator.taro.tsx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React, {
2-
createContext,
32
FunctionComponent,
43
useEffect,
54
useRef,
65
useState,
6+
createContext,
7+
useMemo,
78
} from 'react'
8-
import Taro, { createSelectorQuery, nextTick } from '@tarojs/taro'
9-
10-
import { ScrollView, View } from '@tarojs/components'
9+
import Taro, { nextTick, createSelectorQuery } from '@tarojs/taro'
10+
import { ScrollView, View, Text } from '@tarojs/components'
1111
import classNames from 'classnames'
1212
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
13+
import { harmony } from '@/utils/platform-taro'
1314
import useUuid from '@/utils/use-uuid'
1415

1516
export const elevatorContext = createContext({} as ElevatorData)
@@ -148,8 +149,7 @@ export const Elevator: FunctionComponent<
148149
touchState.current.y2 = firstTouch.pageY
149150
const delta =
150151
(touchState.current.y2 - touchState.current.y1) / spaceHeight || 0
151-
const cacheIndex = state.current.anchorIndex + Math.floor(delta)
152-
152+
const cacheIndex = state.current.anchorIndex + Math.round(delta)
153153
setCodeIndex(cacheIndex)
154154
scrollTo(cacheIndex)
155155
}
@@ -213,6 +213,12 @@ export const Elevator: FunctionComponent<
213213
}
214214
}
215215

216+
const getWrapStyle = useMemo(() => {
217+
const calcHeight = Number.isNaN(+height) ? height : `${height}px`
218+
219+
return { height: harmony() ? Number(height) : calcHeight }
220+
}, [height])
221+
216222
useEffect(() => {
217223
if (listview.current) {
218224
nextTick(() => {
@@ -227,10 +233,7 @@ export const Elevator: FunctionComponent<
227233
style={style}
228234
{...rest}
229235
>
230-
<View
231-
className={`${classPrefix}-list`}
232-
style={{ height: Number.isNaN(+height) ? height : `${height}px` }}
233-
>
236+
<View className={`${classPrefix}-list`} style={getWrapStyle}>
234237
<ScrollView
235238
scrollTop={scrollTop}
236239
scrollY
@@ -297,13 +300,7 @@ export const Elevator: FunctionComponent<
297300
</View>
298301
) : null}
299302
<View className={`${classPrefix}-bars`}>
300-
<View
301-
className={`${classPrefix}-bars-inner`}
302-
onTouchStart={(event) => touchStart(event as any)}
303-
onTouchMove={(event) => touchMove(event as any)}
304-
onTouchEnd={touchEnd}
305-
style={{ touchAction: 'pan-y' }}
306-
>
303+
<View className={`${classPrefix}-bars-inner`}>
307304
{list.map((item: any, index: number) => {
308305
return (
309306
<View
@@ -315,6 +312,10 @@ export const Elevator: FunctionComponent<
315312
data-index={index}
316313
key={index}
317314
onClick={() => handleClickIndex(item[floorKey])}
315+
onTouchStart={(event) => touchStart(event as any)}
316+
onTouchMove={(event) => touchMove(event as any)}
317+
onTouchEnd={touchEnd}
318+
style={{ touchAction: 'pan-y' }}
318319
>
319320
{item[floorKey]}
320321
</View>
@@ -326,9 +327,9 @@ export const Elevator: FunctionComponent<
326327
) : null}
327328
{sticky && scrollY > 0 ? (
328329
<View className={`${classPrefix}-list-fixed`}>
329-
<span className={`${classPrefix}-list-fixed-title`}>
330+
<Text className={`${classPrefix}-list-fixed-title`}>
330331
{list[codeIndex][floorKey]}
331-
</span>
332+
</Text>
332333
</View>
333334
) : null}
334335
</div>

0 commit comments

Comments
 (0)