Skip to content
This repository was archived by the owner on Feb 25, 2020. It is now read-only.

Commit bed78ce

Browse files
authored
fix: fix typescript definitions (#165)
1 parent 4c5c0ac commit bed78ce

File tree

12 files changed

+143
-136
lines changed

12 files changed

+143
-136
lines changed

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"expo-asset": "^6.0.0",
1515
"expo-constants": "~5.0.1",
1616
"react": "16.8.3",
17-
"react-navigation": "^4.0.1",
17+
"react-navigation": "^4.0.3",
1818
"react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
1919
"react-native-safe-area-view": "0.13.1",
2020
"react-native-screens": "1.0.0-alpha.22",

example/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4594,10 +4594,10 @@ react-navigation-stack@1.5.3:
45944594
dependencies:
45954595
prop-types "^15.7.2"
45964596

4597-
react-navigation@^4.0.1:
4598-
version "4.0.1"
4599-
resolved "https://registry.yarnpkg.com/react-navigation/-/react-navigation-4.0.1.tgz#96c17ac90afcf0a5bc957358152326211a79d183"
4600-
integrity sha512-6XzuqvhOnY6FA6tCErD6+vfZdnP+O/7hCQper9qDulxxW2ZVkCF4xWdzoVcv3DDR6P5CK6l1tcbJ1ku256AdFQ==
4597+
react-navigation@^4.0.3:
4598+
version "4.0.3"
4599+
resolved "https://registry.yarnpkg.com/react-navigation/-/react-navigation-4.0.3.tgz#ba2cacb71db56e22ee50d774829ebc7fa95a0724"
4600+
integrity sha512-oASR5gHwd6se1Mw8AM4Ie8GicD5mKzRiYP6oaQujiQroQzQPij9sXxkRSqOscd/Kw1/Hf3htvBX3ZRPbOkWsfA==
46014601
dependencies:
46024602
"@react-navigation/core" "^3.5.0"
46034603
"@react-navigation/native" "^3.6.2"

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"test": "jest",
1515
"typescript": "tsc --noEmit",
1616
"lint": "eslint --ext .js,.ts,.tsx .",
17-
"bootstrap": "yarn && yarn --cwd example",
17+
"bootstrap": "yarn --cwd example && yarn",
1818
"example": "yarn --cwd example",
1919
"release": "yarn release-it",
2020
"prepare": "bob build"
@@ -74,14 +74,14 @@
7474
"react-native": "~0.57.1",
7575
"react-native-gesture-handler": "^1.4.1",
7676
"react-native-reanimated": "^1.2.0",
77-
"react-navigation": "^4.0.1",
77+
"react-navigation": "^4.0.3",
7878
"react-test-renderer": "16.5.0",
7979
"release-it": "^10.3.1",
8080
"typescript": "^3.5.2"
8181
},
8282
"peerDependencies": {
8383
"react": "*",
84-
"react-navigation": "^4.0.1",
84+
"react-navigation": "^4.0.3",
8585
"react-native": "*",
8686
"react-native-gesture-handler": "^1.0.0",
8787
"react-native-reanimated": "^1.0.0-alpha",

src/index.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ export { default as MaterialTopTabBar } from './views/MaterialTopTabBar';
1818
* Utils
1919
*/
2020
export { default as createTabNavigator } from './utils/createTabNavigator';
21+
22+
/**
23+
* Types
24+
*/
25+
26+
export {
27+
NavigationTabState,
28+
NavigationTabProp,
29+
NavigationBottomTabOptions,
30+
NavigationMaterialTabOptions,
31+
} from './types';

src/navigators/createBottomTabNavigator.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
AccessibilityRole,
66
AccessibilityState,
77
} from 'react-native';
8+
import { NavigationRoute } from 'react-navigation';
89

