Skip to content

Commit f5db986

Browse files
PTROCKIferrannp
authored andcommitted
fix: Flow types and extract them
1 parent b54aa36 commit f5db986

File tree

11 files changed

+361
-407
lines changed

11 files changed

+361
-407
lines changed

example/ViewPagerExample.js

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,46 @@
99

1010
'use strict';
1111

12-
import React from 'react';
12+
import * as React from "react";
1313
import { Image,
1414
StyleSheet,
1515
Text,
16-
TouchableWithoutFeedback,
17-
TouchableOpacity,
18-
View,
16+
View,
1917
SafeAreaView,
20-
Platform } from 'react-native'
18+
} from 'react-native'
2119

2220
import ViewPagerAndroid from '@react-native-community/viewpager';
23-
import { PAGES, BGCOLOR, IMAGE_URIS, createPage } from "./Common";
21+
import { PAGES, BGCOLOR, IMAGE_URIS, createPage } from "./utils";
2422
import { Button } from "./src/component/Button";
2523
import { LikeCount } from "./src/component/LikeCount";
2624
import { ProgressBar } from "./src/component/ProgressBar";
25+
import type { CreatePage } from "./utils"
26+
import type {PageScrollEvent, PageScrollState, PageScrollStateChangedEvent, PageSelectedEvent} from "../js";
2727

