Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swiper重构 #1711

Open
wants to merge 55 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
13bbdab
swiper refactor init
Nov 6, 2024
e3f09a4
修复循环自动轮播初始化
Nov 6, 2024
0ce5bb5
add interpolate animateion
Nov 7, 2024
763d32a
animation fix & ts
Nov 8, 2024
ab9c8bf
修复worklet & JS 通信问题 & 边界场景
Nov 10, 2024
e1cb370
修复边界问题
Nov 11, 2024
f3c9706
修复runOnJs 问题
Nov 11, 2024
3b40f5d
merge master
Nov 11, 2024
d5f2083
修复 margin问题
Nov 12, 2024
8102f16
修复快速滚动+轮播问题 & 排查y滚
Nov 12, 2024
94c43d1
swiper 重构拖拽
Nov 15, 2024
3fc91c1
swiper loop
Nov 18, 2024
be7b1a9
fix bug
Nov 18, 2024
ec9cdd1
swiper 修复连续滑动&滑动中触摸
Nov 21, 2024
ae983cd
修复eslint
Nov 21, 2024
e79c797
merge master
Nov 21, 2024
8063477
修改组件命名
Nov 21, 2024
63ca5e3
调整下位置
Nov 21, 2024
5d640ee
调整下变量命名
Nov 21, 2024
10566df
修复lint
Nov 21, 2024
a3d0cea
comment update
Nov 25, 2024
3f10275
merge master
Nov 25, 2024
a7e5100
delete worklet
Nov 25, 2024
4f62f1f
add children change
Nov 25, 2024
e62d16c
Merge branch 'master' into feat-swiper-refactor
Nov 25, 2024
752ac6e
修复canMove & 增加dot动画
Nov 26, 2024
017fb99
优化CR问题
Nov 26, 2024
8f90650
Merge branch 'master' into feat-swiper-refactor
hiyuki Nov 26, 2024
4e23d54
优化边界场景
Nov 29, 2024
d3371f2
Merge branch 'feat-swiper-refactor' of github.com:didi/mpx into feat-…
Nov 29, 2024
2dd2350
fix eslint
Dec 3, 2024
006729c
fix change
Dec 10, 2024
18849c5
支持元素scale动画&修复闪动
Dec 19, 2024
f2d5e6a
merge master &解决冲突
Dec 19, 2024
a2ecebd
删除carouse
Dec 19, 2024
0ec3d28
comment解决
Dec 20, 2024
e90d066
优化comment
Dec 23, 2024
6e1db24
swiper gesture事件更新
Dec 24, 2024
ab7db79
优化useEffect
Dec 25, 2024
68d6561
修复默认不更新问题
Dec 25, 2024
475e786
修复canMove
Dec 25, 2024
65c704e
修复选中0
Dec 25, 2024
855fce2
feat: 修复反向滚动问题
Dec 27, 2024
199c746
优化comment
Jan 5, 2025
ed9ee2b
feat: 修改lint
Jan 6, 2025
3e2a5a5
Merge branch 'master' into feat-swiper-refactor
Jan 6, 2025
3f96a4a
Merge branch 'master' into feat-swiper-refactor
Jan 6, 2025
619ec81
优化comment
Jan 7, 2025
3a12508
优化comment删除wrapperLayout中的loop
Jan 7, 2025
74954ac
优化
Jan 7, 2025
1140d20
优化
Jan 7, 2025
fd78078
onWrapperLayout 增加pauseLoop
Jan 7, 2025
1875716
修复children变化时当前滚动到的索引位置大的问题
Jan 8, 2025
1317412
优化swiper
Jan 10, 2025
3a11d4b
修复lint
Jan 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ export const IntersectionObserverContext = createContext<IntersectionObserver |

export const RouteContext = createContext<number | null>(null)

export const SwiperContext = createContext({})

export const KeyboardAvoidContext = createContext<KeyboardAvoidContextValue | null>(null)
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { View, LayoutChangeEvent } from 'react-native'
import { ReactNode, forwardRef, useRef } from 'react'
import { View } from 'react-native'
import Animated, { useAnimatedStyle, interpolate, SharedValue } from 'react-native-reanimated'
import { ReactNode, forwardRef, useRef, useContext } from 'react'
import useInnerProps from './getInnerListeners'
import useNodesRef, { HandlerRef } from './useNodesRef' // 引入辅助函数
import { useTransformStyle, splitStyle, splitProps, wrapChildren, useLayout } from './utils'
import { SwiperContext } from './context'

interface SwiperItemProps {
'item-id'?: string;
Expand All @@ -14,15 +16,31 @@ interface SwiperItemProps {
'parent-height'?: number;
children?: ReactNode;
style?: Object;
customStyle: [];
itemIndex: number;
}

interface ContextType {
offset: SharedValue<number>;
step: SharedValue<number>;
scale: boolean;
dir: string;
}

const _SwiperItem = forwardRef<HandlerRef<View, SwiperItemProps>, SwiperItemProps>((props: SwiperItemProps, ref) => {
const {
'enable-var': enableVar,
'external-var-context': externalVarContext,
style
style,
customStyle,
itemIndex
} = props

const contextValue = useContext(SwiperContext) as ContextType
const offset = contextValue.offset || 0
const step = contextValue.step || 0
const scale = contextValue.scale || false
const dir = contextValue.dir || 'x'
const { textProps } = splitProps(props)
const nodeRef = useRef(null)

Expand All @@ -47,19 +65,35 @@ const _SwiperItem = forwardRef<HandlerRef<View, SwiperItemProps>, SwiperItemProp
} = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: nodeRef })

const innerProps = useInnerProps(props, {
style: { ...innerStyle, ...layoutStyle },
ref: nodeRef,
...layoutProps
}, [
'children',
'enable-offset'
'enable-offset',
'style'
], { layoutRef })

const itemAnimatedStyle = useAnimatedStyle(() => {
if (!step.value) return {}
const inputRange = [step.value, 0]
const outputRange = [0.7, 1]
// 实现元素的宽度跟随step从0到真实宽度,且不能触发重新渲染整个组件,通过AnimatedStyle的方式实现
const outerLayoutStyle = dir === 'x' ? { width: step.value, height: '100%' } : { width: '100%', height: step.value }
const transformStyle = []
if (scale) {
transformStyle.push({
scale: interpolate(Math.abs(Math.abs(offset.value) - itemIndex * step.value), inputRange, outputRange)
})
}
return Object.assign(outerLayoutStyle, {
transform: transformStyle
})
})
return (
<View
data-itemId={props['item-id']}
{...innerProps}>
{
<Animated.View
{...innerProps}
style={[innerStyle, layoutStyle, itemAnimatedStyle, customStyle]}
data-itemId={props['item-id']}>
{
wrapChildren(
props,
{
Expand All @@ -70,7 +104,7 @@ const _SwiperItem = forwardRef<HandlerRef<View, SwiperItemProps>, SwiperItemProp
}
)
}
</View>
</Animated.View>
)
})

Expand Down
Loading
Loading