• Stars
    star
    5,279
  • Rank 7,823 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A ScrollView component that handles keyboard appearance and automatically scrolls to focused TextInput.

react-native-keyboard-aware-scroll-view

A ScrollView component that handles keyboard appearance and automatically scrolls to focused TextInput.

Scroll demo

Supported versions

  • v0.4.0 requires RN>=0.48
  • v0.2.0 requires RN>=0.32.0.
  • v0.1.2 requires RN>=0.27.2 but you should use 0.2.0 in order to make it work with multiple scroll views.
  • v0.0.7 requires react-native>=0.25.0.
  • Use v0.0.6 for older RN versions.

Installation

Installation can be done through npm or yarn:

npm i react-native-keyboard-aware-scroll-view --save
yarn add react-native-keyboard-aware-scroll-view

Usage

You can use the KeyboardAwareScrollView, KeyboardAwareSectionList or the KeyboardAwareFlatList components. They accept ScrollView, SectionList and FlatList default props respectively and implement a custom high order component called KeyboardAwareHOC to handle keyboard appearance. The high order component is also available if you want to use it in any other component.

Import react-native-keyboard-aware-scroll-view and wrap your content inside it:

import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
<KeyboardAwareScrollView>
  <View>
    <TextInput />
  </View>
</KeyboardAwareScrollView>

Auto-scroll in TextInput fields

As of v0.1.0, the component auto scrolls to the focused TextInput ๐Ÿ˜Ž. For versions v0.0.7 and older you can do the following.

Programatically scroll to any TextInput

In order to scroll to any TextInput field, you can use the built-in method scrollToFocusedInput. Example:

_scrollToInput (reactNode: any) {
  // Add a 'scroll' ref to your ScrollView
  this.scroll.props.scrollToFocusedInput(reactNode)
}
<KeyboardAwareScrollView
  innerRef={ref => {
    this.scroll = ref
  }}>
  <View>
    <TextInput
      onFocus={(event: Event) => {
        // `bind` the function if you're using ES6 classes
        this._scrollToInput(ReactNative.findNodeHandle(event.target))
      }}
    />
  </View>
</KeyboardAwareScrollView>

Programatically scroll to any position

There's another built-in function that lets you programatically scroll to any position of the scroll view:

this.scroll.props.scrollToPosition(0, 0)

Register to keyboard events

You can register to ScrollViewResponder events onKeyboardWillShow and onKeyboardWillHide:

<KeyboardAwareScrollView
  onKeyboardWillShow={(frames: Object) => {
    console.log('Keyboard event', frames)
  }}>
  <View>
    <TextInput />
  </View>
</KeyboardAwareScrollView>

Android Support

First, Android natively has this feature, you can easily enable it by setting windowSoftInputMode in AndroidManifest.xml. Check here.

But if you want to use feature like extraHeight, you need to enable Android Support with the following steps:

  • Make sure you are using react-native 0.46 or above.
  • Set windowSoftInputMode to adjustPan in AndroidManifest.xml.
  • Set enableOnAndroid property to true.

Android Support is not perfect, here is the supported list:

Prop Android Support
viewIsInsideTabBar Yes
resetScrollToCoords Yes
enableAutomaticScroll Yes
extraHeight Yes
extraScrollHeight Yes
enableResetScrollToCoords Yes
keyboardOpeningTime No

API

Props

All the ScrollView/FlatList props will be passed.

Prop Type Description
innerRef Function Catch the reference of the component.
viewIsInsideTabBar boolean Adds an extra offset that represents the TabBarIOS height.
resetScrollToCoords Object: {x: number, y: number} Coordinates that will be used to reset the scroll when the keyboard hides.
enableAutomaticScroll boolean When focus in TextInput will scroll the position, default is enabled.
extraHeight number Adds an extra offset when focusing the TextInputs.
extraScrollHeight number Adds an extra offset to the keyboard. Useful if you want to stick elements above the keyboard.
enableResetScrollToCoords boolean Lets the user enable or disable automatic resetScrollToCoords.
keyboardOpeningTime number Sets the delay time before scrolling to new position, default is 250
enableOnAndroid boolean Enable Android Support

Methods

Use innerRef to get the component reference and use this.scrollRef.props to access these methods.

