@react-native-ohos/react-native-pager-view 6.7.2-rc.1

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.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.OpenSource +11 -0
  3. package/README.md +15 -0
  4. package/harmony/pager_view/LICENSE +21 -0
  5. package/harmony/pager_view/NOTICE +33 -0
  6. package/harmony/pager_view/OAT.xml +38 -0
  7. package/harmony/pager_view/README.OpenSource +11 -0
  8. package/harmony/pager_view/README.md +236 -0
  9. package/harmony/pager_view/build-profile.json5 +9 -0
  10. package/harmony/pager_view/consumer-rules.txt +0 -0
  11. package/harmony/pager_view/hvigorfile.ts +6 -0
  12. package/harmony/pager_view/index.ets +25 -0
  13. package/harmony/pager_view/obfuscation-rules.txt +18 -0
  14. package/harmony/pager_view/oh-package.json5 +21 -0
  15. package/harmony/pager_view/src/main/cpp/CMakeLists.txt +9 -0
  16. package/harmony/pager_view/src/main/cpp/RNCViewPagerTurbomodule.cpp +42 -0
  17. package/harmony/pager_view/src/main/cpp/RNCViewPagerTurbomodule.h +37 -0
  18. package/harmony/pager_view/src/main/cpp/SwiperNode.cpp +375 -0
  19. package/harmony/pager_view/src/main/cpp/SwiperNode.h +123 -0
  20. package/harmony/pager_view/src/main/cpp/ViewPagerComponentInstance.cpp +224 -0
  21. package/harmony/pager_view/src/main/cpp/ViewPagerComponentInstance.h +104 -0
  22. package/harmony/pager_view/src/main/cpp/ViewPagerPackage.h +61 -0
  23. package/harmony/pager_view/src/main/cpp/generated/RNOH/generated/BaseReactNativePagerViewPackage.h +65 -0
  24. package/harmony/pager_view/src/main/cpp/generated/RNOH/generated/components/BaseRNCViewPagerComponentInstance.h +17 -0
  25. package/harmony/pager_view/src/main/cpp/generated/RNOH/generated/components/RNCViewPagerJSIBinder.h +38 -0
  26. package/harmony/pager_view/src/main/cpp/generated/react/renderer/components/react_native_pager_view/ComponentDescriptors.h +22 -0
  27. package/harmony/pager_view/src/main/cpp/generated/react/renderer/components/react_native_pager_view/EventEmitters.cpp +45 -0
  28. package/harmony/pager_view/src/main/cpp/generated/react/renderer/components/react_native_pager_view/EventEmitters.h +54 -0
  29. package/harmony/pager_view/src/main/cpp/generated/react/renderer/components/react_native_pager_view/Props.cpp +35 -0
  30. package/harmony/pager_view/src/main/cpp/generated/react/renderer/components/react_native_pager_view/Props.h +100 -0
  31. package/harmony/pager_view/src/main/cpp/generated/react/renderer/components/react_native_pager_view/ShadowNodes.cpp +19 -0
  32. package/harmony/pager_view/src/main/cpp/generated/react/renderer/components/react_native_pager_view/ShadowNodes.h +34 -0
  33. package/harmony/pager_view/src/main/cpp/generated/react/renderer/components/react_native_pager_view/States.cpp +18 -0
  34. package/harmony/pager_view/src/main/cpp/generated/react/renderer/components/react_native_pager_view/States.h +36 -0
  35. package/harmony/pager_view/src/main/ets/Logger.ets +64 -0
  36. package/harmony/pager_view/src/main/ets/RNCViewPager.ets +281 -0
  37. package/harmony/pager_view/src/main/ets/RNCViewPagerManager.ets +58 -0
  38. package/harmony/pager_view/src/main/ets/ViewPagerModule.ts +34 -0
  39. package/harmony/pager_view/src/main/ets/ViewPagerPackage.ts +46 -0
  40. package/harmony/pager_view/src/main/module.json5 +7 -0
  41. package/harmony/pager_view/src/main/resources/base/element/string.json +8 -0
  42. package/harmony/pager_view/src/main/resources/en_US/element/string.json +8 -0
  43. package/harmony/pager_view/src/main/resources/zh_CN/element/string.json +8 -0
  44. package/harmony/pager_view/ts.ts +26 -0
  45. package/harmony/pager_view.har +0 -0
  46. package/lib/commonjs/PagerView.js +145 -0
  47. package/lib/commonjs/PagerView.js.map +1 -0
  48. package/lib/commonjs/PagerViewNativeComponent.js +14 -0
  49. package/lib/commonjs/PagerViewNativeComponent.js.map +1 -0
  50. package/lib/commonjs/index.js +22 -0
  51. package/lib/commonjs/index.js.map +1 -0
  52. package/lib/commonjs/usePagerView.js +113 -0
  53. package/lib/commonjs/usePagerView.js.map +1 -0
  54. package/lib/commonjs/utils.js +28 -0
  55. package/lib/commonjs/utils.js.map +1 -0
  56. package/lib/module/PagerView.js +138 -0
  57. package/lib/module/PagerView.js.map +1 -0
  58. package/lib/module/PagerViewNativeComponent.js +7 -0
  59. package/lib/module/PagerViewNativeComponent.js.map +1 -0
  60. package/lib/module/index.js +4 -0
  61. package/lib/module/index.js.map +1 -0
  62. package/lib/module/usePagerView.js +107 -0
  63. package/lib/module/usePagerView.js.map +1 -0
  64. package/lib/module/utils.js +20 -0
  65. package/lib/module/utils.js.map +1 -0
  66. package/lib/typescript/PagerView.d.ts +69 -0
  67. package/lib/typescript/PagerViewNativeComponent.d.ts +36 -0
  68. package/lib/typescript/index.d.ts +9 -0
  69. package/lib/typescript/usePagerView.d.ts +39 -0
  70. package/lib/typescript/utils.d.ts +2 -0
  71. package/package.json +139 -0
  72. package/src/PagerView.tsx +171 -0
  73. package/src/PagerViewNativeComponent.ts +68 -0
  74. package/src/index.tsx +27 -0
  75. package/src/usePagerView.ts +148 -0
  76. package/src/utils.tsx +19 -0
