@onekeyfe/react-native-pager-view 1.1.36 → 1.1.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -296
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,306 +1,31 @@
|
|
|
1
|
+
# @onekeyfe/react-native-pager-view
|
|
1
2
|
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
<img alt="React Native PagerView" src="https://github.com/user-attachments/assets/1f0f7b9f-5723-4a9c-b0f1-99ed472f3122">
|
|
5
|
-
</picture>
|
|
6
|
-
</a>
|
|
3
|
+
First, a sincere thank-you to Callstack and the
|
|
4
|
+
`react-native-pager-view` maintainers for their excellent work 🙏
|
|
7
5
|
|
|
8
|
-
|
|
6
|
+
This package is built on, and inspired by,
|
|
7
|
+
[callstack/react-native-pager-view](https://github.com/callstack/react-native-pager-view).
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
[](https://github.com/callstack/react-native-pager-view/blob/master/LICENSE)
|
|
9
|
+
Our original plan was to keep our customizations as patches on top of upstream
|
|
10
|
+
`react-native-pager-view`.
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
As our product requirements evolved, the scope of those changes outgrew what we
|
|
13
|
+
could reasonably maintain as an upstream patch set. We regret that we were
|
|
14
|
+
unable to keep this work in patch form.
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
As the gap grew, we ultimately forked `react-native-pager-view` to keep
|
|
17
|
+
development and delivery stable.
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
<p align="center">
|
|
22
|
-
<img src="img/vp-carousel.gif" alt="ViewPager" width="300">
|
|
23
|
-
</p>
|
|
19
|
+
## Upstream Project
|
|
24
20
|
|
|
25
|
-
|
|
21
|
+
- Repository: [callstack/react-native-pager-view](https://github.com/callstack/react-native-pager-view)
|
|
22
|
+
- License: MIT
|
|
26
23
|
|
|
27
|
-
##
|
|
24
|
+
## Notes
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
- This fork includes OneKey-specific adaptations for our product requirements.
|
|
27
|
+
- If you are looking for original behavior and full documentation, please refer
|
|
28
|
+
to the upstream repository.
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
`"@react-native-community/viewpager"` library has been changed to `react-native-pager-view`. Here you can find more information, how to migrate pager view to the latest [version](https://github.com/callstack/react-native-pager-view/blob/master/MIGRATION.md)
|
|
35
|
-
|
|
36
|
-
## Getting started
|
|
37
|
-
Bun:
|
|
38
|
-
|
|
39
|
-
`bun add react-native-pager-view`
|
|
40
|
-
|
|
41
|
-
Yarn:
|
|
42
|
-
|
|
43
|
-
`yarn add react-native-pager-view`
|
|
44
|
-
|
|
45
|
-
## Linking
|
|
46
|
-
|
|
47
|
-
### >= 0.60
|
|
48
|
-
|
|
49
|
-
Autolinking will just do the job.
|
|
50
|
-
|
|
51
|
-
### < 0.60
|
|
52
|
-
|
|
53
|
-
#### Mostly automatic
|
|
54
|
-
|
|
55
|
-
`react-native link react-native-pager-view`
|
|
56
|
-
|
|
57
|
-
#### Manual linking
|
|
58
|
-
|
|
59
|
-
<details>
|
|
60
|
-
<summary>Manually link the library on iOS</summary>
|
|
61
|
-
</br>
|
|
62
|
-
|
|
63
|
-
Follow the [instructions in the React Native documentation](https://facebook.github.io/react-native/img/linking-libraries-ios#manual-linking) to manually link the framework or link using [Cocoapods](https://cocoapods.org) by adding this to your `Podfile`:
|
|
64
|
-
|
|
65
|
-
```ruby
|
|
66
|
-
pod 'react-native-pager-view', :path => '../node_modules/react-native-pager-view'
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
</details>
|
|
70
|
-
|
|
71
|
-
<details>
|
|
72
|
-
<summary>Manually link the library on Android</summary>
|
|
73
|
-
</br>
|
|
74
|
-
Make the following changes:
|
|
75
|
-
|
|
76
|
-
#### `android/settings.gradle`
|
|
77
|
-
|
|
78
|
-
```groovy
|
|
79
|
-
include ':react-native-pager-view'
|
|
80
|
-
project(':react-native-pager-view').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-pager-view/android')
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
#### `android/app/build.gradle`
|
|
84
|
-
|
|
85
|
-
```groovy
|
|
86
|
-
dependencies {
|
|
87
|
-
...
|
|
88
|
-
implementation project(':react-native-pager-view')
|
|
89
|
-
}
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
#### `android/app/src/main/.../MainApplication.java`
|
|
93
|
-
|
|
94
|
-
On top, where imports are:
|
|
95
|
-
|
|
96
|
-
Add `import com.reactnativepagerview.PagerViewPackage;`
|
|
97
|
-
|
|
98
|
-
Add the `PagerViewPackage` class to your list of exported packages.
|
|
99
|
-
|
|
100
|
-
```java
|
|
101
|
-
@Override
|
|
102
|
-
protected List<ReactPackage> getPackages() {
|
|
103
|
-
return Arrays.<ReactPackage>asList(
|
|
104
|
-
new MainReactPackage(),
|
|
105
|
-
new PagerViewPackage()
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
</details>
|
|
111
|
-
|
|
112
|
-
## Usage
|
|
113
|
-
|
|
114
|
-
```js
|
|
115
|
-
import React from 'react';
|
|
116
|
-
import { StyleSheet, View, Text } from 'react-native';
|
|
117
|
-
import PagerView from 'react-native-pager-view';
|
|
118
|
-
|
|
119
|
-
const MyPager = () => {
|
|
120
|
-
return (
|
|
121
|
-
<PagerView style={styles.pagerView} initialPage={0}>
|
|
122
|
-
<View key="1">
|
|
123
|
-
<Text>First page</Text>
|
|
124
|
-
</View>
|
|
125
|
-
<View key="2">
|
|
126
|
-
<Text>Second page</Text>
|
|
127
|
-
</View>
|
|
128
|
-
</PagerView>
|
|
129
|
-
);
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
const styles = StyleSheet.create({
|
|
133
|
-
pagerView: {
|
|
134
|
-
flex: 1,
|
|
135
|
-
},
|
|
136
|
-
});
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
**Attention:** Note that you can only use `View` components as children of `PagerView` (cf. folder _/example_)
|
|
140
|
-
. For Android if `View` has own children, set prop `collapsable` to false <https://reactnative.dev/docs/view#collapsable-android>, otherwise react-native might remove those children views and and its children will be rendered as separate pages
|
|
141
|
-
|
|
142
|
-
## Advanced usage
|
|
143
|
-
|
|
144
|
-
For advanced usage please take a look into our [example project](https://github.com/callstack/react-native-pager-view/blob/master/example/src/BasicPagerViewExample.tsx)
|
|
145
|
-
|
|
146
|
-
## API
|
|
147
|
-
|
|
148
|
-
| Prop | Description | Platform |
|
|
149
|
-
| -------------------------------------------------------------------- | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------: |
|
|
150
|
-
| `initialPage` | Index of initial page that should be selected | both |
|
|
151
|
-
| `scrollEnabled: boolean` | Should pager view scroll, when scroll enabled | both |
|
|
152
|
-
| `onPageScroll: (e: PageScrollEvent) => void` | Executed when transitioning between pages (either because the animation for the requested page has changed or when the user is swiping/dragging between pages) | both |
|
|
153
|
-
| `onPageScrollStateChanged: (e: PageScrollStateChangedEvent) => void` | Function called when the page scrolling state has changed | both |
|
|
154
|
-
| `onPageSelected: (e: PageSelectedEvent) => void` | This callback will be called once the ViewPager finishes navigating to the selected page | both |
|
|
155
|
-
| `pageMargin: number` | Blank space to be shown between pages | both |
|
|
156
|
-
| `keyboardDismissMode: ('none' / 'on-drag')` | Determines whether the keyboard gets dismissed in response to a drag | both |
|
|
157
|
-
| `orientation: Orientation` | Set `horizontal` or `vertical` scrolling orientation (it does **not** work dynamically) | both |
|
|
158
|
-
| `overScrollMode: OverScrollMode` | Used to override default value of overScroll mode. Can be `auto`, `always` or `never`. Defaults to `auto` | Android |
|
|
159
|
-
| `offscreenPageLimit: number` | Set the number of pages that should be retained to either side of the currently visible page(s). Pages beyond this limit will be recreated from the adapter when needed. Defaults to RecyclerView's caching strategy. The given value must either be larger than 0. | Android |
|
|
160
|
-
| `overdrag: boolean` | Allows for overscrolling after reaching the end or very beginning or pages. Defaults to `false` | iOS |
|
|
161
|
-
| `layoutDirection: ('ltr' / 'rtl' / 'locale')` | Specifies layout direction. Use `ltr` or `rtl` to set explicitly or `locale` to deduce from the default language script of a locale. Defaults to `locale` | both |
|
|
162
|
-
|
|
163
|
-
| Method | Description | Platform |
|
|
164
|
-
| ------------------------------------------ | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------: |
|
|
165
|
-
| `setPage(index: number)` | Function to scroll to a specific page in the PagerView. Invalid index is ignored. | both |
|
|
166
|
-
| `setPageWithoutAnimation(index: number)` | Function to scroll to a specific page in the PagerView. Invalid index is ignored. | both |
|
|
167
|
-
| `setScrollEnabled(scrollEnabled: boolean)` | A helper function to enable/disable scroll imperatively. The recommended way is using the scrollEnabled prop, however, there might be a case where a imperative solution is more useful (e.g. for not blocking an animation) | both |
|
|
168
|
-
|
|
169
|
-
## Contributing
|
|
170
|
-
|
|
171
|
-
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
|
|
172
|
-
|
|
173
|
-
## Known Issues
|
|
174
|
-
|
|
175
|
-
- `flex:1` does not work for child views, please use `width: '100%', height: '100%'` [instead](https://github.com/callstack/react-native-pager-view/issues/186#issuecomment-675320732)
|
|
176
|
-
|
|
177
|
-
- [iOS]: In case of `UIViewControllerHierarchyInconsistency` error, please use below fix:
|
|
178
|
-
|
|
179
|
-
```
|
|
180
|
-
requestAnimationFrame(() => refPagerView.current?.setPage(index));
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
## Preview
|
|
184
|
-
|
|
185
|
-
### Android
|
|
186
|
-
|
|
187
|
-
| horizontal | vertical |
|
|
188
|
-
| :---------------------------------------------------------------: | :------------------------------------------------------------------------: |
|
|
189
|
-
| <img src="img/android-viewpager.gif" alt="ViewPager" width="325"> | <img src="img/android-viewpager-vertical.gif" alt="ViewPager" width="325"> |
|
|
190
|
-
|
|
191
|
-
### iOS
|
|
192
|
-
|
|
193
|
-
| horizontal | vertical |
|
|
194
|
-
| :------------------------------------------------------------------: | :--------------------------------------------------------------------: |
|
|
195
|
-
| <img src="img/ios-viewpager-scroll.gif" alt="ViewPager" width="325"> | <img src="img/ios-viewpager-vertical.gif" alt="ViewPager" width="325"> |
|
|
196
|
-
|
|
197
|
-
## Reanimated onPageScroll handler
|
|
198
|
-
|
|
199
|
-
An example can be found [here](https://github.com/callstack/react-native-pager-view/blob/master/example/src/ReanimatedOnPageScrollExample.tsx)
|
|
200
|
-
|
|
201
|
-
#### Instructions
|
|
202
|
-
|
|
203
|
-
To attach reanimated handler with `onPageScroll` follow the below steps.
|
|
204
|
-
|
|
205
|
-
```jsx
|
|
206
|
-
// 1. Define the handler
|
|
207
|
-
function usePageScrollHandler(handlers, dependencies) {
|
|
208
|
-
const { context, doDependenciesDiffer } = useHandler(handlers, dependencies);
|
|
209
|
-
const subscribeForEvents = ['onPageScroll'];
|
|
210
|
-
|
|
211
|
-
return useEvent(
|
|
212
|
-
(event) => {
|
|
213
|
-
'worklet';
|
|
214
|
-
const { onPageScroll } = handlers;
|
|
215
|
-
if (onPageScroll && event.eventName.endsWith('onPageScroll')) {
|
|
216
|
-
onPageScroll(event, context);
|
|
217
|
-
}
|
|
218
|
-
},
|
|
219
|
-
subscribeForEvents,
|
|
220
|
-
doDependenciesDiffer
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// 2. Attach the event handler
|
|
225
|
-
import PagerView from 'react-native-pager-view';
|
|
226
|
-
import Animated from 'react-native-reanimated';
|
|
227
|
-
const AnimatedPagerView = Animated.createAnimatedComponent(PagerView);
|
|
228
|
-
|
|
229
|
-
const pageScrollHandler = usePageScrollHandler({
|
|
230
|
-
onPageScroll: (e) => {
|
|
231
|
-
'worklet';
|
|
232
|
-
offset.value = e.offset;
|
|
233
|
-
console.log(e.offset, e.position);
|
|
234
|
-
},
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
<AnimatedPagerView onPageScroll={pageScrollHandler} />;
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
## usePagerView Hook Usage
|
|
241
|
-
The `usePagerView` hook is a convenient way to manage the state and control the behavior of the `<PagerView />` component. It provides functions and variables to interact with the pager, such as navigating between pages and enabling/disabling scrolling.
|
|
242
|
-
|
|
243
|
-
Below is an example of how to use the usePager hook:
|
|
244
|
-
|
|
245
|
-
```jsx
|
|
246
|
-
export function PagerHookExample() {
|
|
247
|
-
const { AnimatedPagerView, ref, ...rest } = usePagerView({ pagesAmount: 10 });
|
|
248
|
-
|
|
249
|
-
return (
|
|
250
|
-
<SafeAreaView style={styles.container}>
|
|
251
|
-
<AnimatedPagerView
|
|
252
|
-
testID="pager-view"
|
|
253
|
-
ref={ref}
|
|
254
|
-
style={styles.PagerView}
|
|
255
|
-
initialPage={0}
|
|
256
|
-
layoutDirection="ltr"
|
|
257
|
-
overdrag={rest.overdragEnabled}
|
|
258
|
-
scrollEnabled={rest.scrollEnabled}
|
|
259
|
-
onPageScroll={rest.onPageScroll}
|
|
260
|
-
onPageSelected={rest.onPageSelected}
|
|
261
|
-
onPageScrollStateChanged={rest.onPageScrollStateChanged}
|
|
262
|
-
pageMargin={10}
|
|
263
|
-
orientation="horizontal"
|
|
264
|
-
>
|
|
265
|
-
{useMemo(
|
|
266
|
-
() =>
|
|
267
|
-
rest.pages.map((_, index) => (
|
|
268
|
-
<View
|
|
269
|
-
testID="pager-view-content"
|
|
270
|
-
key={index}
|
|
271
|
-
style={{
|
|
272
|
-
flex: 1,
|
|
273
|
-
backgroundColor: '#fdc08e',
|
|
274
|
-
alignItems: 'center',
|
|
275
|
-
padding: 20,
|
|
276
|
-
}}
|
|
277
|
-
collapsable={false}
|
|
278
|
-
>
|
|
279
|
-
<LikeCount />
|
|
280
|
-
<Text testID={`pageNumber${index}`}>
|
|
281
|
-
{`page number ${index}`}
|
|
282
|
-
</Text>
|
|
283
|
-
</View>
|
|
284
|
-
)),
|
|
285
|
-
[rest.pages]
|
|
286
|
-
)}
|
|
287
|
-
</AnimatedPagerView>
|
|
288
|
-
<NavigationPanel {...rest} />
|
|
289
|
-
</SafeAreaView>
|
|
290
|
-
);
|
|
291
|
-
}
|
|
292
|
-
```
|
|
293
|
-
### How the Example Works:
|
|
294
|
-
|
|
295
|
-
- **Pager View Setup**: The `AnimatedPagerView` component wraps `PagerView` in React Native's animation capabilities. It accepts multiple props from the `usePager` hook, such as `overdragEnabled`, `scrollEnabled`, `onPageScroll`, `onPageSelected`, and others to manage pager behavior.
|
|
296
|
-
|
|
297
|
-
- **Rendering Pages**: The pages are dynamically generated using the `rest.pages` array (initialized by `usePager`). The `useMemo` hook ensures the pages are only recomputed when necessary for performance reasons.
|
|
298
|
-
|
|
299
|
-
### Conclusion
|
|
300
|
-
|
|
301
|
-
The `usePager` hook makes it easy to handle pagination with dynamic views. This example demonstrates how to set up a simple paginated interface where users can scroll through pages, interact with page elements, and control the pager with external navigation.
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
## License
|
|
305
|
-
|
|
306
|
-
MIT
|
|
30
|
+
Thank you again to Callstack and everyone who contributes to
|
|
31
|
+
`react-native-pager-view` 💙
|