Method Parameter Description
getScrollResponder void Get ScrollResponder
scrollToPosition x: number, y: number, animated: bool = true Scroll to specific position with or without animation.
scrollToEnd animated?: bool = true Scroll to end with or without animation.
scrollIntoView element: React.Element<*>, options: { getScrollPosition: ?(parentLayout, childLayout, contentOffset) => { x: number, y: number, animated: boolean } } Scrolls an element inside a KeyboardAwareScrollView into view.

Using high order component

Enabling any component to be keyboard-aware is very easy. Take a look at the code of KeyboardAwareFlatList:

/* @flow */

import { FlatList } from 'react-native'
import listenToKeyboardEvents from './KeyboardAwareHOC'

export default listenToKeyboardEvents(FlatList)

The HOC can also be configured. Sometimes it's more convenient to provide a static config than configuring the behavior with props. This HOC config can be overriden with props.

/* @flow */

import { FlatList } from 'react-native'
import listenToKeyboardEvents from './KeyboardAwareHOC'

const config = {
  enableOnAndroid: true,
  enableAutomaticScroll: true
}

export default listenToKeyboardEvents(config)(FlatList)

The available config options are:

{
  enableOnAndroid: boolean,
  contentContainerStyle: ?Object,
  enableAutomaticScroll: boolean,
  extraHeight: number,
  extraScrollHeight: number,
  enableResetScrollToCoords: boolean,
  keyboardOpeningTime: number,
  viewIsInsideTabBar: boolean,
  refPropName: string,
  extractNativeRef: Function
}

License

MIT.

Author

รlvaro Medina Ballester <amedina at apsl.net>

Built with ๐Ÿ’› by APSL.

More Repositories

1

react-native-button

A React Native button component customizable via props
JavaScript
746
star
2

puput

A Django blog app implemented in Wagtail
Python
626
star
3

react-native-version-number

Gets the version number and build number of your app.
Java
382
star
4

docker-thumbor

Docker image for thumbor. Detectors, optimizers, lazy detection and separate docker for remotecv.
Smarty
292
star
5

redux-i18n

A simple and powerful package for translate your react applications using react-redux.
JavaScript
233
star
6

react-native-item-cell

React Native default iOS item cell
JavaScript
72
star
7

react-native-floating-label

A React Native floating label text input for tcomb-form-native
JavaScript
66
star
8

kaneda

Configurable Python library for metrics collection
Python
54
star
9

kubernetes-charts

Helm Charts to deploy Django apps in Kubernetes
Mustache
51
star
10

django-yubin

Send e-mails asyncronously using Celery
Python
49
star
11

go-aws-elb-log-analyzer

Download logs from ELB and join in a sorted file
Go
30
star
12

react-native-navigator-wrapper

A React Native Navigator component wrapper that implements nested navigators for both push and modal transitions
JavaScript
30
star
13

sauron

Sauron is a Django Channels app and Micropython IoT device to monitor toilet status
Python
23
star
14

transmanager

TransManager is a simple django app to deal with the translations tasks of the models content based on django-hvad.
HTML
19
star
15

django-hattori

Command to anonymize sensitive data
Python
18
star
16

pytest-checkipdb

Plugin to check if there are ipdb breakpoints left
Python
18
star
17

django-kaio

Class based settings for Django projects that can be read from multiple sources
Python
17
star
18

rasengan

Rasengan is a command-line tool for automated testing of multiple kind of integrations tests for domains, with a simple and flexible YAML definition syntax.
Python
15
star
19

docker-puput

Docker image for Puput blog
Python
14
star
20

wordpress-to-puput

Import your Wordpress blog data into Puput
Python
13
star
21

react-native-meneame

A meneame.net client for iOS and Android built with React Native
JavaScript
11
star
22

react-native-sumup

Sumup SDK for React Native
Objective-C
11
star
23

react-native-section-menu

A multi-platform React Native sectioned menu
JavaScript
10
star
24

tcomb-form-native-json-schema

A bridge between tcomb-json-schema and tcomb-form-native
JavaScript
10
star
25

react-native-options-button

A bottom animated options button
JavaScript
9
star
26

wagtail-jsonschema-forms

Generate JsonSchema Forms from Wagtail's forms and validate it
Python
8
star
27

