-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathNavigator.tsx
More file actions
125 lines (107 loc) · 2.84 KB
/
Navigator.tsx
File metadata and controls
125 lines (107 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React, { useEffect, useState } from 'react';
import { View, Text, Pressable, BackHandler } from 'react-native';
export interface RouteInfo {
component: React.ComponentType;
title?: string;
rightButtonAction?: () => void;
}
export type NavigatorRoutes = Record<string, RouteInfo>;
export interface NavigatorProps {
initialRouteName: string;
}
export interface ButtonProps {
title: string;
onPress: () => void;
}
const Button = (props: ButtonProps) => {
return (
<Pressable
style={{
width: 48,
height: 48,
justifyContent: 'center',
}}
onPress={() => {
props.onPress();
}}>
<Text style={{ textAlign: 'center', fontSize: 24 }}>{props.title}</Text>
</Pressable>
);
};
export default class Navigator {
private routes: NavigatorRoutes = {};
private history: string[] = [];
private setCurrentRoute!: (route: string) => void;
static create() {
return new Navigator();
}
setRoutes(routes: NavigatorRoutes) {
this.routes = routes;
}
navigateTo(route: string) {
this.history.push(route);
this.setCurrentRoute(route);
}
goBack() {
if (this.history.length === 1) {
throw new Error("Can't go back, no history");
}
this.history.pop();
this.setCurrentRoute(this.history[this.history.length - 1]);
}
canGoBack() {
return this.history.length > 1;
}
backHandler = () => {
if (this.canGoBack()) {
this.goBack();
return true;
}
return false;
};
Navigator = (props: NavigatorProps) => {
const [currentRoute, setCurrentRoute] = useState(props.initialRouteName);
this.setCurrentRoute = setCurrentRoute;
useEffect(() => {
// eslint-disable-next-line @typescript-eslint/unbound-method
return BackHandler.addEventListener('hardwareBackPress', this.backHandler)
.remove;
}, []);
useEffect(() => {
if (this.history.length === 0) {
this.history.push(currentRoute);
}
}, [currentRoute]);
const route = this.routes[currentRoute];
return (
<View style={{ flex: 1 }}>
<View
style={{
height: 48,
width: '100%',
flexDirection: 'row',
justifyContent: 'space-between',
borderBottomColor: '#ccc',
borderBottomWidth: 1,
}}>
<Button
title={this.canGoBack() ? '<' : ''}
onPress={() => {
this.canGoBack() ? this.goBack() : null;
}}
/>
<Text style={{ alignSelf: 'center', fontSize: 20 }}>
{route.title ?? ''}
</Text>
<Button
title={route.rightButtonAction ? '>' : ''}
onPress={() => {
route.rightButtonAction?.();
}}
/>
</View>
<route.component />
</View>
);
};
}