KeyboardHandlingExample.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import React from 'react';
  2. import { StatusBar, View, TextInput, InteractionManager } from 'react-native';
  3. import { createStackNavigator, withNavigationFocus } from 'react-navigation';
  4. import { Button } from './commonComponents/ButtonWithMargin';
  5. class ScreenOne extends React.Component {
  6. static navigationOptions = {
  7. title: 'Home',
  8. };
  9. render() {
  10. const { navigation } = this.props;
  11. return (
  12. <View style={{ paddingTop: 30 }}>
  13. <Button
  14. onPress={() => navigation.push('ScreenTwo')}
  15. title="Push screen with focused text input"
  16. />
  17. <Button onPress={() => navigation.goBack(null)} title="Go Home" />
  18. <StatusBar barStyle="default" />
  19. </View>
  20. );
  21. }
  22. }
  23. class ScreenTwo extends React.Component {
  24. static navigationOptions = ({ navigation }) => ({
  25. title: navigation.getParam('inputValue', 'Screen w/ Input'),
  26. });
  27. componentDidMount() {
  28. InteractionManager.runAfterInteractions(() => {
  29. this._textInput.focus();
  30. });
  31. }
  32. render() {
  33. const { navigation } = this.props;
  34. return (
  35. <View style={{ paddingTop: 30 }}>
  36. <View style={{ alignSelf: 'center', paddingVertical: 20 }}>
  37. <TextInput
  38. ref={c => (this._textInput = c)}
  39. onChangeText={inputValue => navigation.setParams({ inputValue })}
  40. style={{
  41. backgroundColor: 'white',
  42. height: 24,
  43. width: 150,
  44. borderColor: '#555',
  45. borderWidth: 1,
  46. }}
  47. />
  48. </View>
  49. <Button onPress={() => navigation.pop()} title="Pop" />
  50. </View>
  51. );
  52. }
  53. }
  54. export default createStackNavigator({
  55. ScreenOne,
  56. ScreenTwo: withNavigationFocus(ScreenTwo),
  57. });