docker-django

Django base Dockerfile. Process managed by circus. Config with envtpl.
Shell
7
star
28

puput-demo

An example blog app using Puput
Python
7
star
29

docker-rundeck

Shell
6
star
30

AssetsGenerator

Simple Python script to generate iOS icon image assets
Python
6
star
31

sshail

SSHail is a tool to generate on-demand SSH jails for users using Docker.
Python
6
star
32

docker-circusbase

Docker base image for processes managed with circus. Config parameters with envtpl.
Shell
6
star
33

kumostatus

Create CloudWatch dashboards in mobile html version
Python
6
star
34

docker-tomcat6

Docker container for Legacy Tomcat6 / Java6 app server
Smarty
5
star
35

django-email-foundation

Package for help you to make ease build email templates in your project.
Python
5
star
36

thumbor_talk

CSS
4
star
37

rxp-python

The official Realex Payments Python SDK
4
star
38

django-apslutils

Herramientas y librerรญas varias que utiliza el equipo de APSL para sus proyectos
JavaScript
4
star
39

pycharm-live-templates

Custom live templates
4
star
40

django-behind-lb

Behind-LB is a very simple and efficient Django middleware to obtain the client IP address when the project runs behind a trusted load balancer.
Python
4
star
41

page-size-check

An utility to check the size of your pages and resources used
Python
4
star
42

remotecv_aws

Module to load s3 images in remotecv
Python
3
star
43

apsl.github.io

APSL GitHub page
JavaScript
3
star
44

docker-lamp

Docker APSL base project for LAMP apps
Smarty
3
star
45

docker-bareos-bat

๐Ÿ‹ Dockerized bareos-bat
Dockerfile
3
star
46

docker-devpi

Nginx
3
star
47

django-object-authority

Package to authorize actions (CRUD) to users over concrete items.
Python
3
star
48

pycon-fastapi-example

Python
3
star
49

redux-action-api-utils

Redux actions utility for creating and handling REST API requests
JavaScript
3
star
50

zinnia-to-puput

Import your Zinnia blog data into Puput
Python
2
star
51

docker-postgres

Docker image for postgresql: configure with enviroment variables, postgis and wal-e
Smarty
2
star
52

django-month-filter

Month filter for admin
Python
2
star
53

generator-apsl-angular

This generator extends the default yo generator-angular in order to scaffold angular projects according to APSL dev standards.
JavaScript
2
star
54

docker-tomcat

Docker base container for Tomcat app server
Smarty
2
star
55

docker-wordpress

Wordpress on LAMP managed with circus. Wordpress config parameters managed with envtpl. Keys generated if not found in env.
Smarty
2
star
56

blogger-to-puput

Import your Blogger blog data into Puput
Python
2
star
57

postgresql-wal-e-nagios

Python
2
star
58

docker-tomcat5

Docker container for Legacy Tomcat5 / Java5 app server
Shell
1
star
59

docker-java

Docker base project for java Apps
1
star
60

docker-gcloud

gcloud tool with keys authentication
Shell
1
star
61

deploy_talk

CSS
1
star
62

react-native-scrum-pocket

Scrum Pocket's source code, made with React Native.
JavaScript
1
star
63

webtest

Helper class for selenium tests. Helps running single test, or multi-mechanize tests.
Python
1
star
64

redis-cluster-nagios

Script python to control the status of a master/slave redis cluster.
Python
1
star
65

salt-shellshock

Salt recipe for shellshock (CVE-2014-6271)
Shell
1
star
66

archiclean

Utility for cleaning apache archiva repository versions and snapshots
Python
1
star
67

sepakit

Converts AEB 19.14 Direct Debit TXT file to SEPA XML (Core scheme)
Go
1
star
68

docker-nginx-static

Nginx
1
star
69

ecsh

Cli tool to access Amazon ECS containers with a shell
Python
1
star
70

docker-drupal

Drupal on LAMP managed with circus. Drupal config parameters managed with envtpl. Keys generated if not found in env.
Shell
1
star
71

python-branch

A branch.io client in Python
Python
1
star
72

docker_talk

Docker intro presentation
CSS
1
star
73

ecs-scheduled-task-updater

Simple script to update the task definition of a scheduled task to the latest version
Python
1
star