Skip to content

Commit 3b8a67e

Browse files
chore(ts): setup codegen spec & fix types (#601)
* feat: add codegenConfig * feat: update native spec & types * fix: add missing native props * fix: change codegenConfig format * fix: fix types for codegen Co-authored-by: krozniata <[email protected]> Co-authored-by: okwasniewski <[email protected]>
1 parent fcbc131 commit 3b8a67e

File tree

5 files changed

+79
-45
lines changed

5 files changed

+79
-45
lines changed

example/src/App.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type State = {
2929
dotsVisible: boolean;
3030
};
3131
export default class App extends React.Component<{}, State> {
32-
viewPager: React.Ref<typeof ViewPager>;
32+
viewPager: { current: React.ElementRef<typeof ViewPager> | null };
3333

3434
constructor(props: any) {
3535
super(props);
@@ -84,11 +84,9 @@ export default class App extends React.Component<{}, State> {
8484

8585
go = (page: number) => {
8686
if (this.state.animationsAreEnabled) {
87-
/* @ts-ignore */
88-
this.viewPager.current.setPage(page);
87+
this.viewPager?.current?.setPage(page);
8988
} else {
90-
/* @ts-ignore */
91-
this.viewPager.current.setPageWithoutAnimation(page);
89+
this.viewPager?.current?.setPageWithoutAnimation(page);
9290
}
9391
};
9492

@@ -120,7 +118,6 @@ export default class App extends React.Component<{}, State> {
120118
// Lib does not support dynamically orientation change
121119
orientation="horizontal"
122120
showPageIndicator={dotsVisible}
123-
/* @ts-ignore */
124121
ref={this.viewPager}
125122
>
126123
{pages.map((p) => this.renderPage(p))}

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@
134134
"trailingComma": "es5",
135135
"useTabs": false
136136
},
137+
"codegenConfig": {
138+
"name": "PagerViewView",
139+
"type": "components",
140+
"jsSrcsDir": "src"
141+
},
137142
"react-native-builder-bob": {
138143
"source": "src",
139144
"output": "lib",

src/PagerView.tsx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { ReactElement } from 'react';
2-
import { Platform, UIManager, Keyboard } from 'react-native';
3-
import ReactNative, { I18nManager } from 'react-native';
1+
import React from 'react';
2+
import { Platform, Keyboard } from 'react-native';
3+
import { I18nManager } from 'react-native';
44
import type {
55
PagerViewOnPageScrollEvent,
66
PagerViewOnPageSelectedEvent,
@@ -9,7 +9,9 @@ import type {
99
} from './types';
1010

1111
import { childrenWithOverriddenStyle } from './utils';
12-
import PagerViewView, { NativeCommands } from './PagerViewViewNativeComponent';
12+
import PagerViewView, {
13+
Commands as PagerViewCommands,
14+
} from './PagerViewViewNativeComponent';
1315

1416
/**
1517
* Container that allows to flip left and right between child views. Each
@@ -55,7 +57,7 @@ import PagerViewView, { NativeCommands } from './PagerViewViewNativeComponent';
5557

5658
export class PagerView extends React.Component<PagerViewProps> {
5759
private isScrolling = false;
58-
private PagerView = React.createRef<NativeCommands>();
60+
pagerView: React.ElementRef<typeof PagerViewView> | null = null;
5961

6062
private _onPageScroll = (e: PagerViewOnPageScrollEvent) => {
6163
if (this.props.onPageScroll) {
@@ -90,20 +92,19 @@ export class PagerView extends React.Component<PagerViewProps> {
9092
* The transition between pages will be animated.
9193
*/
9294
public setPage = (selectedPage: number) => {
93-
//@ts-ignore FIX IT
94-
this.PagerView.current?.setPage(this.PagerView.current, selectedPage);
95+
if (this.pagerView) {
96+
PagerViewCommands.setPage(this.pagerView, selectedPage);
97+
}
9598
};
9699

97100
/**
98101
* A helper function to scroll to a specific page in the PagerView.
99102
* The transition between pages will *not* be animated.
100103
*/
101104
public setPageWithoutAnimation = (selectedPage: number) => {
102-
//@ts-ignore FIX IT
103-
this.PagerView.current?.setPageWithoutAnimation(
104-
this.PagerView.current,
105-
selectedPage
106-
);
105+
if (this.pagerView) {
106+
PagerViewCommands.setPageWithoutAnimation(this.pagerView, selectedPage);
107+
}
107108
};
108109

109110
/**
@@ -112,11 +113,9 @@ export class PagerView extends React.Component<PagerViewProps> {
112113
* imperative solution is more useful (e.g. for not blocking an animation)
113114
*/
114115
public setScrollEnabled = (scrollEnabled: boolean) => {
115-
//@ts-ignore FIX IT
116-
this.PagerView.current?.setScrollEnabled(
117-
this.PagerView.current,
118-
scrollEnabled
119-
);
116+
if (this.pagerView) {
117+
PagerViewCommands.setScrollEnabled(this.pagerView, scrollEnabled);
118+
}
120119
};
121120

122121
private _onMoveShouldSetResponderCapture = () => {
@@ -138,7 +137,9 @@ export class PagerView extends React.Component<PagerViewProps> {
138137
return (
139138
<PagerViewView
140139
{...this.props}
141-
ref={this.PagerView as any /** TODO: Fix ref type */}
140+
ref={(ref) => {
141+
this.pagerView = ref;
142+
}}
142143
style={this.props.style}
143144
layoutDirection={this.deducedLayoutDirection}
144145
onPageScroll={this._onPageScroll}
Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,55 @@
1-
import codegenNativeComponent, {
2-
NativeComponentType,
3-
} from 'react-native/Libraries/Utilities/codegenNativeComponent';
4-
import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands';
51
import type * as React from 'react';
6-
import type { PagerViewProps } from './types';
2+
import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands';
3+
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
4+
import type { HostComponent, ViewProps } from 'react-native';
5+
import type {
6+
BubblingEventHandler,
7+
DirectEventHandler,
8+
Double,
9+
Int32,
10+
WithDefault,
11+
} from 'react-native/Libraries/Types/CodegenTypes';
12+
13+
type OnPageScrollEventData = Readonly<{
14+
position: Double;
15+
offset: Double;
16+
}>;
17+
18+
type OnPageSelectedEventData = Readonly<{
19+
position: Double;
20+
}>;
721

8-
const VIEW_MANAGER_NAME = 'PagerViewView';
22+
type OnPageScrollStateChangedEventData = Readonly<{
23+
pageScrollState: 'idle' | 'dragging' | 'settling';
24+
}>;
25+
26+
export type PagerViewOnPageScrollEventData = OnPageScrollEventData;
27+
export type PagerViewOnPageSelectedEventData = OnPageSelectedEventData;
28+
export type PageScrollStateChangedEvent = OnPageScrollStateChangedEventData;
29+
30+
interface NativeProps extends ViewProps {
31+
scrollEnabled?: WithDefault<boolean, true>;
32+
layoutDirection?: WithDefault<'rtl' | 'ltr' | 'locale', 'rtl'>;
33+
initialPage?: Int32;
34+
orientation?: WithDefault<'horizontal' | 'vertical', 'horizontal'>;
35+
offscreenPageLimit?: Int32;
36+
pageMargin?: Int32;
37+
overScrollMode?: WithDefault<'auto' | 'always' | 'never', 'auto'>;
38+
onPageScroll: BubblingEventHandler<OnPageScrollEventData>;
39+
onPageSelected: DirectEventHandler<OnPageSelectedEventData>;
40+
onPageScrollStateChanged: DirectEventHandler<OnPageScrollStateChangedEventData>;
41+
}
942

10-
export type PagerViewViewType = NativeComponentType<PagerViewProps>;
43+
type PagerViewViewType = HostComponent<NativeProps>;
1144

1245
export interface NativeCommands {
1346
setPage: (
1447
viewRef: React.ElementRef<PagerViewViewType>,
15-
selectedPage: number
48+
selectedPage: Int32
1649
) => void;
1750
setPageWithoutAnimation: (
1851
viewRef: React.ElementRef<PagerViewViewType>,
19-
selectedPage: number
52+
selectedPage: Int32
2053
) => void;
2154
setScrollEnabled: (
2255
viewRef: React.ElementRef<PagerViewViewType>,
@@ -28,4 +61,6 @@ export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
2861
supportedCommands: ['setPage', 'setPageWithoutAnimation', 'setScrollEnabled'],
2962
});
3063

31-
export default codegenNativeComponent<PagerViewProps>(VIEW_MANAGER_NAME);
64+
export default codegenNativeComponent<NativeProps>(
65+
'PagerViewView'
66+
) as HostComponent<NativeProps>;

src/types.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
import type * as ReactNative from 'react-native';
2+
import type {
3+
PagerViewOnPageScrollEventData,
4+
PagerViewOnPageSelectedEventData,
5+
PageScrollStateChangedEvent,
6+
} from './PagerViewViewNativeComponent';
27

38
export type Orientation = 'horizontal' | 'vertical';
49
export type OverScrollMode = 'auto' | 'always' | 'never';
510
export type PageScrollState = 'idle' | 'dragging' | 'settling';
611

712
export type PagerViewOnPageScrollEvent =
813
ReactNative.NativeSyntheticEvent<PagerViewOnPageScrollEventData>;
9-
export interface PagerViewOnPageScrollEventData {
10-
position: number;
11-
offset: number;
12-
}
1314

1415
export type PagerViewOnPageSelectedEvent =
1516
ReactNative.NativeSyntheticEvent<PagerViewOnPageSelectedEventData>;
16-
export interface PagerViewOnPageSelectedEventData {
17-
position: number;
18-
}
1917

2018
export type PageScrollStateChangedNativeEvent =
2119
ReactNative.NativeSyntheticEvent<PageScrollStateChangedEvent>;
22-
export interface PageScrollStateChangedEvent {
23-
pageScrollState: PageScrollState;
24-
}
2520

26-
export interface PagerViewProps extends ReactNative.ViewProps {
21+
export interface PagerViewProps {
2722
/**
2823
* Index of initial page that should be selected. Use `setPage` method to
2924
* update the page, and `onPageSelected` to monitor page changes
@@ -87,6 +82,7 @@ export interface PagerViewProps extends ReactNative.ViewProps {
8782
*/
8883
pageMargin?: number;
8984

85+
style?: ReactNative.StyleProp<ReactNative.ViewStyle>;
9086
/**
9187
* Set the number of pages that should be retained to either side
9288
* of the currently visible page(s). Pages beyond this limit will

0 commit comments

Comments
 (0)