react-native-navigation的迁移库

PushedScreen.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import React, {Component, PropTypes} from 'react';
  2. import {
  3. Text,
  4. View,
  5. ScrollView,
  6. TouchableOpacity,
  7. StyleSheet,
  8. TextInput,
  9. Alert
  10. } from 'react-native';
  11. import {connect} from 'react-redux';
  12. import * as counterActions from '../reducers/counter/actions';
  13. const leftButtons = ['back', 'cancel', 'accept', 'sideMenu'];
  14. let bottomTabsVisible = true;
  15. // this is a traditional React component connected to the redux store
  16. class PushedScreen extends Component {
  17. static navigatorStyle = {
  18. statusBarColor: '#303F9F',
  19. toolBarColor: '#3F51B5',
  20. navigationBarColor: '#303F9F',
  21. tabSelectedTextColor: '#FFA000',
  22. tabNormalTextColor: '#FFC107',
  23. tabIndicatorColor: '#FF4081'
  24. };
  25. static navigatorButtons = {
  26. leftButton: {
  27. id: 'back',
  28. color: '#00ff00'
  29. }
  30. };
  31. constructor(props) {
  32. super(props);
  33. this.bgColor = this.getRandomColor();
  34. console.log(`constructor ${this.bgColor}`);
  35. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  36. this.currentBackButton = 0;
  37. }
  38. componentWillUnmount() {
  39. console.log(`componentWillUnmount ${this.bgColor}`);
  40. }
  41. getRandomColor() {
  42. var letters = '0123456789ABCDEF'.split('');
  43. var color = '#';
  44. for (var i = 0; i < 6; i++) {
  45. color += letters[Math.floor(Math.random() * 16)];
  46. }
  47. return color;
  48. }
  49. onNavigatorEvent(event) {
  50. switch (event.id) {
  51. case 'cancel':
  52. Alert.alert('NavBar', 'Cancel button pressed');
  53. break;
  54. case 'accept':
  55. Alert.alert('NavBar', 'Accept button pressed');
  56. break;
  57. }
  58. }
  59. render() {
  60. return (
  61. <ScrollView style={{flex: 1, padding: 20, backgroundColor: this.bgColor}}>
  62. <Text style={styles.text}>
  63. <Text style={{fontWeight: '500'}}>Counter: </Text> {this.props.counter.count}
  64. </Text>
  65. <TouchableOpacity onPress={ this.onIncrementPress.bind(this) }>
  66. <Text style={styles.button}>Increment Counter</Text>
  67. </TouchableOpacity>
  68. <TouchableOpacity onPress={ this.onPushPress.bind(this) }>
  69. <Text style={styles.button}>Push Another</Text>
  70. </TouchableOpacity>
  71. <TouchableOpacity onPress={ this.onPopPress.bind(this) }>
  72. <Text style={styles.button}>Pop Screen</Text>
  73. </TouchableOpacity>
  74. <TouchableOpacity onPress={ this.onShowModalPress.bind(this) }>
  75. <Text style={styles.button}>Modal Screen</Text>
  76. </TouchableOpacity>
  77. <TouchableOpacity onPress={ this.onDismissModal.bind(this) }>
  78. <Text style={styles.button}>Dismiss modal</Text>
  79. </TouchableOpacity>
  80. <TouchableOpacity onPress={ this.onDismissAllModalsPress.bind(this) }>
  81. <Text style={styles.button}>Dismiss all modals</Text>
  82. </TouchableOpacity>
  83. <TouchableOpacity onPress={ this.onPopToRootPress.bind(this) }>
  84. <Text style={styles.button}>Pop to root</Text>
  85. </TouchableOpacity>
  86. <TouchableOpacity onPress={ this.onNewStackPress.bind(this) }>
  87. <Text style={styles.button}>New Stack</Text>
  88. </TouchableOpacity>
  89. <TouchableOpacity onPress={ this.onChangeLeftButtonPress.bind(this) }>
  90. <Text style={styles.button}>Change Left Button</Text>
  91. </TouchableOpacity>
  92. <TouchableOpacity onPress={ this.onToggleBottomTabsPress.bind(this) }>
  93. <Text style={styles.button}>Toggle BottomTabs</Text>
  94. </TouchableOpacity>
  95. <TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1}}/>
  96. <Text style={{fontWeight: '500'}}>String prop: {this.props.str}</Text>
  97. <Text style={{fontWeight: '500'}}>Number prop: {this.props.num}</Text>
  98. {this.props.obj ? <Text style={{fontWeight: '500'}}>Object prop: {this.props.obj.str}</Text> : false}
  99. {this.props.obj && this.props.obj.arr ? <Text style={{fontWeight: '500'}}>Array prop: {this.props.obj.arr[0].str}</Text> : false}
  100. </ScrollView>
  101. );
  102. }
  103. onIncrementPress() {
  104. this.props.dispatch(counterActions.increment());
  105. }
  106. onPushPress() {
  107. this.props.navigator.push({
  108. title: "List",
  109. screen: "example.ListScreen",
  110. passProps: {
  111. passed: 'This is a prop passed in \'navigator.push()\'!',
  112. obj: {
  113. str: 'This is a prop passed in an object!',
  114. arr: [
  115. {
  116. str: 'This is a prop in an object in an array in an object!'
  117. }
  118. ]
  119. },
  120. num: 1234
  121. }
  122. });
  123. }
  124. onPopPress() {
  125. this.props.navigator.pop();
  126. }
  127. onShowModalPress() {
  128. this.props.navigator.showModal({
  129. title: "Modal Screen",
  130. screen: "example.PushedScreen",
  131. passProps: {
  132. passed: 'This is a prop passed in \'navigator.showModal()\'!',
  133. obj: {
  134. str: 'This is a prop passed in an object!',
  135. arr: [
  136. {
  137. str: 'This is a prop in an object in an array in an object!'
  138. }
  139. ]
  140. },
  141. num: 1234
  142. }
  143. });
  144. }
  145. onDismissAllModalsPress() {
  146. this.props.navigator.dismissAllModals();
  147. }
  148. onDismissModal() {
  149. this.props.navigator.dismissModal();
  150. }
  151. onPopToRootPress() {
  152. this.props.navigator.popToRoot();
  153. }
  154. onNewStackPress() {
  155. this.props.navigator.resetTo({
  156. title: "NEW STACK",
  157. screen: "example.PushedScreen",
  158. passProps: {
  159. passed: 'This is a prop passed in \'navigator.push()\'!',
  160. obj: {
  161. str: 'This is a prop passed in an object!',
  162. arr: [
  163. {
  164. str: 'This is a prop in an object in an array in an object!'
  165. }
  166. ]
  167. },
  168. num: 1234
  169. }
  170. });
  171. }
  172. onChangeLeftButtonPress() {
  173. this.props.navigator.setButtons({
  174. leftButton: {
  175. id: leftButtons[this.currentBackButton]
  176. }
  177. });
  178. this.currentBackButton += 1;
  179. this.currentBackButton = this.currentBackButton % 4;
  180. }
  181. onToggleBottomTabsPress() {
  182. this.props.navigator.toggleTabs({
  183. to: bottomTabsVisible ? 'shown' : 'hidden',
  184. animated: true
  185. });
  186. bottomTabsVisible = !bottomTabsVisible;
  187. }
  188. }
  189. const styles = StyleSheet.create({
  190. text: {
  191. textAlign: 'center',
  192. fontSize: 18,
  193. marginBottom: 10,
  194. marginTop: 10
  195. },
  196. button: {
  197. textAlign: 'center',
  198. fontSize: 18,
  199. marginBottom: 10,
  200. marginTop: 10,
  201. color: 'blue'
  202. }
  203. });
  204. // which props do we want to inject, given the global state?
  205. function mapStateToProps(state) {
  206. return {
  207. counter: state.counter
  208. };
  209. }
  210. export default connect(mapStateToProps)(PushedScreen);