forked from react-navigation/react-navigation
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPagerViewAdapter.harmony.tsx
185 lines (162 loc) · 4.84 KB
/
PagerViewAdapter.harmony.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/
import * as React from 'react';
import { Animated, Keyboard, StyleSheet } from 'react-native';
import ViewPager, {
PageScrollStateChangedNativeEvent,
} from '@react-native-oh-tpl/react-native-pager-view';
import type {
EventEmitterProps,
Listener,
NavigationState,
PagerProps,
Route,
} from './types';
import { useAnimatedValue } from 'react-native-tab-view/src/useAnimatedValue';
const AnimatedViewPager = Animated.createAnimatedComponent(ViewPager);
type Props<T extends Route> = PagerProps & {
onIndexChange: (index: number) => void;
navigationState: NavigationState<T>;
children: (
props: EventEmitterProps & {
// Animated value which represents the state of current index
// It can include fractional digits as it represents the intermediate value
position: Animated.AnimatedInterpolation<number>;
// Function to actually render the content of the pager
// The parent component takes care of rendering
render: (children: React.ReactNode) => React.ReactNode;
// Callback to call when switching the tab
// The tab switch animation is performed even if the index in state is unchanged
jumpTo: (key: string) => void;
}
) => React.ReactElement;
};
export function PagerViewAdapter<T extends Route>({
keyboardDismissMode = 'auto',
swipeEnabled = true,
navigationState,
onIndexChange,
onSwipeStart,
onSwipeEnd,
children,
style,
animationEnabled,
...rest
}: Props<T>) {
const { index } = navigationState;
const listenersRef = React.useRef<Listener[]>([]);
const pagerRef = React.useRef<ViewPager>();
const indexRef = React.useRef<number>(index);
const navigationStateRef = React.useRef(navigationState);
const position = useAnimatedValue(index);
const offset = useAnimatedValue(0);
React.useEffect(() => {
navigationStateRef.current = navigationState;
});
const jumpTo = React.useCallback(
(key: string) => {
const index = navigationStateRef.current.routes.findIndex(
(route: { key: string }) => route.key === key
);
if (animationEnabled) {
pagerRef.current?.setPage(index);
} else {
pagerRef.current?.setPageWithoutAnimation(index);
position.setValue(index);
}
},
[animationEnabled, position]
);
React.useEffect(() => {
if (keyboardDismissMode === 'auto') {
Keyboard.dismiss();
}
if (indexRef.current !== index) {
if (animationEnabled) {
pagerRef.current?.setPage(index);
} else {
pagerRef.current?.setPageWithoutAnimation(index);
position.setValue(index);
}
}
}, [keyboardDismissMode, index, animationEnabled, position]);
const onPageScrollStateChanged = (
state: PageScrollStateChangedNativeEvent
) => {
const { pageScrollState } = state.nativeEvent;
switch (pageScrollState) {
case 'idle':
onSwipeEnd?.();
return;
case 'dragging': {
const subscription = offset.addListener(({ value }) => {
const next =
index + (value > 0 ? Math.ceil(value) : Math.floor(value));
if (next !== index) {
listenersRef.current.forEach((listener) => listener(next));
}
offset.removeListener(subscription);
});
onSwipeStart?.();
return;
}
}
};
const addEnterListener = React.useCallback((listener: Listener) => {
listenersRef.current.push(listener);
return () => {
const index = listenersRef.current.indexOf(listener);
if (index > -1) {
listenersRef.current.splice(index, 1);
}
};
}, []);
const memoizedPosition = React.useMemo(
() => Animated.add(position, offset),
[offset, position]
);
return children({
position: memoizedPosition,
addEnterListener,
jumpTo,
render: (children) => (
<AnimatedViewPager
{...rest}
ref={pagerRef}
style={[styles.container, style]}
initialPage={index}
keyboardDismissMode={
keyboardDismissMode === 'auto' ? 'on-drag' : keyboardDismissMode
}
onPageScroll={Animated.event(
[
{
nativeEvent: {
position: position,
offset: offset,
},
},
],
{ useNativeDriver: true }
)}
onPageSelected={(e) => {
const index = e.nativeEvent.position;
indexRef.current = index;
onIndexChange(index);
}}
onPageScrollStateChanged={onPageScrollStateChanged}
scrollEnabled={swipeEnabled}
>
{children}
</AnimatedViewPager>
),
});
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});