react-native-navigation的迁移库

Actions.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import React from 'react';
  2. import {StyleSheet, ScrollView} from 'react-native';
  3. import Row from '../components/Row';
  4. class Actions extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this._fab = false;
  8. this._rightButton = null;
  9. this._contextualMenu = false;
  10. this._toggleTabs = 'shown';
  11. this._toggleNavBar = 'shown';
  12. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  13. }
  14. onNavigatorEvent(event) {
  15. if (event.id === 'contextualMenuDismissed') {
  16. this._contextualMenu = false;
  17. }
  18. }
  19. setTitle = () => {
  20. this.props.navigator.setTitle({
  21. title: 'New Title!',
  22. });
  23. };
  24. toggleTabs = () => {
  25. const to = this._toggleTabs === 'shown' ? 'hidden' : 'shown';
  26. this.props.navigator.toggleTabs({
  27. to,
  28. animated: true,
  29. });
  30. this._toggleTabs = to;
  31. };
  32. setTabBadge = () => {
  33. this.props.navigator.setTabBadge({
  34. tabIndex: 1,
  35. badge: Math.floor(Math.random() * 20) + 1,
  36. });
  37. };
  38. switchToTab = () => {
  39. this.props.navigator.switchToTab({
  40. tabIndex: 0,
  41. });
  42. };
  43. toggleNavBar = () => {
  44. const to = this._toggleNavBar === 'shown' ? 'hidden' : 'shown';
  45. this.props.navigator.toggleNavBar({
  46. to,
  47. animated: true,
  48. });
  49. this._toggleNavBar = to;
  50. };
  51. showSnackbar = () => {
  52. this.props.navigator.showSnackbar({
  53. title: 'Woo Snacks!',
  54. });
  55. };
  56. toggleContextualMenu = () => {
  57. if (!this._contextualMenu) {
  58. this.props.navigator.showContextualMenu({
  59. rightButtons: [{
  60. title: 'Edit',
  61. icon: require('../../img/edit.png'),
  62. }, {
  63. title: 'Delete',
  64. icon: require('../../img/delete.png'),
  65. }],
  66. onButtonPressed: (index) => console.log(`Button ${index} tapped`)
  67. });
  68. this._contextualMenu = true;
  69. } else {
  70. this.props.navigator.dismissContextualMenu();
  71. this._contextualMenu = false;
  72. }
  73. };
  74. setButtons = () => {
  75. let title = '';
  76. if (!this._rightButton) {
  77. title = 'Hello';
  78. } else if (this._rightButton === 'Hello') {
  79. title = 'Its Me';
  80. }
  81. this.props.navigator.setButtons({
  82. rightButtons: [{
  83. title,
  84. id: 'topRight',
  85. }],
  86. animated: true,
  87. });
  88. this._rightButton = title;
  89. };
  90. toggleFAB = () => {
  91. if (this._fab) {
  92. this.props.navigator.setButtons({
  93. fab: {},
  94. });
  95. this._fab = false;
  96. } else {
  97. this.props.navigator.setButtons({
  98. fab: {
  99. collapsedId: 'share',
  100. collapsedIcon: require('../../img/edit@1x.png'),
  101. expendedId: 'clear',
  102. expendedIcon: require('../../img/edit@1x.png'),
  103. backgroundColor: '#ff505c',
  104. actions: [
  105. {
  106. id: 'mail',
  107. icon: require('../../img/edit@1x.png'),
  108. backgroundColor: '#03A9F4'
  109. },
  110. {
  111. id: 'delete',
  112. icon: require('../../img/delete@1x.png'),
  113. backgroundColor: '#4CAF50'
  114. }
  115. ]
  116. },
  117. animated: true,
  118. });
  119. this._fab = true;
  120. }
  121. };
  122. render() {
  123. return (
  124. <ScrollView style={styles.container}>
  125. <Row title={'Set Title'} onPress={this.setTitle}/>
  126. <Row title={'Toggle Tabs'} onPress={this.toggleTabs}/>
  127. <Row title={'Set Tab Badge'} onPress={this.setTabBadge}/>
  128. <Row title={'Switch To Tab 0'} onPress={this.switchToTab}/>
  129. <Row title={'Toggle Nav Bar'} onPress={this.toggleNavBar}/>
  130. <Row title={'Show Snackbar'} onPress={this.showSnackbar} platform={'android'}/>
  131. <Row title={'Toggle Contextual Menu'} onPress={this.toggleContextualMenu} platform={'android'}/>
  132. <Row title={'Set Right Buttons'} onPress={this.setButtons}/>
  133. <Row title={'Toggle FAB'} onPress={this.toggleFAB} platform={'android'}/>
  134. </ScrollView>
  135. );
  136. }
  137. }
  138. const styles = StyleSheet.create({
  139. container: {},
  140. });
  141. export default Actions;