@@ -13,14 +13,16 @@ A ScrollView component that handles keyboard appearance and automatically scroll
13
13
</p >
14
14
15
15
## Supported versions
16
+
16
17
- ` v0.4.0 ` requires ` RN>=0.48 `
17
18
- ` v0.2.0 ` requires ` RN>=0.32.0 ` .
18
19
- ` 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.
19
20
- ` v0.0.7 ` requires ` react-native>=0.25.0 ` .
20
21
- Use ` v0.0.6 ` for older RN versions.
21
22
22
23
## Installation
23
- Installation can be done through `` npm `` or ` yarn ` :
24
+
25
+ Installation can be done through ` npm ` or ` yarn ` :
24
26
25
27
``` shell
26
28
npm i react-native-keyboard-aware-scroll-view --save
@@ -31,12 +33,13 @@ yarn add react-native-keyboard-aware-scroll-view
31
33
```
32
34
33
35
## Usage
34
- You can use the ` KeyboardAwareScrollView ` , the ` KeyboardAwareListView ` , ` KeyboardAwareSectionList ` or the ` KeyboardAwareFlatList `
35
- components. They accept ` ScrollView ` , ` ListView ` , ` SectionList ` and ` FlatList ` default props respectively and
36
+
37
+ You can use the ` KeyboardAwareScrollView ` , ` KeyboardAwareSectionList ` or the ` KeyboardAwareFlatList `
38
+ components. They accept ` ScrollView ` , ` SectionList ` and ` FlatList ` default props respectively and
36
39
implement a custom high order componente called ` KeyboardAwareHOC ` to handle keyboard appearance.
37
40
The high order component is also available if you want to use it in any other component.
38
41
39
- Import `` react-native-keyboard-aware-scroll-view ` ` and wrap your content inside
42
+ Import ` react-native-keyboard-aware-scroll-view ` and wrap your content inside
40
43
it:
41
44
42
45
``` js
@@ -52,9 +55,11 @@ import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view
52
55
```
53
56
54
57
## Auto-scroll in ` TextInput ` fields
58
+
55
59
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.
56
60
57
61
### Programatically scroll to any ` TextInput `
62
+
58
63
In order to scroll to any ` TextInput ` field, you can use the built-in method ` scrollToFocusedInput ` . Example:
59
64
60
65
``` js
@@ -65,24 +70,31 @@ _scrollToInput (reactNode: any) {
65
70
```
66
71
67
72
``` jsx
68
- < KeyboardAwareScrollView innerRef= {ref => {this .scroll = ref}}>
73
+ < KeyboardAwareScrollView
74
+ innerRef= {ref => {
75
+ this .scroll = ref
76
+ }}>
69
77
< View>
70
- < TextInput onFocus= {(event : Event ) => {
71
- // `bind` the function if you're using ES6 classes
72
- this ._scrollToInput (ReactNative .findNodeHandle (event .target ))
73
- }} / >
78
+ < TextInput
79
+ onFocus= {(event : Event ) => {
80
+ // `bind` the function if you're using ES6 classes
81
+ this ._scrollToInput (ReactNative .findNodeHandle (event .target ))
82
+ }}
83
+ / >
74
84
< / View>
75
85
< / KeyboardAwareScrollView>
76
86
```
77
87
78
88
### Programatically scroll to any position
89
+
79
90
There's another built-in function that lets you programatically scroll to any position of the scroll view:
80
91
81
92
``` js
82
93
this .scroll .props .scrollToPosition (0 , 0 )
83
94
```
84
95
85
96
## Register to keyboard events
97
+
86
98
You can register to ` ScrollViewResponder ` events ` onKeyboardWillShow ` and ` onKeyboardWillHide ` :
87
99
88
100
``` jsx
@@ -97,6 +109,7 @@ You can register to `ScrollViewResponder` events `onKeyboardWillShow` and `onKey
97
109
```
98
110
99
111
## Android Support
112
+
100
113
First, Android natively has this feature, you can easily enable it by setting ` windowSoftInputMode ` in ` AndroidManifest.xml ` . Check [ here] ( https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft ) .
101
114
102
115
But if you want to use feature like ` extraHeight ` , you need to enable Android Support with the following steps:
@@ -107,69 +120,72 @@ But if you want to use feature like `extraHeight`, you need to enable Android Su
107
120
108
121
Android Support is not perfect, here is the supported list:
109
122
110
- | ** Prop** | ** Android Support** |
111
- | ----------| -----------------|
112
- | ` viewIsInsideTabBar ` | Yes |
113
- | ` resetScrollToCoords ` | Yes |
114
- | ` enableAutomaticScroll ` | Yes |
115
- | ` extraHeight ` | Yes |
116
- | ` extraScrollHeight ` | Yes |
117
- | ` enableResetScrollToCoords ` | Yes |
118
- | ` keyboardOpeningTime ` | No |
119
-
123
+ | ** Prop** | ** Android Support** |
124
+ | --------------------------- | ------------------- |
125
+ | ` viewIsInsideTabBar ` | Yes |
126
+ | ` resetScrollToCoords ` | Yes |
127
+ | ` enableAutomaticScroll ` | Yes |
128
+ | ` extraHeight ` | Yes |
129
+ | ` extraScrollHeight ` | Yes |
130
+ | ` enableResetScrollToCoords ` | Yes |
131
+ | ` keyboardOpeningTime ` | No |
120
132
121
133
## API
134
+
122
135
### Props
123
- All the ` ScrollView ` /` ListView ` /` FlatList ` props will be passed.
124
-
125
- | ** Prop** | ** Type** | ** Description** |
126
- | ----------| ----------| -----------------|
127
- | ` innerRef ` | ` Function ` | Catch the reference of the component. |
128
- | ` viewIsInsideTabBar ` | ` boolean ` | Adds an extra offset that represents the ` TabBarIOS ` height. |
129
- | ` resetScrollToCoords ` | ` Object: {x: number, y: number} ` | Coordinates that will be used to reset the scroll when the keyboard hides. |
130
- | ` enableAutomaticScroll ` | ` boolean ` | When focus in ` TextInput ` will scroll the position, default is enabled. |
131
- | ` extraHeight ` | ` number ` | Adds an extra offset when focusing the ` TextInput ` s. |
132
- | ` extraScrollHeight ` | ` number ` | Adds an extra offset to the keyboard. Useful if you want to stick elements above the keyboard. |
133
- | ` enableResetScrollToCoords ` | ` boolean ` | Lets the user enable or disable automatic resetScrollToCoords. |
134
- | ` keyboardOpeningTime ` | ` number ` | Sets the delay time before scrolling to new position, default is 250 |
135
- | ` enableOnAndroid ` | ` boolean ` | Enable Android Support |
136
+
137
+ All the ` ScrollView ` /` FlatList ` props will be passed.
138
+
139
+ | ** Prop** | ** Type** | ** Description** |
140
+ | --------------------------- | -------------------------------- | ---------------------------------------------------------------------------------------------- |
141
+ | ` innerRef ` | ` Function ` | Catch the reference of the component. |
142
+ | ` viewIsInsideTabBar ` | ` boolean ` | Adds an extra offset that represents the ` TabBarIOS ` height. |
143
+ | ` resetScrollToCoords ` | ` Object: {x: number, y: number} ` | Coordinates that will be used to reset the scroll when the keyboard hides. |
144
+ | ` enableAutomaticScroll ` | ` boolean ` | When focus in ` TextInput ` will scroll the position, default is enabled. |
145
+ | ` extraHeight ` | ` number ` | Adds an extra offset when focusing the ` TextInput ` s. |
146
+ | ` extraScrollHeight ` | ` number ` | Adds an extra offset to the keyboard. Useful if you want to stick elements above the keyboard. |
147
+ | ` enableResetScrollToCoords ` | ` boolean ` | Lets the user enable or disable automatic resetScrollToCoords. |
148
+ | ` keyboardOpeningTime ` | ` number ` | Sets the delay time before scrolling to new position, default is 250 |
149
+ | ` enableOnAndroid ` | ` boolean ` | Enable Android Support |
136
150
137
151
### Methods
152
+
138
153
Use ` innerRef ` to get the component reference and use ` this.scrollRef.props ` to access these methods.
139
154
140
- | ** Method** | ** Parameter** | ** Description** |
141
- | ------------| ---------------| -----------------|
142
- | ` getScrollResponder ` | ` void ` | Get ` ScrollResponder ` |
143
- | ` scrollToPosition ` | ` x: number, y: number, animated: bool = true ` | Scroll to specific position with or without animation. |
144
- | ` scrollToEnd ` | ` animated?: bool = true ` | Scroll to end with or without animation. |
145
- | ` scrollIntoView ` | ` element: React.Element<*>, options: { getScrollPosition: ?(parentLayout, childLayout, contentOffset) => { x: number, y: number, animated: boolean } } ` | Scrolls an element inside a KeyboardAwareScrollView into view. |
155
+ | ** Method** | ** Parameter** | ** Description** |
156
+ | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
157
+ | ` getScrollResponder ` | ` void ` | Get ` ScrollResponder ` |
158
+ | ` scrollToPosition ` | ` x: number, y: number, animated: bool = true ` | Scroll to specific position with or without animation. |
159
+ | ` scrollToEnd ` | ` animated?: bool = true ` | Scroll to end with or without animation. |
160
+ | ` scrollIntoView ` | ` element: React.Element<*>, options: { getScrollPosition: ?(parentLayout, childLayout, contentOffset) => { x: number, y: number, animated: boolean } } ` | Scrolls an element inside a KeyboardAwareScrollView into view. |
146
161
147
162
### Using high order component
148
- Enabling any component to be keyboard-aware is very easy. Take a look at the code of ` KeyboardAwareListView ` :
163
+
164
+ Enabling any component to be keyboard-aware is very easy. Take a look at the code of ` KeyboardAwareFlatList ` :
149
165
150
166
``` js
151
167
/* @flow */
152
168
153
- import { ListView } from ' react-native'
169
+ import { FlatList } from ' react-native'
154
170
import listenToKeyboardEvents from ' ./KeyboardAwareHOC'
155
171
156
- export default listenToKeyboardEvents (ListView )
172
+ export default listenToKeyboardEvents (FlatList )
157
173
```
158
174
159
175
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.
160
176
161
177
``` js
162
178
/* @flow */
163
179
164
- import { ListView } from ' react-native'
180
+ import { FlatList } from ' react-native'
165
181
import listenToKeyboardEvents from ' ./KeyboardAwareHOC'
166
182
167
183
const config = {
168
184
enableOnAndroid: true ,
169
- enableAutomaticScroll: true ,
170
- };
185
+ enableAutomaticScroll: true
186
+ }
171
187
172
- export default listenToKeyboardEvents (config)(ListView )
188
+ export default listenToKeyboardEvents (config)(FlatList )
173
189
```
174
190
175
191
The available config options are:
@@ -189,7 +205,6 @@ The available config options are:
189
205
}
190
206
```
191
207
192
-
193
208
## License
194
209
195
210
MIT.
0 commit comments