|
1 | 1 | import * as React from 'react'; |
2 | 2 | import { View, Text, Button, StyleSheet } from 'react-native'; |
3 | 3 | import Stack, { SceneProps, Route } from './components/Stack'; |
4 | | -import Card from './components/Card'; |
5 | | -import { SlideFromRightIOS } from './TransitionConfigs/TransitionPresets'; |
6 | 4 |
|
7 | 5 | type CustomRoute = Route & { initial?: boolean }; |
8 | 6 |
|
9 | 7 | type State = { |
10 | 8 | routes: CustomRoute[]; |
11 | | - closing: string[]; |
| 9 | + initialRoutes: string[]; |
| 10 | + closingRoutes: string[]; |
12 | 11 | }; |
13 | 12 |
|
14 | 13 | export default class App extends React.Component<{}, State> { |
15 | 14 | state: State = { |
16 | | - routes: [{ key: '0', initial: true }, { key: '1', initial: true }], |
17 | | - closing: [], |
| 15 | + routes: [{ key: '0' }, { key: '1' }], |
| 16 | + initialRoutes: ['0', '1'], |
| 17 | + closingRoutes: [], |
18 | 18 | }; |
19 | 19 |
|
20 | 20 | private key = 2; |
21 | 21 |
|
22 | | - private renderScene = ( |
23 | | - { route, ...rest }: SceneProps<CustomRoute>, |
24 | | - index: number |
25 | | - ) => { |
| 22 | + private renderScene = ({ route, index }: SceneProps<CustomRoute>) => { |
26 | 23 | return ( |
27 | | - <Card |
28 | | - {...rest} |
29 | | - closing={this.state.closing.includes(route.key)} |
30 | | - onClose={() => |
31 | | - this.setState(state => ({ |
32 | | - routes: state.routes.filter(r => r !== route), |
33 | | - closing: state.closing.filter(key => key !== route.key), |
34 | | - })) |
35 | | - } |
36 | | - gesturesEnabled={index !== 0} |
37 | | - animationsEnabled={!route.initial} |
38 | | - {...SlideFromRightIOS} |
39 | | - > |
40 | | - <View style={styles.scene}> |
41 | | - <Text style={styles.item}>{index}</Text> |
| 24 | + <View style={styles.scene}> |
| 25 | + <Text style={styles.item}>{index}</Text> |
| 26 | + <View style={styles.item}> |
| 27 | + <Button |
| 28 | + title="Add screen" |
| 29 | + onPress={() => { |
| 30 | + this.setState(state => ({ |
| 31 | + routes: [...state.routes, { key: String(this.key++) }], |
| 32 | + })); |
| 33 | + }} |
| 34 | + /> |
| 35 | + </View> |
| 36 | + {index !== 0 ? ( |
42 | 37 | <View style={styles.item}> |
43 | 38 | <Button |
44 | | - title="Add screen" |
45 | | - onPress={() => { |
| 39 | + title="Go back" |
| 40 | + onPress={() => |
46 | 41 | this.setState(state => ({ |
47 | | - routes: [...state.routes, { key: String(this.key++) }], |
48 | | - })); |
49 | | - }} |
| 42 | + closingRoutes: [...state.closingRoutes, route.key], |
| 43 | + })) |
| 44 | + } |
50 | 45 | /> |
51 | 46 | </View> |
52 | | - {index !== 0 ? ( |
53 | | - <View style={styles.item}> |
54 | | - <Button |
55 | | - title="Go back" |
56 | | - onPress={() => |
57 | | - this.setState(state => ({ |
58 | | - closing: [...state.closing, route.key], |
59 | | - })) |
60 | | - } |
61 | | - /> |
62 | | - </View> |
63 | | - ) : null} |
64 | | - </View> |
65 | | - </Card> |
| 47 | + ) : null} |
| 48 | + </View> |
66 | 49 | ); |
67 | 50 | }; |
68 | 51 |
|
69 | 52 | render() { |
70 | | - return <Stack routes={this.state.routes} renderScene={this.renderScene} />; |
| 53 | + return ( |
| 54 | + <Stack |
| 55 | + routes={this.state.routes} |
| 56 | + initialRoutes={this.state.initialRoutes} |
| 57 | + closingRoutes={this.state.closingRoutes} |
| 58 | + onGoBack={({ route }) => |
| 59 | + this.setState(state => ({ |
| 60 | + closingRoutes: [...state.closingRoutes, route.key], |
| 61 | + })) |
| 62 | + } |
| 63 | + onCloseRoute={({ route }) => |
| 64 | + this.setState(state => ({ |
| 65 | + routes: state.routes.filter(r => r !== route), |
| 66 | + closingRoutes: state.closingRoutes.filter(key => key !== route.key), |
| 67 | + initialRoutes: state.initialRoutes.filter(key => key !== route.key), |
| 68 | + })) |
| 69 | + } |
| 70 | + renderScene={this.renderScene} |
| 71 | + /> |
| 72 | + ); |
71 | 73 | } |
72 | 74 | } |
73 | 75 |
|
|
0 commit comments