Skip to content

Commit

Permalink
fix(elevator): harmony适配 (#2836)
Browse files Browse the repository at this point in the history
* fix: 解决微信小程序滚动报错问题

* fix(elevator): harmony适配

* fix: harmony height
  • Loading branch information
irisSong authored Dec 12, 2024
1 parent 3bfa151 commit fe50c4b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
11 changes: 7 additions & 4 deletions src/packages/elevator/elevator.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,23 @@
}

&-fixed {
display: flex;
align-items: center;
width: 100%;
position: absolute;
top: 0;
left: 0;
z-index: 1;
padding: $elevator-list-item-padding;
height: $elevator-list-item-code-height;
line-height: $elevator-list-item-code-line-height;
font-size: $elevator-list-item-code-font-size;
color: $elevator-list-fixed-color;
font-weight: $elevator-list-item-code-font-weight;
background-color: $elevator-list-fixed-bg-color;
box-sizing: border-box;
box-shadow: $elevator-list-fixed-box-shadow;
&-title {
font-size: $elevator-list-item-code-font-size;
color: $elevator-list-fixed-color;
font-weight: $elevator-list-item-code-font-weight;
}
}
}

Expand Down
39 changes: 20 additions & 19 deletions src/packages/elevator/elevator.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React, {
createContext,
FunctionComponent,
useEffect,
useRef,
useState,
createContext,
useMemo,
} from 'react'
import Taro, { createSelectorQuery, nextTick } from '@tarojs/taro'

import { ScrollView, View } from '@tarojs/components'
import Taro, { nextTick, createSelectorQuery } from '@tarojs/taro'
import { ScrollView, View, Text } from '@tarojs/components'
import classNames from 'classnames'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
import { harmony } from '@/utils/platform-taro'
import useUuid from '@/utils/use-uuid'

export const elevatorContext = createContext({} as ElevatorData)
Expand Down Expand Up @@ -148,8 +149,7 @@ export const Elevator: FunctionComponent<
touchState.current.y2 = firstTouch.pageY
const delta =
(touchState.current.y2 - touchState.current.y1) / spaceHeight || 0
const cacheIndex = state.current.anchorIndex + Math.floor(delta)

const cacheIndex = state.current.anchorIndex + Math.round(delta)
setCodeIndex(cacheIndex)
scrollTo(cacheIndex)
}
Expand Down Expand Up @@ -213,6 +213,12 @@ export const Elevator: FunctionComponent<
}
}

const getWrapStyle = useMemo(() => {
const calcHeight = Number.isNaN(+height) ? height : `${height}px`

return { height: harmony() ? Number(height) : calcHeight }
}, [height])

useEffect(() => {
if (listview.current) {
nextTick(() => {
Expand All @@ -227,10 +233,7 @@ export const Elevator: FunctionComponent<
style={style}
{...rest}
>
<View
className={`${classPrefix}-list`}
style={{ height: Number.isNaN(+height) ? height : `${height}px` }}
>
<View className={`${classPrefix}-list`} style={getWrapStyle}>
<ScrollView
scrollTop={scrollTop}
scrollY
Expand Down Expand Up @@ -297,13 +300,7 @@ export const Elevator: FunctionComponent<
</View>
) : null}
<View className={`${classPrefix}-bars`}>
<View
className={`${classPrefix}-bars-inner`}
onTouchStart={(event) => touchStart(event as any)}
onTouchMove={(event) => touchMove(event as any)}
onTouchEnd={touchEnd}
style={{ touchAction: 'pan-y' }}
>
<View className={`${classPrefix}-bars-inner`}>
{list.map((item: any, index: number) => {
return (
<View
Expand All @@ -315,6 +312,10 @@ export const Elevator: FunctionComponent<
data-index={index}
key={index}
onClick={() => handleClickIndex(item[floorKey])}
onTouchStart={(event) => touchStart(event as any)}
onTouchMove={(event) => touchMove(event as any)}
onTouchEnd={touchEnd}
style={{ touchAction: 'pan-y' }}
>
{item[floorKey]}
</View>
Expand All @@ -326,9 +327,9 @@ export const Elevator: FunctionComponent<
) : null}
{sticky && scrollY > 0 ? (
<View className={`${classPrefix}-list-fixed`}>
<span className={`${classPrefix}-list-fixed-title`}>
<Text className={`${classPrefix}-list-fixed-title`}>
{list[codeIndex][floorKey]}
</span>
</Text>
</View>
) : null}
</div>
Expand Down

0 comments on commit fe50c4b

Please sign in to comment.