Skip to content

Commit b7a5feb

Browse files
committed
refactor: migrate code to typescript (facebook#59)
1 parent 5083831 commit b7a5feb

File tree

13 files changed

+206
-81
lines changed

13 files changed

+206
-81
lines changed

packages/drawer/.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/drawer/src/index.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

packages/drawer/src/index.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Navigators
3+
*/
4+
/**
5+
* Router
6+
*/
7+
import * as DrawerAcions from './routers/DrawerActions';
8+
9+
export {
10+
default as createDrawerNavigator,
11+
} from './navigators/createDrawerNavigator';
12+
13+
export { DrawerAcions };
14+
export { default as DrawerRouter } from './routers/DrawerRouter';
15+
16+
/**
17+
* Views
18+
*/
19+
export { default as DrawerNavigatorItems } from './views/DrawerNavigatorItems';
20+
export { default as DrawerSidebar } from './views/DrawerSidebar';
21+
export { default as DrawerView } from './views/DrawerView';
22+
23+
export { default as DrawerGestureContext } from './utils/DrawerGestureContext';

packages/drawer/src/navigators/createDrawerNavigator.js renamed to packages/drawer/src/navigators/createDrawerNavigator.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import React from 'react';
1+
import * as React from 'react';
22
import { Dimensions, Platform, ScrollView } from 'react-native';
33
import { createNavigator } from '@react-navigation/core';
44
import { SafeAreaView } from '@react-navigation/native';
55
import DrawerRouter from '../routers/DrawerRouter';
66
import DrawerView from '../views/DrawerView';
7-
import DrawerItems from '../views/DrawerNavigatorItems';
7+
import DrawerItems, { Props } from '../views/DrawerNavigatorItems';
88

99
// A stack navigators props are the intersection between
1010
// the base navigator props (navgiation, screenProps, etc)
1111
// and the view's props
1212

13-
const defaultContentComponent = props => (
13+
const defaultContentComponent = (props: Props) => (
1414
<ScrollView alwaysBounceVertical={false}>
1515
<SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
1616
<DrawerItems {...props} />
@@ -45,7 +45,7 @@ const DefaultDrawerConfig = {
4545
overlayColor: 'black',
4646
};
4747

48-
const DrawerNavigator = (routeConfigs, config = {}) => {
48+
const DrawerNavigator = (routeConfigs: object, config: any = {}) => {
4949
const mergedConfig = { ...DefaultDrawerConfig, ...config };
5050
const drawerRouter = DrawerRouter(routeConfigs, mergedConfig);
5151
const navigator = createNavigator(DrawerView, drawerRouter, mergedConfig);

packages/drawer/src/types.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { DrawerActionType } from './routers/DrawerActions';
2+
3+
export type Route = {
4+
key: string;
5+
routeName: string;
6+
};
7+
8+
export type Scene = {
9+
route: Route;
10+
index: number;
11+
focused: boolean;
12+
tintColor?: string;
13+
};
14+
15+
export type Navigation = {
16+
state: {
17+
key: string;
18+
index: number;
19+
routes: Route[];
20+
openId: string;
21+
closeId: string;
22+
toggleId: string;
23+
isDrawerIdle: boolean;
24+
isDrawerOpen: boolean;
25+
};
26+
openDrawer: () => void;
27+
closeDrawer: () => void;
28+
dispatch: (action: {
29+
type: DrawerActionType;
30+
key: string;
31+
willShow?: boolean;
32+
}) => void;
33+
};

packages/drawer/src/utils/DrawerGestureContext.js

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as React from 'react';
2+
3+
export default React.createContext<React.RefObject<any> | null>(null);

packages/drawer/src/utils/invariant.js renamed to packages/drawer/src/utils/invariant.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
* will remain to ensure logic does not differ in production.
2121
*/
2222

23-
export default function invariant(condition, format, a, b, c, d, e, f) {
23+
export default function invariant(
24+
condition: any,
25+
format: string,
26+
...args: any[]
27+
) {
2428
if (format === undefined) {
2529
throw new Error('invariant requires an error message argument');
2630
}
@@ -32,12 +36,12 @@ export default function invariant(condition, format, a, b, c, d, e, f) {
3236
'Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.'
3337
);
3438
} else {
35-
var args = [a, b, c, d, e, f];
3639
var argIndex = 0;
3740
error = new Error(format.replace(/%s/g, () => args[argIndex++]));
3841
error.name = 'Invariant Violation';
3942
}
4043

44+
// @ts-ignore
4145
error.framesToPop = 1; // we don't care about invariant's own frame
4246
throw error;
4347
}

packages/drawer/src/views/DrawerNavigatorItems.js renamed to packages/drawer/src/views/DrawerNavigatorItems.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1-
import React from 'react';
2-
import { View, Text, StyleSheet } from 'react-native';
1+
import * as React from 'react';
2+
import { View, Text, StyleSheet, ViewStyle, TextStyle } from 'react-native';
33
import { SafeAreaView } from '@react-navigation/native';
44
import TouchableItem from './TouchableItem';
5+
import { Scene, Route } from '../types';
6+
7+
export type Props = {
8+
items: Route[];
9+
activeItemKey?: string | null;
10+
activeTintColor?: string;
11+
activeBackgroundColor?: string;
12+
inactiveTintColor?: string;
13+
inactiveBackgroundColor?: string;
14+
getLabel: (scene: Scene) => React.ReactNode;
15+
renderIcon: (scene: Scene) => React.ReactNode;
16+
onItemPress: (scene: { route: Route; focused: boolean }) => void;
17+
itemsContainerStyle?: ViewStyle;
18+
itemStyle?: ViewStyle;
19+
labelStyle?: TextStyle;
20+
activeLabelStyle?: TextStyle;
21+
inactiveLabelStyle?: TextStyle;
22+
iconContainerStyle?: ViewStyle;
23+
drawerPosition: 'left' | 'right';
24+
};
525

626
/**
727
* Component that renders the navigation list in the drawer.
@@ -23,9 +43,9 @@ const DrawerNavigatorItems = ({
2343
inactiveLabelStyle,
2444
iconContainerStyle,
2545
drawerPosition,
26-
}) => (
46+
}: Props) => (
2747
<View style={[styles.container, itemsContainerStyle]}>
28-
{items.map((route, index) => {
48+
{items.map((route, index: number) => {
2949
const focused = activeItemKey === route.key;
3050
const color = focused ? activeTintColor : inactiveTintColor;
3151
const backgroundColor = focused

packages/drawer/src/views/DrawerSidebar.js renamed to packages/drawer/src/views/DrawerSidebar.tsx

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
import React from 'react';
2-
import { StyleSheet, View } from 'react-native';
3-
1+
import * as React from 'react';
2+
import { StyleSheet, View, Animated, ViewStyle } from 'react-native';
43
import { NavigationActions } from '@react-navigation/core';
4+
5+
import { Props as DrawerNavigatorItemsProps } from './DrawerNavigatorItems';
56
import invariant from '../utils/invariant';
7+
import { Navigation, Scene, Route } from '../types';
8+
9+
export type ContentComponentProps = DrawerNavigatorItemsProps & {
10+
navigation: Navigation;
11+
descriptors: { [key: string]: any };
12+
drawerOpenProgress: Animated.AnimatedInterpolation;
13+
screenProps: unknown;
14+
};
15+
16+
type Props = {
17+
contentComponent?: React.ComponentType<ContentComponentProps>;
18+
contentOptions?: object;
19+
screenProps?: unknown;
20+
navigation: Navigation;
21+
descriptors: { [key: string]: any };
22+
drawerOpenProgress: Animated.AnimatedInterpolation;
23+
drawerPosition: 'left' | 'right';
24+
style?: ViewStyle;
25+
};
626

727
/**
828
* Component that renders the sidebar screen of the drawer.
929
*/
10-
11-
class DrawerSidebar extends React.PureComponent {
12-
_getScreenOptions = routeKey => {
30+
class DrawerSidebar extends React.PureComponent<Props> {
31+
_getScreenOptions = (routeKey: string) => {
1332
const descriptor = this.props.descriptors[routeKey];
1433
invariant(
1534
descriptor.options,
@@ -18,7 +37,7 @@ class DrawerSidebar extends React.PureComponent {
1837
return descriptor.options;
1938
};
2039

21-
_getLabel = ({ focused, tintColor, route }) => {
40+
_getLabel = ({ focused, tintColor, route }: Scene) => {
2241
const { drawerLabel, title } = this._getScreenOptions(route.key);
2342
if (drawerLabel) {
2443
return typeof drawerLabel === 'function'
@@ -33,7 +52,7 @@ class DrawerSidebar extends React.PureComponent {
3352
return route.routeName;
3453
};
3554

36-
_renderIcon = ({ focused, tintColor, route }) => {
55+
_renderIcon = ({ focused, tintColor, route }: Scene) => {
3756
const { drawerIcon } = this._getScreenOptions(route.key);
3857
if (drawerIcon) {
3958
return typeof drawerIcon === 'function'
@@ -43,7 +62,7 @@ class DrawerSidebar extends React.PureComponent {
4362
return null;
4463
};
4564

46-
_onItemPress = ({ route, focused }) => {
65+
_onItemPress = ({ route, focused }: { route: Route; focused: boolean }) => {
4766
if (focused) {
4867
this.props.navigation.closeDrawer();
4968
} else {
@@ -55,11 +74,15 @@ class DrawerSidebar extends React.PureComponent {
5574

5675
render() {
5776
const ContentComponent = this.props.contentComponent;
77+
5878
if (!ContentComponent) {
5979
return null;
6080
}
81+
6182
const { state } = this.props.navigation;
83+
6284
invariant(typeof state.index === 'number', 'should be set');
85+
6386
return (
6487
<View style={[styles.container, this.props.style]}>
6588
<ContentComponent

0 commit comments

Comments
 (0)