@react-native-ohos/react-native-tab-view 4.0.11-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 (86) hide show
  1. package/LICENSE +21 -0
  2. package/README.OpenSource +11 -0
  3. package/README.md +9 -0
  4. package/lib/module/Pager.android.js +4 -0
  5. package/lib/module/Pager.android.js.map +1 -0
  6. package/lib/module/Pager.ios.js +4 -0
  7. package/lib/module/Pager.ios.js.map +1 -0
  8. package/lib/module/Pager.js +4 -0
  9. package/lib/module/Pager.js.map +1 -0
  10. package/lib/module/PagerViewAdapter.js +126 -0
  11. package/lib/module/PagerViewAdapter.js.map +1 -0
  12. package/lib/module/PanResponderAdapter.js +200 -0
  13. package/lib/module/PanResponderAdapter.js.map +1 -0
  14. package/lib/module/PlatformPressable.js +59 -0
  15. package/lib/module/PlatformPressable.js.map +1 -0
  16. package/lib/module/SceneMap.js +24 -0
  17. package/lib/module/SceneMap.js.map +1 -0
  18. package/lib/module/SceneView.js +73 -0
  19. package/lib/module/SceneView.js.map +1 -0
  20. package/lib/module/TabBar.js +472 -0
  21. package/lib/module/TabBar.js.map +1 -0
  22. package/lib/module/TabBarIndicator.js +122 -0
  23. package/lib/module/TabBarIndicator.js.map +1 -0
  24. package/lib/module/TabBarItem.js +218 -0
  25. package/lib/module/TabBarItem.js.map +1 -0
  26. package/lib/module/TabBarItemLabel.js +33 -0
  27. package/lib/module/TabBarItemLabel.js.map +1 -0
  28. package/lib/module/TabView.js +140 -0
  29. package/lib/module/TabView.js.map +1 -0
  30. package/lib/module/index.js +8 -0
  31. package/lib/module/index.js.map +1 -0
  32. package/lib/module/package.json +1 -0
  33. package/lib/module/types.js +4 -0
  34. package/lib/module/types.js.map +1 -0
  35. package/lib/module/useAnimatedValue.js +12 -0
  36. package/lib/module/useAnimatedValue.js.map +1 -0
  37. package/lib/typescript/package.json +1 -0
  38. package/lib/typescript/src/Pager.android.d.ts +2 -0
  39. package/lib/typescript/src/Pager.android.d.ts.map +1 -0
  40. package/lib/typescript/src/Pager.d.ts +2 -0
  41. package/lib/typescript/src/Pager.d.ts.map +1 -0
  42. package/lib/typescript/src/Pager.ios.d.ts +2 -0
  43. package/lib/typescript/src/Pager.ios.d.ts.map +1 -0
  44. package/lib/typescript/src/PagerViewAdapter.d.ts +15 -0
  45. package/lib/typescript/src/PagerViewAdapter.d.ts.map +1 -0
  46. package/lib/typescript/src/PanResponderAdapter.d.ts +16 -0
  47. package/lib/typescript/src/PanResponderAdapter.d.ts.map +1 -0
  48. package/lib/typescript/src/PlatformPressable.d.ts +13 -0
  49. package/lib/typescript/src/PlatformPressable.d.ts.map +1 -0
  50. package/lib/typescript/src/SceneMap.d.ts +10 -0
  51. package/lib/typescript/src/SceneMap.d.ts.map +1 -0
  52. package/lib/typescript/src/SceneView.d.ts +16 -0
  53. package/lib/typescript/src/SceneView.d.ts.map +1 -0
  54. package/lib/typescript/src/TabBar.d.ts +32 -0
  55. package/lib/typescript/src/TabBar.d.ts.map +1 -0
  56. package/lib/typescript/src/TabBarIndicator.d.ts +15 -0
  57. package/lib/typescript/src/TabBarIndicator.d.ts.map +1 -0
  58. package/lib/typescript/src/TabBarItem.d.ts +19 -0
  59. package/lib/typescript/src/TabBarItem.d.ts.map +1 -0
  60. package/lib/typescript/src/TabBarItemLabel.d.ts +11 -0
  61. package/lib/typescript/src/TabBarItemLabel.d.ts.map +1 -0
  62. package/lib/typescript/src/TabView.d.ts +30 -0
  63. package/lib/typescript/src/TabView.d.ts.map +1 -0
  64. package/lib/typescript/src/index.d.ts +11 -0
  65. package/lib/typescript/src/index.d.ts.map +1 -0
  66. package/lib/typescript/src/types.d.ts +70 -0
  67. package/lib/typescript/src/types.d.ts.map +1 -0
  68. package/lib/typescript/src/useAnimatedValue.d.ts +3 -0
  69. package/lib/typescript/src/useAnimatedValue.d.ts.map +1 -0
  70. package/package.json +79 -0
  71. package/src/Pager.android.tsx +1 -0
  72. package/src/Pager.ios.tsx +1 -0
  73. package/src/Pager.tsx +1 -0
  74. package/src/PagerViewAdapter.tsx +182 -0
  75. package/src/PanResponderAdapter.tsx +339 -0
  76. package/src/PlatformPressable.tsx +75 -0
  77. package/src/SceneMap.tsx +30 -0
  78. package/src/SceneView.tsx +107 -0
  79. package/src/TabBar.tsx +729 -0
  80. package/src/TabBarIndicator.tsx +190 -0
  81. package/src/TabBarItem.tsx +305 -0
  82. package/src/TabBarItemLabel.tsx +42 -0
  83. package/src/TabView.tsx +195 -0
  84. package/src/index.tsx +15 -0
  85. package/src/types.tsx +87 -0
  86. package/src/useAnimatedValue.tsx +12 -0