@@ -0,0 +1,171 @@
1
+ import React from 'react';
2
+ import { Platform, Keyboard } from 'react-native';
3
+ import { I18nManager } from 'react-native';
4
+ import type * as ReactNative from 'react-native';
5
+
6
+ import {
7
+ childrenWithOverriddenStyle,
8
+ } from './utils';
9
+
10
+ import PagerViewNativeComponent, {
11
+ Commands as PagerViewNativeCommands,
12
+ OnPageScrollEventData,
13
+ OnPageScrollStateChangedEventData,
14
+ OnPageSelectedEventData,
15
+ NativeProps,
16
+ } from './PagerViewNativeComponent';
17
+
18
+
19
+ /**
20
+ * Container that allows to flip left and right between child views. Each
21
+ * child view of the `PagerView` will be treated as a separate page
22
+ * and will be stretched to fill the `PagerView`.
23
+ *
24
+ * It is important all children are `<View>`s and not composite components.
25
+ * You can set style properties like `padding` or `backgroundColor` for each
26
+ * child. It is also important that each child have a `key` prop.
27
+ *
28
+ * Example:
29
+ *
30
+ * ```
31
+ * render: function() {
32
+ * return (
33
+ * <PagerView
34
+ * style={styles.PagerView}
35
+ * initialPage={0}>
36
+ * <View style={styles.pageStyle} key="1">
37
+ * <Text>First page</Text>
38
+ * </View>
39
+ * <View style={styles.pageStyle} key="2">
40
+ * <Text>Second page</Text>
41
+ * </View>
42
+ * </PagerView>
43
+ * );
44
+ * }
45
+ *
46
+ * ...
47
+ *
48
+ * var styles = {
49
+ * ...
50
+ * PagerView: {
51
+ * flex: 1
52
+ * },
53
+ * pageStyle: {
54
+ * alignItems: 'center',
55
+ * padding: 20,
56
+ * }
57
+ * }
58
+ * ```
59
+ */
60
+
61
+ export class PagerView extends React.Component<NativeProps> {
62
+ private isScrolling = false;
63
+ pagerView: React.ElementRef<typeof PagerViewNativeComponent> | null = null;
64
+
65
+
66
+ private get deducedLayoutDirection() {
67
+ if (
68
+ !this.props.layoutDirection ||
69
+ //@ts-ignore fix it
70
+ this.props.layoutDirection === 'locale'
71
+ ) {
72
+ return I18nManager.isRTL ? 'rtl' : 'ltr';
73
+ } else {
74
+ return this.props.layoutDirection;
75
+ }
76
+ }
77
+
78
+ private _onPageScroll = (
79
+ e: ReactNative.NativeSyntheticEvent<OnPageScrollEventData>
80
+ ) => {
81
+ if (this.props.onPageScroll) {
82
+ this.props.onPageScroll(e);
83
+ }
84
+
85
+ // Not implemented on iOS yet
86
+ // @ts-ignore
87
+ if (Platform.OS === 'android' || Platform.OS === 'harmony') {
88
+ if (this.props.keyboardDismissMode === 'on-drag') {
89
+ Keyboard.dismiss();
90
+ }
91
+ }
92
+ };
93
+
94
+ private _onPageScrollStateChanged = (
95
+ e: ReactNative.NativeSyntheticEvent<OnPageScrollStateChangedEventData>
96
+ ) => {
97
+ if (this.props.onPageScrollStateChanged) {
98
+ this.props.onPageScrollStateChanged(e);
99
+ }
100
+ this.isScrolling = e.nativeEvent.pageScrollState === 'dragging';
101
+ };
102
+
103
+ private _onPageSelected = (
104
+ e: ReactNative.NativeSyntheticEvent<OnPageSelectedEventData>
105
+ ) => {
106
+ if (this.props.onPageSelected) {
107
+ this.props.onPageSelected(e);
108
+ }
109
+ };
110
+
111
+ private _onMoveShouldSetResponderCapture = () => {
112
+ return this.isScrolling;
113
+ };
114
+
115
+ /**
116
+ * A helper function to scroll to a specific page in the PagerView.
117
+ * The transition between pages will be animated.
118
+ */
119
+ public setPage = (selectedPage: number) => {
120
+ if (this.pagerView) {
121
+ PagerViewNativeCommands.setPage(this.pagerView, selectedPage);
122
+ }
123
+ };
124
+
125
+ /**
126
+ * A helper function to scroll to a specific page in the PagerView.
127
+ * The transition between pages will *not* be animated.
128
+ */
129
+ public setPageWithoutAnimation = (selectedPage: number) => {
130
+ if (this.pagerView) {
131
+ PagerViewNativeCommands.setPageWithoutAnimation(
132
+ this.pagerView,
133
+ selectedPage
134
+ );
135
+ }
136
+ };
137
+
138
+ /**
139
+ * A helper function to enable/disable scroll imperatively
140
+ * The recommended way is using the scrollEnabled prop, however, there might be a case where a
141
+ * imperative solution is more useful (e.g. for not blocking an animation)
142
+ */
143
+ public setScrollEnabled = (scrollEnabled: boolean) => {
144
+ if (this.pagerView) {
145
+ PagerViewNativeCommands.setScrollEnabledImperatively(
146
+ this.pagerView,
147
+ scrollEnabled
148
+ );
149
+ }
150
+ };
151
+
152
+ render() {
153
+ return (
154
+ <PagerViewNativeComponent
155
+ {...this.props}
156
+ ref={(ref) => {
157
+ this.pagerView = ref;
158
+ }}
159
+ style={this.props.style}
160
+ layoutDirection={this.deducedLayoutDirection}
161
+ onPageScroll={this._onPageScroll}
162
+ onPageScrollStateChanged={this._onPageScrollStateChanged}
163
+ onPageSelected={this._onPageSelected}
164
+ onMoveShouldSetResponderCapture={
165
+ this._onMoveShouldSetResponderCapture
166
+ }
167
+ children={childrenWithOverriddenStyle(this.props.children)}
168
+ />
169
+ );
170
+ }
171
+ }
@@ -0,0 +1,68 @@
1
+ import type * as React from 'react';
2
+ import type { HostComponent, ViewProps } from 'react-native';
3
+ import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands';
4
+ import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
5
+
6
+ import type {
7
+ DirectEventHandler,
8
+ Double,
9
+ Int32,
10
+ WithDefault,
11
+ } from 'react-native/Libraries/Types/CodegenTypes';
12
+
13
+ export type OnPageScrollEventData = Readonly<{
14
+ position: Double;
15
+ offset: Double;
16
+ }>;
17
+
18
+ export type OnPageSelectedEventData = Readonly<{
19
+ position: Double;
20
+ }>;
21
+
22
+ export type OnPageScrollStateChangedEventData = Readonly<{
23
+ pageScrollState: 'idle' | 'dragging' | 'settling';
24
+ }>;
25
+
26
+ export interface NativeProps extends ViewProps {
27
+ scrollEnabled?: WithDefault<boolean, true>;
28
+ layoutDirection?: WithDefault<'ltr' | 'rtl', 'ltr'>;
29
+ initialPage?: Int32;
30
+ orientation?: WithDefault<'horizontal' | 'vertical', 'horizontal'>;
31
+ offscreenPageLimit?: Int32;
32
+ pageMargin?: Int32;
33
+ overScrollMode?: WithDefault<'auto' | 'always' | 'never', 'auto'>;
34
+ overdrag?: WithDefault<boolean, false>;
35
+ keyboardDismissMode?: WithDefault<'none' | 'on-drag', 'none'>;
36
+ onPageScroll?: DirectEventHandler<OnPageScrollEventData>;
37
+ onPageSelected?: DirectEventHandler<OnPageSelectedEventData>;
38
+ onPageScrollStateChanged?: DirectEventHandler<OnPageScrollStateChangedEventData>;
39
+ }
40
+
41
+ type PagerViewViewType = HostComponent<NativeProps>;
42
+
43
+ export interface NativeCommands {
44
+ setPage: (
45
+ viewRef: React.ElementRef<PagerViewViewType>,
46
+ selectedPage: Int32
47
+ ) => void;
48
+ setPageWithoutAnimation: (
49
+ viewRef: React.ElementRef<PagerViewViewType>,
50
+ selectedPage: Int32
51
+ ) => void;
52
+ setScrollEnabledImperatively: (
53
+ viewRef: React.ElementRef<PagerViewViewType>,
54
+ scrollEnabled: boolean
55
+ ) => void;
56
+ }
57
+
58
+ export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
59
+ supportedCommands: [
60
+ 'setPage',
61
+ 'setPageWithoutAnimation',
62
+ 'setScrollEnabledImperatively',
63
+ ],
64
+ });
65
+
66
+ export default codegenNativeComponent<NativeProps>(
67
+ 'RNCViewPager'
68
+ ) as HostComponent<NativeProps>;
package/src/index.tsx ADDED
@@ -0,0 +1,27 @@
1
+ import type * as ReactNative from 'react-native';
2
+ import { PagerView } from './PagerView';
3
+ export default PagerView;
4
+ export * from './usePagerView';
5
+
6
+ import type {
7
+ OnPageScrollEventData as PagerViewOnPageScrollEventData,
8
+ OnPageSelectedEventData as PagerViewOnPageSelectedEventData,
9
+ OnPageScrollStateChangedEventData as PageScrollStateChangedNativeEventData,
10
+ NativeProps,
11
+ } from './PagerViewNativeComponent';
12
+
13
+ export type {
14
+ PagerViewOnPageScrollEventData,
15
+ PagerViewOnPageSelectedEventData,
16
+ PageScrollStateChangedNativeEventData,
17
+ NativeProps as PagerViewProps,
18
+ };
19
+
20
+ export type PagerViewOnPageScrollEvent =
21
+ ReactNative.NativeSyntheticEvent<PagerViewOnPageScrollEventData>;
22
+
23
+ export type PagerViewOnPageSelectedEvent =
24
+ ReactNative.NativeSyntheticEvent<PagerViewOnPageSelectedEventData>;
25
+
26
+ export type PageScrollStateChangedNativeEvent =
27
+ ReactNative.NativeSyntheticEvent<PageScrollStateChangedNativeEventData>;
@@ -0,0 +1,148 @@
1
+ import type * as ReactNative from 'react-native';
2
+ import type {
3
+ OnPageScrollEventData as PagerViewOnPageScrollEventData,
4
+ OnPageSelectedEventData as PagerViewOnPageSelectedEventData,
5
+ OnPageScrollStateChangedEventData as PageScrollStateChangedNativeEventData,
6
+ } from './PagerViewNativeComponent';
7
+
8
+ type PageScrollStateChangedNativeEvent =
9
+ ReactNative.NativeSyntheticEvent<PageScrollStateChangedNativeEventData>;
10
+
11
+ import { PagerView } from './PagerView';
12
+
13
+ import { Animated } from 'react-native';
14
+ import { useCallback, useMemo, useRef, useState } from 'react';
15
+
16
+ export type UsePagerViewProps = ReturnType<typeof usePagerView>;
17
+
18
+ const AnimatedPagerView = Animated.createAnimatedComponent(PagerView);
19
+
20
+ type UsePagerViewParams = {
21
+ pagesAmount: number;
22
+ };
23
+
24
+ export function usePagerView(
25
+ { pagesAmount }: UsePagerViewParams = { pagesAmount: 0 }
26
+ ) {
27
+ const ref = useRef<PagerView>(null);
28
+ const [pages, setPages] = useState<number[]>(
29
+ new Array(pagesAmount).fill('').map((_v, index) => index)
30
+ );
31
+ const [activePage, setActivePage] = useState(0);
32
+ const [isAnimated, setIsAnimated] = useState(true);
33
+ const [overdragEnabled, setOverdragEnabled] = useState(false);
34
+ const [scrollEnabled, setScrollEnabled] = useState(true);
35
+ const [scrollState, setScrollState] = useState('idle');
36
+ const [progress, setProgress] = useState({ position: 0, offset: 0 });
37
+ const onPageScrollOffset = useRef(new Animated.Value(0)).current;
38
+ const onPageScrollPosition = useRef(new Animated.Value(0)).current;
39
+ const onPageSelectedPosition = useRef(new Animated.Value(0)).current;
40
+
41
+ const setPage = useCallback(
42
+ (page: number) =>
43
+ isAnimated
44
+ ? ref.current?.setPage(page)
45
+ : ref.current?.setPageWithoutAnimation(page),
46
+ [isAnimated]
47
+ );
48
+
49
+ const addPage = useCallback(() => {
50
+ setPages((prevPages) => {
51
+ return [...prevPages, prevPages.length];
52
+ });
53
+ }, []);
54
+
55
+ const removePage = useCallback(() => {
56
+ setPages((prevPages) => {
57
+ if (prevPages.length === 1) {
58
+ return prevPages;
59
+ }
60
+ return prevPages.slice(0, prevPages.length - 1);
61
+ });
62
+ }, []);
63
+
64
+ const toggleAnimation = useCallback(
65
+ () => setIsAnimated((animated) => !animated),
66
+ []
67
+ );
68
+
69
+ const toggleScroll = useCallback(
70
+ () => setScrollEnabled((enabled) => !enabled),
71
+ []
72
+ );
73
+
74
+ const toggleOverdrag = useCallback(
75
+ () => setOverdragEnabled((enabled) => !enabled),
76
+ []
77
+ );
78
+
79
+ const onPageScroll = useMemo(
80
+ () =>
81
+ Animated.event<PagerViewOnPageScrollEventData>(
82
+ [
83
+ {
84
+ nativeEvent: {
85
+ offset: onPageScrollOffset,
86
+ position: onPageScrollPosition,
87
+ },
88
+ },
89
+ ],
90
+ {
91
+ listener: ({ nativeEvent: { offset, position } }) => {
92
+ setProgress({
93
+ position,
94
+ offset,
95
+ });
96
+ },
97
+ useNativeDriver: true,
98
+ }
99
+ ),
100
+ // eslint-disable-next-line react-hooks/exhaustive-deps
101
+ []
102
+ );
103
+
104
+ const onPageSelected = useMemo(
105
+ () =>
106
+ Animated.event<PagerViewOnPageSelectedEventData>(
107
+ [{ nativeEvent: { position: onPageSelectedPosition } }],
108
+ {
109
+ listener: ({ nativeEvent: { position } }) => {
110
+ setActivePage(position);
111
+ },
112
+ useNativeDriver: true,
113
+ }
114
+ ),
115
+ // eslint-disable-next-line react-hooks/exhaustive-deps
116
+ []
117
+ );
118
+
119
+ const onPageScrollStateChanged = useCallback(
120
+ (e: PageScrollStateChangedNativeEvent) => {
121
+ setScrollState(e.nativeEvent.pageScrollState);
122
+ },
123
+ []
124
+ );
125
+
126
+ return {
127
+ ref,
128
+ activePage,
129
+ isAnimated,
130
+ pages,
131
+ scrollState,
132
+ scrollEnabled,
133
+ progress,
134
+ overdragEnabled,
135
+ setPage,
136
+ addPage,
137
+ removePage,
138
+ toggleScroll,
139
+ toggleAnimation,
140
+ setProgress,
141
+ onPageScroll,
142
+ onPageSelected,
143
+ onPageScrollStateChanged,
144
+ toggleOverdrag,
145
+ AnimatedPagerView,
146
+ PagerView,
147
+ };
148
+ }
package/src/utils.tsx ADDED
@@ -0,0 +1,19 @@
1
+ import React, { Children, ReactNode } from 'react';
2
+ import { StyleSheet, View } from 'react-native';
3
+
4
+ export const childrenWithOverriddenStyle = (children?: ReactNode) => {
5
+ return Children.map(children, (child) => {
6
+ const element = child as React.ReactElement;
7
+ return (
8
+ // Add a wrapper to ensure layout is calculated correctly
9
+ <View style={StyleSheet.absoluteFill} collapsable={false}>
10
+ {/* @ts-ignore */}
11
+ {React.cloneElement(element, {
12
+ ...element.props,
13
+ // Override styles so that each page will fill the parent.
14
+ style: [element.props.style, StyleSheet.absoluteFill],
15
+ })}
16
+ </View>
17
+ );
18
+ });
19
+ };