28-
export default class ViewPagerExample extends React.Component {
29-
constructor(props) {
28+
type State = {
29+
page: number,
30+
animationsAreEnabled: boolean,
31+
scrollEnabled: boolean,
32+
progress: {
33+
position: number,
34+
offset: number,
35+
},
36+
pages: Array<CreatePage>,
37+
scrollState: PageScrollState
38+
};
39+
40+
export default class ViewPagerExample extends React.Component<*, State> {
41+
42+
viewPager: React.Ref<typeof ViewPagerAndroid>;
43+
44+
constructor(props: any) {
3045
super(props);
3146

3247
const pages = [];
3348
for (let i = 0; i < PAGES; i++) {
34-
pages.push(this.createPage(i));
49+
pages.push(createPage(i));
3550
}
36-
51+
3752
this.state = {
3853
page: 0,
3954
animationsAreEnabled: true,
@@ -42,52 +57,47 @@ export default class ViewPagerExample extends React.Component {
4257
position: 0,
4358
offset: 0,
4459
},
45-
pages
60+
pages: pages,
61+
scrollState: 'idle'
4662
};
63+
this.viewPager = React.createRef();
4764
};
4865

49-
onPageSelected = e => {
66+
onPageSelected = (e: PageSelectedEvent) => {
5067
this.setState({page: e.nativeEvent.position});
5168
};
5269

53-
onPageScroll = e => {
54-
this.setState({progress: e.nativeEvent});
70+
onPageScroll = (e: PageScrollEvent)=> {
71+
this.setState({progress: {
72+
position: e.nativeEvent.position,
73+
offset: e.nativeEvent.offset,
74+
}});
5575
};
5676

57-
onPageScrollStateChanged = e => {
77+
onPageScrollStateChanged = (e: PageScrollStateChangedEvent) => {
5878
this.setState({scrollState: e.nativeEvent.pageScrollState});
5979
};
6080

61-
addPage = e => {
62-
this.setState(prevState => ({ pages: [...prevState.pages, this.createPage(prevState.pages.length)]}));
63-
}
81+
addPage = () => {
82+
this.setState(prevState => ({ pages: [...prevState.pages, createPage(prevState.pages.length)]}));
83+
};
6484

65-
move = delta => {
85+
move = (delta: number) => {
6686
const page = this.state.page + delta;
6787
this.go(page);
6888
};
6989

70-
go = page => {
90+
go = (page: number) => {
7191
if (this.state.animationsAreEnabled) {
72-
this.viewPager.setPage(page);
92+
/* $FlowFixMe we need to update flow to support React.Ref and createRef() */
93+
this.viewPager.current.setPage(page);
7394
} else {
74-
this.viewPager.setPageWithoutAnimation(page);
95+
/* $FlowFixMe we need to update flow to support React.Ref and createRef() */
96+
this.viewPager.current.setPageWithoutAnimation(page);
7597
}
7698
};
7799

78-
createPage(key) {
79-
return {
80-
key: key,
81-
style: {
82-
backgroundColor: BGCOLOR[key % BGCOLOR.length],
83-
alignItems: 'center',
84-
padding: 20,
85-
},
86-
imgSource: { uri: IMAGE_URIS[key % BGCOLOR.length] }
87-
}
88-
};
89-
90-
renderPage(page) {
100+
renderPage(page: CreatePage) {
91101
return (
92102
<View key={page.key} style={page.style} collapsable={false}>
93103
<Image
@@ -111,9 +121,7 @@ export default class ViewPagerExample extends React.Component {
111121
onPageSelected={this.onPageSelected}
112122
onPageScrollStateChanged={this.onPageScrollStateChanged}
113123
pageMargin={10}
114-
ref={viewPager => {
115-
this.viewPager = viewPager;
116-
}}>
124+
ref={this.viewPager}>
117125
{ pages.map( page => this.renderPage(page)) }
118126
</ViewPagerAndroid>
119127
<View style={styles.buttons}>
@@ -166,7 +174,7 @@ export default class ViewPagerExample extends React.Component {
166174
</View>
167175
<View style={styles.progress}>
168176
<Text style={styles.buttonText}> Page {page + 1} / {pages.length} </Text>
169-
<ProgressBar numberOfPages={pages.length} size={300} progress={this.state.progress} />
177+
<ProgressBar numberOfPages={pages.length} size={300} progress={this.state.progress} />
170178
</View>
171179
</SafeAreaView>
172180
);

example/src/component/Button.js

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,63 @@
1-
// @flow
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
29

310
import React from 'react';
411
import {
5-
Image,
612
StyleSheet,
713
Text,
814
TouchableWithoutFeedback,
9-
TouchableOpacity,
1015
View,
1116
} from 'react-native'
1217

13-
export class Button extends React.Component {
14-
18+
type Props = $ReadOnly<{|
19+
enabled?: boolean,
20+
onPress: () => void,
21+
text: string,
22+
|}>;
23+
24+
export class Button extends React.Component<Props> {
1525
_handlePress = () => {
16-
if (this.props.enabled && this.props.onPress) {
17-
this.props.onPress();
18-
}
19-
};
20-
21-
render() {
22-
return (
23-
<TouchableWithoutFeedback onPress={this._handlePress}>
24-
<View
25-
style={[
26-
styles.button,
27-
this.props.enabled ? {} : styles.buttonDisabled,
28-
]}>
29-
<Text style={styles.buttonText}>{this.props.text}</Text>
30-
</View>
31-
</TouchableWithoutFeedback>
32-
);
26+
if (this.props.enabled && this.props.onPress) {
27+
this.props.onPress();
3328
}
29+
};
30+
31+
render() {
32+
return (
33+
<TouchableWithoutFeedback onPress={this._handlePress}>
34+
<View
35+
style={[
36+
styles.button,
37+
this.props.enabled ? {} : styles.buttonDisabled,
38+
]}>
39+
<Text style={styles.buttonText}>{this.props.text}</Text>
40+
</View>
41+
</TouchableWithoutFeedback>
42+
);
3443
}
44+
}
3545

36-
const styles = StyleSheet.create({
37-
button: {
38-
flex: 1,
39-
width: 0,
40-
margin: 5,
41-
borderColor: 'gray',
42-
borderWidth: 1,
43-
backgroundColor: 'gray',
44-
},
45-
buttonDisabled: {
46-
backgroundColor: 'black',
47-
opacity: 0.5,
48-
},
49-
buttonText: {
50-
color: 'white',
51-
margin: 10,
52-
},
53-
})
46+
const styles = StyleSheet.create({
47+
button: {
48+
flex: 1,
49+
width: 0,
50+
margin: 5,
51+
borderColor: 'gray',
52+
borderWidth: 1,
53+
backgroundColor: 'gray',
54+
},
55+
buttonDisabled: {
56+
backgroundColor: 'black',
57+
opacity: 0.5,
58+
},
59+
buttonText: {
60+
color: 'white',
61+
margin: 10,
62+
},
63+
});

example/src/component/LikeCount.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
// @flow
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
29

310
import React from 'react';
4-
import { Image,
11+
import {
512
StyleSheet,
613
Text,
7-
TouchableWithoutFeedback,
814
TouchableOpacity,
9-
View } from 'react-native'
15+
View,
16+
} from 'react-native'
1017

11-
import { thumbsUp } from './../../Common'
18+
import { thumbsUp } from '../../utils'
1219

1320
type Props = $ReadOnly<{||}>;
1421
type State = {|likes: number|};
@@ -52,4 +59,4 @@ const styles = StyleSheet.create({
5259
flex: 1,
5360
margin: 8,
5461
},
55-
});
62+
});
Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1-
// @flow
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
210
import React from 'react';
3-
import { Image,
4-
StyleSheet,
5-
Text,
6-
TouchableWithoutFeedback,
7-
TouchableOpacity,
8-
View } from 'react-native'
11+
import {StyleSheet, View} from 'react-native'
12+
13+
type Props = {
14+
progress: {
15+
position: number,
16+
offset: number,
17+
},
18+
numberOfPages: number,
19+
size: number,
20+
}
921

10-
export class ProgressBar extends React.Component {
11-
12-
render() {
13-
const fractionalPosition = this.props.progress.position + this.props.progress.offset;
14-
const progressBarSize = (fractionalPosition / (this.props.numberOfPages - 1)) * this.props.size;
15-
return (
16-
<View style={[styles.progressBarContainer, {width: this.props.size}]}>
17-
<View style={[styles.progressBar, {width: progressBarSize}]} />
18-
</View>
22+
export class ProgressBar extends React.Component<Props> {
23+
render() {
24+
const fractionalPosition = this.props.progress.position + this.props.progress.offset;
25+
const progressBarSize = (fractionalPosition / (this.props.numberOfPages - 1)) * this.props.size;
26+
return (
27+
<View style={[styles.progressBarContainer, {width: this.props.size}]}>
28+
<View style={[styles.progressBar, {width: progressBarSize}]} />
29+
</View>
1930
);
2031
}
2132
}
@@ -32,4 +43,4 @@ const styles = StyleSheet.create({
3243
flex: 1,
3344
backgroundColor: '#eeeeee',
3445
},
35-
});
46+
});
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
// @flow
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
29

310
export const PAGES = 5;
411
export const BGCOLOR = ['#fdc08e', '#fff6b9', '#99d1b7', '#dde5fe', '#f79273'];
@@ -10,15 +17,22 @@ export const IMAGE_URIS = [
1017
'https://apod.nasa.gov/apod/image/1510/lunareclipse_27Sep_beletskycrop4.jpg',
1118
];
1219
export const thumbsUp = '\uD83D\uDC4D';
20+
import type { ViewStyleProp } from 'react-native/Libraries/StyleSheet/StyleSheet';
1321

14-
export const createPage = (key) => {
22+
export type CreatePage = {
23+
key: number,
24+
style: ViewStyleProp,
25+
imgSource: { uri: string }
26+
}
27+
28+
export const createPage = (key: number): CreatePage => {
1529
return {
1630
key: key,
1731
style: {
1832
backgroundColor: BGCOLOR[key % BGCOLOR.length],
1933
alignItems: 'center',
2034
padding: 20,
21-
},
35+
},
2236
imgSource: { uri: IMAGE_URIS[key % BGCOLOR.length] }
2337
}
24-
};
38+
};

0 commit comments

Comments
 (0)