react-native-navigation的迁移库

StaticLifecycleOverlay.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const React = require('react');
  2. const { Component } = require('react');
  3. const { View, Text, TouchableOpacity } = require('react-native');
  4. const { Navigation } = require('react-native-navigation');
  5. const testIDs = require('../testIDs');
  6. class StaticLifecycleOverlay extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. text: 'nothing yet',
  11. events: []
  12. };
  13. this.listeners = [];
  14. this.listeners.push(Navigation.events().registerComponentDidAppearListener((event) => {
  15. event.event = 'componentDidAppear';
  16. this.setState({
  17. events: [...this.state.events, { ...event }]
  18. });
  19. }));
  20. this.listeners.push(Navigation.events().registerComponentDidDisappearListener((event) => {
  21. event.event = 'componentDidDisappear';
  22. this.setState({
  23. events: [...this.state.events, { ...event }]
  24. });
  25. }));
  26. this.listeners.push(Navigation.events().registerCommandCompletedListener(({ commandId }) => {
  27. this.setState({
  28. events: [...this.state.events, { event: 'commandCompleted', commandId }]
  29. });
  30. }));
  31. this.listeners.push(Navigation.events().registerNavigationButtonPressedListener(({ componentId, buttonId }) => {
  32. this.setState({
  33. events: [...this.state.events, { event: 'navigationButtonPressed', buttonId, componentId }]
  34. });
  35. }));
  36. this.listeners.push(Navigation.events().registerModalDismissedListener(({ componentId }) => {
  37. this.setState({
  38. events: [...this.state.events, { event: 'modalDismissed', componentId }]
  39. });
  40. }));
  41. }
  42. componentWillUnmount() {
  43. this.listeners.forEach(listener => listener.remove());
  44. this.listeners = [];
  45. alert('Overlay Unmounted');
  46. }
  47. renderEvent(event) {
  48. if (event.commandId) {
  49. return <Text style={styles.h2}>{`${event.commandId}`}</Text>;
  50. } else if (event.componentName) {
  51. return <Text style={styles.h2}>{`${event.event} | ${event.componentName}`}</Text>;
  52. } else if (event.buttonId) {
  53. return <Text style={styles.h2}>{`${event.event} | ${event.buttonId}`}</Text>;
  54. } else {
  55. return <Text style={styles.h2}>{`${event.event} | ${event.componentId}`}</Text>;
  56. }
  57. }
  58. render() {
  59. const events = this.state.events.map((event, idx) =>
  60. (
  61. <View key={`${event.componentId}${idx}`}>
  62. {this.renderEvent(event)}
  63. </View>
  64. ));
  65. return (
  66. <View style={styles.root}>
  67. <Text style={styles.h1}>{`Static Lifecycle Events Overlay`}</Text>
  68. <View style={styles.events}>
  69. {events}
  70. </View>
  71. {this.renderDismissButton()}
  72. </View>
  73. );
  74. }
  75. renderDismissButton = () => {
  76. return (
  77. <TouchableOpacity
  78. style={styles.dismissBtn}
  79. onPress={() => Navigation.dismissOverlay(this.props.componentId)}
  80. >
  81. <Text testID={testIDs.DISMISS_BUTTON} style={{ color: 'red', alignSelf: 'center' }}>X</Text>
  82. </TouchableOpacity>
  83. );
  84. }
  85. }
  86. module.exports = StaticLifecycleOverlay;
  87. const styles = {
  88. root: {
  89. position: 'absolute',
  90. bottom: 0,
  91. left: 0,
  92. right: 0,
  93. height: 150,
  94. backgroundColor: '#c1d5e0ae',
  95. flexDirection: 'column'
  96. },
  97. dismissBtn: {
  98. position: 'absolute',
  99. width: 35,
  100. height: 35,
  101. backgroundColor: 'white',
  102. justifyContent: 'center'
  103. },
  104. events: {
  105. flexDirection: 'column',
  106. marginHorizontal: 2
  107. },
  108. h1: {
  109. fontSize: 14,
  110. textAlign: 'center',
  111. margin: 4
  112. },
  113. h2: {
  114. fontSize: 10
  115. }
  116. };