@@ -0,0 +1,195 @@
1
+ import * as React from 'react';
2
+ import {
3
+ I18nManager,
4
+ type LayoutChangeEvent,
5
+ Platform,
6
+ type StyleProp,
7
+ StyleSheet,
8
+ View,
9
+ type ViewStyle,
10
+ } from 'react-native';
11
+
12
+ import { Pager } from './Pager';
13
+ import { SceneView } from './SceneView';
14
+ import { TabBar } from './TabBar';
15
+ import type {
16
+ Layout,
17
+ LocaleDirection,
18
+ NavigationState,
19
+ PagerProps,
20
+ Route,
21
+ SceneRendererProps,
22
+ TabDescriptor,
23
+ } from './types';
24
+
25
+ export type Props<T extends Route> = Omit<PagerProps, 'layoutDirection'> & {
26
+ onIndexChange: (index: number) => void;
27
+ navigationState: NavigationState<T>;
28
+ renderLazyPlaceholder?: (props: { route: T }) => React.ReactNode;
29
+ renderTabBar?: (
30
+ props: SceneRendererProps & {
31
+ navigationState: NavigationState<T>;
32
+ options: Record<string, TabDescriptor<T>> | undefined;
33
+ }
34
+ ) => React.ReactNode;
35
+ tabBarPosition?: 'top' | 'bottom';
36
+ initialLayout?: Partial<Layout>;
37
+ lazy?: ((props: { route: T }) => boolean) | boolean;
38
+ lazyPreloadDistance?: number;
39
+ direction?: LocaleDirection;
40
+ pagerStyle?: StyleProp<ViewStyle>;
41
+ style?: StyleProp<ViewStyle>;
42
+ renderScene: (props: SceneRendererProps & { route: T }) => React.ReactNode;
43
+ options?: Record<string, TabDescriptor<T>>;
44
+ commonOptions?: TabDescriptor<T>;
45
+ };
46
+
47
+ const renderLazyPlaceholderDefault = () => null;
48
+
49
+ export function TabView<T extends Route>({
50
+ onIndexChange,
51
+ navigationState,
52
+ renderScene,
53
+ initialLayout,
54
+ keyboardDismissMode = 'auto',
55
+ lazy = false,
56
+ lazyPreloadDistance = 0,
57
+ onSwipeStart,
58
+ onSwipeEnd,
59
+ renderLazyPlaceholder = renderLazyPlaceholderDefault,
60
+ // eslint-disable-next-line @eslint-react/no-unstable-default-props
61
+ renderTabBar = (props) => <TabBar {...props} />,
62
+ pagerStyle,
63
+ style,
64
+ direction = I18nManager.getConstants().isRTL ? 'rtl' : 'ltr',
65
+ swipeEnabled = true,
66
+ tabBarPosition = 'top',
67
+ animationEnabled = true,
68
+ overScrollMode,
69
+ options: sceneOptions,
70
+ commonOptions,
71
+ }: Props<T>) {
72
+ if (
73
+ Platform.OS !== 'web' &&
74
+ direction !== (I18nManager.getConstants().isRTL ? 'rtl' : 'ltr')
75
+ ) {
76
+ console.warn(
77
+ `The 'direction' prop is set to '${direction}' but the effective value is '${
78
+ I18nManager.getConstants().isRTL ? 'rtl' : 'ltr'
79
+ }'. This is not supported. Please use I18nManager.forceRTL to change the layout direction.`
80
+ );
81
+ }
82
+
83
+ const [layout, setLayout] = React.useState({
84
+ width: 0,
85
+ height: 0,
86
+ ...initialLayout,
87
+ });
88
+
89
+ const jumpToIndex = (index: number) => {
90
+ if (index !== navigationState.index) {
91
+ onIndexChange(index);
92
+ }
93
+ };
94
+
95
+ const handleLayout = (e: LayoutChangeEvent) => {
96
+ const { height, width } = e.nativeEvent.layout;
97
+
98
+ setLayout((prevLayout) => {
99
+ if (prevLayout.width === width && prevLayout.height === height) {
100
+ return prevLayout;
101
+ }
102
+
103
+ return { height, width };
104
+ });
105
+ };
106
+
107
+ const options = Object.fromEntries(
108
+ navigationState.routes.map((route) => [
109
+ route.key,
110
+ {
111
+ ...commonOptions,
112
+ ...sceneOptions?.[route.key],
113
+ },
114
+ ])
115
+ );
116
+
117
+ return (
118
+ <View onLayout={handleLayout} style={[styles.pager, style]}>
119
+ <Pager
120
+ layout={layout}
121
+ navigationState={navigationState}
122
+ keyboardDismissMode={keyboardDismissMode}
123
+ swipeEnabled={swipeEnabled}
124
+ onSwipeStart={onSwipeStart}
125
+ onSwipeEnd={onSwipeEnd}
126
+ onIndexChange={jumpToIndex}
127
+ animationEnabled={animationEnabled}
128
+ overScrollMode={overScrollMode}
129
+ style={pagerStyle}
130
+ layoutDirection={direction}
131
+ >
132
+ {({ position, render, addEnterListener, jumpTo }) => {
133
+ // All the props here must not change between re-renders
134
+ // This is crucial to optimizing the routes with PureComponent
135
+ const sceneRendererProps = {
136
+ position,
137
+ layout,
138
+ jumpTo,
139
+ };
140
+
141
+ return (
142
+ <React.Fragment>
143
+ {tabBarPosition === 'top' &&
144
+ renderTabBar({
145
+ ...sceneRendererProps,
146
+ options,
147
+ navigationState,
148
+ })}
149
+ {render(
150
+ navigationState.routes.map((route, i) => {
151
+ const { sceneStyle } = options?.[route.key] ?? {};
152
+
153
+ return (
154
+ <SceneView
155
+ {...sceneRendererProps}
156
+ addEnterListener={addEnterListener}
157
+ key={route.key}
158
+ index={i}
159
+ lazy={typeof lazy === 'function' ? lazy({ route }) : lazy}
160
+ lazyPreloadDistance={lazyPreloadDistance}
161
+ navigationState={navigationState}
162
+ style={sceneStyle}
163
+ >
164
+ {({ loading }) =>
165
+ loading
166
+ ? renderLazyPlaceholder({ route })
167
+ : renderScene({
168
+ ...sceneRendererProps,
169
+ route,
170
+ })
171
+ }
172
+ </SceneView>
173
+ );
174
+ })
175
+ )}
176
+ {tabBarPosition === 'bottom' &&
177
+ renderTabBar({
178
+ ...sceneRendererProps,
179
+ options,
180
+ navigationState,
181
+ })}
182
+ </React.Fragment>
183
+ );
184
+ }}
185
+ </Pager>
186
+ </View>
187
+ );
188
+ }
189
+
190
+ const styles = StyleSheet.create({
191
+ pager: {
192
+ flex: 1,
193
+ overflow: 'hidden',
194
+ },
195
+ });
package/src/index.tsx ADDED
@@ -0,0 +1,15 @@
1
+ export { SceneMap } from './SceneMap';
2
+ export type { Props as TabBarProps } from './TabBar';
3
+ export { TabBar } from './TabBar';
4
+ export type { Props as TabBarIndicatorProps } from './TabBarIndicator';
5
+ export { TabBarIndicator } from './TabBarIndicator';
6
+ export type { Props as TabBarItemProps } from './TabBarItem';
7
+ export { TabBarItem } from './TabBarItem';
8
+ export type { Props as TabViewProps } from './TabView';
9
+ export { TabView } from './TabView';
10
+ export type {
11
+ NavigationState,
12
+ Route,
13
+ SceneRendererProps,
14
+ TabDescriptor,
15
+ } from './types';
package/src/types.tsx ADDED
@@ -0,0 +1,87 @@
1
+ import type { Animated, StyleProp, TextStyle, ViewStyle } from 'react-native';
2
+ import type { PagerViewProps } from 'react-native-pager-view';
3
+
4
+ export type TabDescriptor<T extends Route> = {
5
+ accessibilityLabel?: string;
6
+ accessible?: boolean;
7
+ testID?: string;
8
+ labelText?: string;
9
+ labelAllowFontScaling?: boolean;
10
+ href?: string;
11
+ label?: (props: {
12
+ route: T;
13
+ labelText?: string;
14
+ focused: boolean;
15
+ color: string;
16
+ allowFontScaling?: boolean;
17
+ style?: StyleProp<TextStyle>;
18
+ }) => React.ReactNode;
19
+ labelStyle?: StyleProp<TextStyle>;
20
+ icon?: (props: {
21
+ route: T;
22
+ focused: boolean;
23
+ color: string;
24
+ size: number;
25
+ }) => React.ReactNode;
26
+ badge?: (props: { route: T }) => React.ReactElement;
27
+ sceneStyle?: StyleProp<ViewStyle>;
28
+ };
29
+
30
+ export type LocaleDirection = 'ltr' | 'rtl';
31
+
32
+ export type Route = {
33
+ key: string;
34
+ icon?: string;
35
+ title?: string;
36
+ accessible?: boolean;
37
+ accessibilityLabel?: string;
38
+ testID?: string;
39
+ };
40
+
41
+ export type Event = {
42
+ defaultPrevented: boolean;
43
+ preventDefault(): void;
44
+ };
45
+
46
+ export type Scene<T extends Route> = {
47
+ route: T;
48
+ };
49
+
50
+ export type NavigationState<T extends Route> = {
51
+ index: number;
52
+ routes: T[];
53
+ };
54
+
55
+ export type Layout = {
56
+ width: number;
57
+ height: number;
58
+ };
59
+
60
+ export type Listener = (value: number) => void;
61
+
62
+ export type SceneRendererProps = {
63
+ layout: Layout;
64
+ position: Animated.AnimatedInterpolation<number>;
65
+ jumpTo: (key: string) => void;
66
+ };
67
+
68
+ export type EventEmitterProps = {
69
+ addEnterListener: (listener: Listener) => () => void;
70
+ };
71
+
72
+ export type PagerProps = Omit<
73
+ PagerViewProps,
74
+ | 'initialPage'
75
+ | 'scrollEnabled'
76
+ | 'onPageScroll'
77
+ | 'onPageSelected'
78
+ | 'onPageScrollStateChanged'
79
+ | 'keyboardDismissMode'
80
+ | 'children'
81
+ > & {
82
+ keyboardDismissMode?: 'none' | 'on-drag' | 'auto';
83
+ swipeEnabled?: boolean;
84
+ animationEnabled?: boolean;
85
+ onSwipeStart?: () => void;
86
+ onSwipeEnd?: () => void;
87
+ };
@@ -0,0 +1,12 @@
1
+ import * as React from 'react';
2
+ import { Animated } from 'react-native';
3
+
4
+ export function useAnimatedValue(initialValue: number) {
5
+ const lazyRef = React.useRef<Animated.Value>();
6
+
7
+ if (lazyRef.current === undefined) {
8
+ lazyRef.current = new Animated.Value(initialValue);
9
+ }
10
+
11
+ return lazyRef.current as Animated.Value;
12
+ }