910
// eslint-disable-next-line import/no-unresolved
1011
import { ScreenContainer } from 'react-native-screens';
@@ -15,26 +16,25 @@ import createTabNavigator, {
1516
import BottomTabBar from '../views/BottomTabBar';
1617
import ResourceSavingScene from '../views/ResourceSavingScene';
1718
import {
18-
NavigationProp,
19-
Route,
20-
SceneDescriptor,
19+
NavigationTabProp,
2120
NavigationBottomTabOptions,
2221
BottomTabBarOptions,
22+
SceneDescriptorMap,
2323
} from '../types';
2424

2525
type Props = NavigationViewProps & {
2626
getAccessibilityRole: (props: {
27-
route: Route;
27+
route: NavigationRoute;
2828
}) => AccessibilityRole | undefined;
2929
getAccessibilityStates: (props: {
30-
route: Route;
30+
route: NavigationRoute;
3131
focused: boolean;
3232
}) => AccessibilityState[];
3333
lazy?: boolean;
3434
tabBarComponent?: React.ComponentType<any>;
3535
tabBarOptions?: BottomTabBarOptions;
36-
navigation: NavigationProp;
37-
descriptors: { [key: string]: SceneDescriptor<NavigationBottomTabOptions> };
36+
navigation: NavigationTabProp;
37+
descriptors: SceneDescriptorMap;
3838
screenProps?: unknown;
3939
};
4040

@@ -68,7 +68,7 @@ class TabNavigationView extends React.PureComponent<Props, State> {
6868
loaded: [this.props.navigation.state.index],
6969
};
7070

71-
_getButtonComponent = ({ route }: { route: Route }) => {
71+
_getButtonComponent = ({ route }: { route: NavigationRoute }) => {
7272
const { descriptors } = this.props;
7373
const descriptor = descriptors[route.key];
7474
const options = descriptor.options;

src/navigators/createMaterialTopTabNavigator.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import createTabNavigator, {
66
} from '../utils/createTabNavigator';
77
import MaterialTopTabBar from '../views/MaterialTopTabBar';
88
import {
9-
NavigationProp,
10-
SceneDescriptor,
9+
NavigationTabProp,
1110
NavigationMaterialTabOptions,
1211
MaterialTabBarOptions,
12+
SceneDescriptorMap,
1313
} from '../types';
1414

1515
type Route = {
@@ -32,8 +32,8 @@ type Props = NavigationViewProps & {
3232
tabBarPosition?: 'top' | 'bottom';
3333
sceneContainerStyle?: StyleProp<ViewStyle>;
3434
style?: StyleProp<ViewStyle>;
35-
navigation: NavigationProp;
36-
descriptors: { [key: string]: SceneDescriptor<NavigationMaterialTabOptions> };
35+
navigation: NavigationTabProp;
36+
descriptors: SceneDescriptorMap;
3737
screenProps?: unknown;
3838
};
3939

@@ -119,9 +119,10 @@ class MaterialTabView extends React.PureComponent<Props> {
119119
const options = descriptor.options;
120120

121121
let swipeEnabled =
122+
// @ts-ignore
122123
options.swipeEnabled == null
123124
? this.props.swipeEnabled
124-
: options.swipeEnabled;
125+
: (options as any).swipeEnabled;
125126

126127
if (typeof swipeEnabled === 'function') {
127128
swipeEnabled = swipeEnabled(state);

src/types.tsx

Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,21 @@ import {
88
} from 'react-native';
99
import SafeAreaView from 'react-native-safe-area-view';
1010
import Animated from 'react-native-reanimated';
11-
12-
export type Route = {
13-
key: string;
14-
routeName: string;
15-
} & (NavigationState | undefined);
16-
17-
export type NavigationEventName =
18-
| 'willFocus'
19-
| 'didFocus'
20-
| 'willBlur'
21-
| 'didBlur';
22-
23-
export type NavigationState = {
24-
key: string;
25-
index: number;
26-
routes: Route[];
27-
isTransitioning?: boolean;
28-
params?: { [key: string]: unknown };
29-
};
30-
31-
export type NavigationProp<RouteName = string, Params = object> = {
32-
emit(eventName: string): void;
33-
navigate(routeName: RouteName): void;
34-
goBack(): void;
35-
goBack(key: string | null): void;
36-
addListener: (
37-
event: NavigationEventName,
38-
callback: () => void
39-
) => { remove: () => void };
40-
isFocused(): boolean;
41-
state: NavigationState;
42-
setParams(params: Params): void;
43-
getParam(): Params;
44-
dispatch(action: { type: string }): boolean;
45-
dangerouslyGetParent(): NavigationProp | undefined;
11+
import {
12+
NavigationRoute,
13+
NavigationState,
14+
NavigationScreenProp,
15+
NavigationParams,
16+
NavigationDescriptor,
17+
} from 'react-navigation';
18+
19+
export type NavigationTabState = NavigationState;
20+
21+
export type NavigationTabProp<
22+
State = NavigationRoute,
23+
Params = NavigationParams
24+
> = NavigationScreenProp<State, Params> & {
25+
jumpTo(routeName: string, key?: string): void;
4626
};
4727

4828
export type ThemedColor =
@@ -75,22 +55,24 @@ export type BottomTabBarOptions = {
7555
};
7656

7757
export type BottomTabBarProps = BottomTabBarOptions & {
78-
navigation: NavigationProp;
79-
onTabPress: (props: { route: Route }) => void;
80-
onTabLongPress: (props: { route: Route }) => void;
81-
getAccessibilityLabel: (props: { route: Route }) => string | undefined;
58+
navigation: NavigationTabProp;
59+
onTabPress: (props: { route: NavigationRoute }) => void;
60+
onTabLongPress: (props: { route: NavigationRoute }) => void;
61+
getAccessibilityLabel: (props: {
62+
route: NavigationRoute;
63+
}) => string | undefined;
8264
getAccessibilityRole: (props: {
83-
route: Route;
65+
route: NavigationRoute;
8466
}) => AccessibilityRole | undefined;
8567
getAccessibilityStates: (props: {
86-
route: Route;
68+
route: NavigationRoute;
8769
focused: boolean;
8870
}) => AccessibilityState[];
8971
getButtonComponent: (props: {
90-
route: Route;
72+
route: NavigationRoute;
9173
}) => React.ComponentType<any> | undefined;
9274
getLabelText: (props: {
93-
route: Route;
75+
route: NavigationRoute;
9476
}) =>
9577
| ((scene: {
9678
focused: boolean;
@@ -99,9 +81,9 @@ export type BottomTabBarProps = BottomTabBarOptions & {
9981
}) => string | undefined)
10082
| string
10183
| undefined;
102-
getTestID: (props: { route: Route }) => string | undefined;
84+
getTestID: (props: { route: NavigationRoute }) => string | undefined;
10385
renderIcon: (props: {
104-
route: Route;
86+
route: NavigationRoute;
10587
focused: boolean;
10688
tintColor?: string;
10789
horizontal?: boolean;
@@ -140,26 +122,28 @@ export type MaterialTabBarProps = MaterialTabBarOptions & {
140122
position: Animated.Node<number>;
141123
jumpTo: (key: string) => void;
142124
getLabelText: (scene: {
143-
route: Route;
125+
route: NavigationRoute;
144126
}) =>
145127
| ((scene: { focused: boolean; tintColor: string }) => string | undefined)
146128
| string
147129
| undefined;
148-
getAccessible?: (scene: { route: Route }) => boolean | undefined;
149-
getAccessibilityLabel: (scene: { route: Route }) => string | undefined;
150-
getTestID: (scene: { route: Route }) => string | undefined;
130+
getAccessible?: (scene: { route: NavigationRoute }) => boolean | undefined;
131+
getAccessibilityLabel: (scene: {
132+
route: NavigationRoute;
133+
}) => string | undefined;
134+
getTestID: (scene: { route: NavigationRoute }) => string | undefined;
151135
renderIcon: (scene: {
152-
route: Route;
136+
route: NavigationRoute;
153137
focused: boolean;
154138
tintColor: string;
155139
horizontal?: boolean;
156140
}) => React.ReactNode;
157-
renderBadge?: (scene: { route: Route }) => React.ReactNode;
158-
onTabPress?: (scene: { route: Route }) => void;
159-
onTabLongPress?: (scene: { route: Route }) => void;
141+
renderBadge?: (scene: { route: NavigationRoute }) => React.ReactNode;
142+
onTabPress?: (scene: { route: NavigationRoute }) => void;
143+
onTabLongPress?: (scene: { route: NavigationRoute }) => void;
160144
tabBarPosition?: 'top' | 'bottom';
161145
screenProps: unknown;
162-
navigation: NavigationProp;
146+
navigation: NavigationTabProp;
163147
};
164148

165149
export type NavigationCommonTabOptions = {
@@ -176,11 +160,11 @@ export type NavigationCommonTabOptions = {
176160
horizontal?: boolean;
177161
}) => React.ReactNode);
178162
tabBarOnPress?: (props: {
179-
navigation: NavigationProp;
163+
navigation: NavigationTabProp;
180164
defaultHandler: () => void;
181165
}) => void;
182166
tabBarOnLongPress?: (props: {
183-
navigation: NavigationProp;
167+
navigation: NavigationTabProp;
184168
defaultHandler: () => void;
185169
}) => void;
186170
};
@@ -194,17 +178,10 @@ export type NavigationMaterialTabOptions = NavigationCommonTabOptions & {
194178
swipeEnabled?: boolean | ((state: NavigationState) => boolean);
195179
};
196180

197-
export type SceneDescriptor<Options extends NavigationCommonTabOptions> = {
198-
key: string;
199-
options: Options;
200-
navigation: NavigationProp;
201-
getComponent(): React.ComponentType;
202-
};
203-
204-
export type Screen<
205-
Options extends NavigationCommonTabOptions
206-
> = React.ComponentType<any> & {
207-
navigationOptions?: Options & {
208-
[key: string]: any;
209-
};
181+
export type SceneDescriptorMap = {
182+
[key: string]: NavigationDescriptor<
183+
NavigationParams,
184+
NavigationBottomTabOptions | NavigationMaterialTabOptions,
185+
NavigationTabProp
186+
>;
210187
};

0 commit comments

Comments
 (0)