Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import {
  3. Dimensions,
  4. Keyboard,
  5. KeyboardAvoidingView,
  6. SafeAreaView,
  7. ScrollView,
  8. TextInput,
  9. TouchableWithoutFeedback,
  10. View,
  11. } from 'react-native'
  12.  
  13. /*
  14. * Components -- these are custom components from our boilerplate
  15. * You can replace these with the component of your choice
  16. * -- FullButton is made from ReactNative's TouchableOpacity and View
  17. * -- Text is made from ReactNative's Text component with additional style prop helpers
  18. */
  19. import { FullButton, Text } from '../../components'
  20.  
  21. /*
  22. * Component to demonstrate a form example where the keyboard scrolls the view up properly, allows
  23. * the user to click the button when the keyboard is open, and allows the user to click on the
  24. * background to close the keyboard
  25. *
  26. * Note: To fully test this component, make sure that your device is set to use the software
  27. * keyboard
  28. */
  29. class KeyboardAvoidExample extends Component {
  30. componentDidMount() {
  31. this._keyboardDidShowListener = Keyboard.addListener(
  32. 'keyboardDidShow',
  33. this._handleKeyboardDidShow,
  34. )
  35. }
  36.  
  37. componentWillUnmount() {
  38. this._keyboardDidShowListener.remove()
  39. }
  40.  
  41. _handleKeyboardDidShow = () => {
  42. /*
  43. * The scrollToEnd call can be omitted if you don't want to scroll to the bottom.
  44. * If you have a long form, you should remove this. If your form fits with the keyboard shown,
  45. * you can use this to ensure the action button is visible
  46. */
  47. this.scrollView.scrollToEnd()
  48.  
  49. /*
  50. * Wait a half second before flashing the scroll indicators. This makes sure the keyboard has
  51. * finished animating so the user isn't distracted by that animiation. Flashing the scroll
  52. * indicators shows the user they can scroll in the view
  53. */
  54. setTimeout(() => {
  55. this.scrollView.flashScrollIndicators()
  56. }, 500)
  57. }
  58.  
  59. render() {
  60. return (
  61. <KeyboardAvoidingView behavior="padding">
  62. {/*
  63. * `keyboardShouldPersistTaps` ensures that the user can click on the button even when the
  64. * keyboard is open. If that isn't the desired behavior, you cann remove
  65. * `keyboardShouldPersistTaps` and the TouchableWithoutFeedback component below
  66. */}
  67. <ScrollView
  68. keyboardShouldPersistTaps
  69. alwaysBounceVertical={false}
  70. ref={ref => {
  71. this.scrollView = ref
  72. }}
  73. >
  74. {/*
  75. * Enable the user to click the background and close the keyboard. This isn't needed if
  76. * you remove `keyboardShouldPersistTaps` from the ScrollView, as the ScrollView handles
  77. * this otherwise
  78. */}
  79. <TouchableWithoutFeedback
  80. onPress={() => {
  81. Keyboard.dismiss()
  82. }}
  83. >
  84. <SafeAreaView style={{ flex: 1 }}>
  85. <View style={{ minHeight: Dimensions.get('window').height }}>
  86. <Text center dark mY={200}>
  87. Some Text!
  88. </Text>
  89.  
  90. {/* This doesn't have proper prop values and is just for demonstration */}
  91. <TextInput
  92. onChangeText={() => {}}
  93. style={{ backgroundColor: 'white' }}
  94. value={1}
  95. />
  96.  
  97. <FullButton title="Do Something" />
  98. </View>
  99. </SafeAreaView>
  100. </TouchableWithoutFeedback>
  101. </ScrollView>
  102. </KeyboardAvoidingView>
  103. )
  104. }
  105. }
  106.  
  107. export default KeyboardAvoidExample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement