EvanBacon / expo-router-top-tabs

experimental
MIT License
47 stars 8 forks source link

Keyboard aware scroll view #13

Open sobrinho opened 1 month ago

sobrinho commented 1 month ago

Hi, @EvanBacon !

How can we integrate that library with something like react-native-keyboard-controller or a similar one?

Quick example: clicking on a field near to the end of the page, the keyboard hides the input:

Screenshot 2024-08-02 at 14 57 21 Screenshot 2024-08-02 at 14 57 27
sobrinho commented 1 month ago

I would expect to click on "Senha" and the app scroll to a position like this:

Screenshot 2024-08-02 at 14 58 43
rabbitbrother91 commented 2 weeks ago

Dear @sobrinho,

To address the issue of the keyboard obscuring input fields in your React Native application, I recommend implementing a keyboard-aware scroll view. Below are some detailed steps to assist you:

1. Utilize KeyboardAvoidingView

Begin by wrapping your entire form in a KeyboardAvoidingView. This component will help adjust the layout when the keyboard is displayed.

import React from 'react';
import { KeyboardAvoidingView, ScrollView, TextInput, StyleSheet } from 'react-native';

const YourComponent = () => {
  return (
    <KeyboardAvoidingView style={styles.container} behavior="padding">
      <ScrollView>
        <TextInput placeholder="Nome completo" style={styles.input} />
        <TextInput placeholder="CPF" style={styles.input} />
        <TextInput placeholder="Celular" style={styles.input} />
        <TextInput placeholder="Cidade" style={styles.input} />
        <TextInput placeholder="Senha" style={styles.input} secureTextEntry />
        <TextInput placeholder="Confirmação da senha" style={styles.input} secureTextEntry />
      </ScrollView>
    </KeyboardAvoidingView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  input: {
    height: 50,
    borderColor: 'gray',
    borderWidth: 1,
    margin: 10,
    paddingHorizontal: 10,
  },
});

2. Consider the react-native-keyboard-aware-scroll-view Library

For enhanced functionality, I recommend using the react-native-keyboard-aware-scroll-view library, which automatically adjusts the scroll view when the keyboard appears.

Installation:

npm install --save react-native-keyboard-aware-scroll-view

Implementation:

import React from 'react';
import { KeyboardAvoidingView, StyleSheet } from 'react-native';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';

const YourComponent = () => {
  return (
    <KeyboardAvoidingView style={styles.container}>
      <KeyboardAwareScrollView>
        {/* Include your input fields here */}
      </KeyboardAwareScrollView>
    </KeyboardAvoidingView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});

3. Conduct Testing on Various Devices

It is essential to test the implementation across different devices and orientations to ensure consistent keyboard behavior.

4. Adjust Scroll Behavior as Necessary

You may need to modify properties such as extraHeight or keyboardShouldPersistTaps in the KeyboardAwareScrollView to achieve the desired behavior.