Skip to content

Commit b7ed1aa

Browse files
authored
React 16 (APSL#173)
* Started to support r16 * First attempt to HOC all the things * ScrollView working * Moved to HOC all the components * Merged with latest branch * Updated Readme * Updated Readme * Removed unused deps
1 parent fe3b979 commit b7ed1aa

15 files changed

+10695
-1076
lines changed

.eslintrc

Lines changed: 208 additions & 153 deletions
Large diffs are not rendered by default.

.flowconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,4 @@ suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(>=0\\.\\(2[0-3]\\|1[0-9]\\|[0-9
9393
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
9494

9595
[version]
96-
0.47.0
96+
>=0.47.0

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ AwesomeProjectTests
55
index.ios.js
66
iOS
77

8-
.idea/
8+
.idea/
9+
.vscode/

README.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ A ScrollView component that handles keyboard appearance and automatically scroll
1313
</p>
1414

1515
## Supported versions
16+
- `v0.3.0` requires `RN>=0.48`
1617
- `v0.2.0` requires `RN>=0.32.0`.
1718
- `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.
1819
- `v0.0.7` requires `react-native>=0.25.0`.
@@ -30,10 +31,10 @@ yarn add react-native-keyboard-aware-scroll-view
3031
```
3132

3233
## Usage
33-
You can use the ``KeyboardAwareScrollView`` or the ``KeyboardAwareListView``
34-
components. Both accept ``ScrollView`` and ``ListView`` default props and
35-
implements a custom ``KeyboardAwareMixin`` to handle keyboard appearance.
36-
The mixin is also available if you want to use it in any other component.
34+
You can use the `KeyboardAwareScrollView`, the `KeyboardAwareListView` or the `KeyboardAwareFlatList`
35+
components. They accept `ScrollView`, `ListView` and `FlatList` default props respectively and
36+
implement a custom high order componente called `KeyboardAwareHOC` to handle keyboard appearance.
37+
The high order component is also available if you want to use it in any other component.
3738

3839
Import ``react-native-keyboard-aware-scroll-view`` and wrap your content inside
3940
it:
@@ -59,17 +60,17 @@ In order to scroll to any `TextInput` field, you can use the built-in method `sc
5960
```js
6061
_scrollToInput (reactNode: any) {
6162
// Add a 'scroll' ref to your ScrollView
62-
this.refs.scroll.scrollToFocusedInput(reactNode)
63+
this.scroll.props.scrollToFocusedInput(reactNode)
6364
}
6465
```
6566

6667
```jsx
67-
<KeyboardAwareScrollView ref='scroll'>
68+
<KeyboardAwareScrollView innerRef={ref => {this.scroll = ref}}>
6869
<View>
6970
<TextInput onFocus={(event: Event) => {
7071
// `bind` the function if you're using ES6 classes
7172
this._scrollToInput(ReactNative.findNodeHandle(event.target))
72-
}/>
73+
} />
7374
</View>
7475
</KeyboardAwareScrollView>
7576
```
@@ -78,7 +79,7 @@ _scrollToInput (reactNode: any) {
7879
There's another built-in function that lets you programatically scroll to any position of the scroll view:
7980
8081
```js
81-
this.refs.scroll.scrollToPosition(0, 0, true)
82+
this.scroll.props.scrollToPosition(0, 0)
8283
```
8384
8485
## Register to keyboard events
@@ -104,7 +105,7 @@ But if you want to use feature like `extraHeight`, you need to enable Android Su
104105
- Set `windowSoftInputMode` to `adjustPan` in `AndroidManifest.xml`.
105106
- Set `enableOnAndroid` property to `true`.
106107
107-
Android Suppor is not perfect, here is the support list:
108+
Android Support is not perfect, here is the supported list:
108109
109110
| **Prop** | **Android Support** |
110111
|----------|-----------------|
@@ -119,10 +120,11 @@ Android Suppor is not perfect, here is the support list:
119120
120121
## API
121122
### Props
122-
All the `ScrollView`/`ListView` props will be passed.
123+
All the `ScrollView`/`ListView`/`FlatList` props will be passed.
123124
124125
| **Prop** | **Type** | **Description** |
125126
|----------|----------|-----------------|
127+
| `innerRef` | `Function` | Catch the reference of the component. |
126128
| `viewIsInsideTabBar` | `boolean` | Adds an extra offset that represents the `TabBarIOS` height. |
127129
| `resetScrollToCoords` | `Object: {x: number, y: number}` | Coordinates that will be used to reset the scroll when the keyboard hides. |
128130
| `enableAutoAutomaticScroll` | `boolean` | When focus in `TextInput` will scroll the position, default is enabled. |
@@ -132,12 +134,33 @@ All the `ScrollView`/`ListView` props will be passed.
132134
| `keyboardOpeningTime` | `number` | Sets the delay time before scrolling to new position, default is 250 |
133135
| `enableOnAndroid` | `boolean` | Enable Android Support |
134136
137+
### Methods
138+
Use `innerRef` to get the component reference and use `this.scrollRef.props` to access these methods.
139+
135140
| **Method** | **Parameter** | **Description** |
136141
|------------|---------------|-----------------|
137142
| `getScrollResponder` | `void` | Get `ScrollResponder` |
138143
| `scrollToPosition` | `x: number, y: number, animated: bool = true` | Scroll to specific position with or without animation. |
139144
| `scrollToEnd` | `animated?: bool = true` | Scroll to end with or without animation. |
140145
146+
### Using high order component
147+
Enabling any component to be keyboard-aware is very easy. Take a look at the code of `KeyboardAwareListView`:
148+
149+
```js
150+
/* @flow */
151+
152+
import { ListView } from 'react-native'
153+
import listenToKeyboardEvents from './KeyboardAwareHOC'
154+
155+
export default listenToKeyboardEvents(ListView)
156+
```
157+
141158
## License
142159
143160
MIT.
161+
162+
## Author
163+
164+
Álvaro Medina Ballester `<amedina at apsl.net>`
165+
166+
Built with 💛 by [APSL](https://github.com/apsl).

index.d.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import * as React from 'react'
77
import { ScrollViewProperties, ListViewProperties } from 'react-native'
88

99
interface KeyboardAwareProps {
10-
/**
10+
/**
1111
* Adds an extra offset that represents the TabBarIOS height.
1212
*
13+
* Default is false
1314
* @type {boolean}
1415
* @memberof KeyboardAwareProps
1516
*/
16-
viewIsInsideTabBar?: boolean
17+
viewIsInsideTabBar?: boolean
1718

18-
/**
19+
/**
1920
* Coordinates that will be used to reset the scroll when the keyboard hides.
2021
*
2122
* @type {{
@@ -24,39 +25,39 @@ interface KeyboardAwareProps {
2425
* }}
2526
* @memberof KeyboardAwareProps
2627
*/
27-
resetScrollToCoords?: {
28-
x: number,
29-
y: number
30-
}
28+
resetScrollToCoords?: {
29+
x: number
30+
y: number
31+
}
3132

32-
/**
33+
/**
3334
* Lets the user enable or disable automatic resetScrollToCoords
3435
*
3536
* @type {boolean}
3637
* @memberof KeyboardAwareProps
3738
*/
38-
enableResetScrollToCoords?: boolean
39+
enableResetScrollToCoords?: boolean
3940

40-
/**
41+
/**
4142
* When focus in TextInput will scroll the position
4243
*
4344
* Default is true
4445
*
4546
* @type {boolean}
4647
* @memberof KeyboardAwareProps
4748
*/
48-
enableAutoAutomaticScroll?: boolean
49+
enableAutoAutomaticScroll?: boolean
4950

50-
/**
51+
/**
5152
* Adds an extra offset when focusing the TextInputs.
5253
*
5354
* Default is 75
5455
* @type {number}
5556
* @memberof KeyboardAwareProps
5657
*/
57-
extraHeight?: number
58+
extraHeight?: number
5859

59-
/**
60+
/**
6061
* Adds an extra offset to the keyboard.
6162
* Useful if you want to stick elements above the keyboard.
6263
*
@@ -65,26 +66,36 @@ interface KeyboardAwareProps {
6566
* @type {number}
6667
* @memberof KeyboardAwareProps
6768
*/
68-
extraScrollHeight?: number
69+
extraScrollHeight?: number
6970

70-
/**
71+
/**
7172
* Sets the delay time before scrolling to new position
7273
*
7374
* Default is 250
7475
*
7576
* @type {number}
7677
* @memberof KeyboardAwareProps
7778
*/
78-
keyboardOpeningTime?: number
79+
keyboardOpeningTime?: number
7980
}
8081

81-
interface KeyboardAwareListViewProps extends KeyboardAwareProps, ListViewProperties {}
82-
interface KeyboardAwareScrollViewProps extends KeyboardAwareProps, ScrollViewProperties {}
82+
interface KeyboardAwareListViewProps
83+
extends KeyboardAwareProps,
84+
ListViewProperties {}
85+
interface KeyboardAwareScrollViewProps
86+
extends KeyboardAwareProps,
87+
ScrollViewProperties {}
8388

8489
interface KeyboardAwareState {
8590
keyboardSpace: number
8691
}
8792

8893
export class KeyboardAwareMixin {}
89-
export class KeyboardAwareListView extends React.Component<KeyboardAwareListViewProps, KeyboardAwareState> { }
90-
export class KeyboardAwareScrollView extends React.Component<KeyboardAwareScrollViewProps, KeyboardAwareState> { }
94+
export class KeyboardAwareListView extends React.Component<
95+
KeyboardAwareListViewProps,
96+
KeyboardAwareState
97+
> {}
98+
export class KeyboardAwareScrollView extends React.Component<
99+
KeyboardAwareScrollViewProps,
100+
KeyboardAwareState
101+
> {}

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* @flow */
22

3-
import KeyboardAwareMixin from './lib/KeyboardAwareMixin'
3+
import listenToKeyboardEvents from './lib/KeyboardAwareHOC'
44
import KeyboardAwareScrollView from './lib/KeyboardAwareScrollView'
55
import KeyboardAwareListView from './lib/KeyboardAwareListView'
66
import KeyboardAwareFlatList from './lib/KeyboardAwareFlatList'
77

88
export {
9-
KeyboardAwareMixin,
9+
listenToKeyboardEvents,
1010
KeyboardAwareListView,
1111
KeyboardAwareFlatList,
1212
KeyboardAwareScrollView

lib/KeyboardAwareFlatList.js

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,6 @@
11
/* @flow */
22

3-
import React from 'react';
4-
import createReactClass from 'create-react-class';
5-
import PropTypes from 'prop-types';
6-
import { FlatList, Platform } from 'react-native';
7-
import KeyboardAwareMixin from './KeyboardAwareMixin';
3+
import { FlatList } from 'react-native'
4+
import listenToKeyboardEvents from './KeyboardAwareHOC'
85

9-
const KeyboardAwareFlatList = createReactClass({
10-
propTypes: {
11-
viewIsInsideTabBar: PropTypes.bool,
12-
resetScrollToCoords: PropTypes.shape({
13-
x: PropTypes.number,
14-
y: PropTypes.number,
15-
}),
16-
onScroll: PropTypes.func,
17-
enableOnAndroid: PropTypes.bool,
18-
},
19-
mixins: [KeyboardAwareMixin],
20-
21-
componentWillMount: function () {
22-
this.setViewIsInsideTabBar(!!this.props.viewIsInsideTabBar)
23-
if (this.props.resetScrollToCoords) {
24-
this.setResetScrollToCoords(this.props.resetScrollToCoords)
25-
}
26-
},
27-
28-
onScroll: function (e: SyntheticEvent & {nativeEvent: {contentOffset: number}}) {
29-
this.handleOnScroll(e)
30-
this.props.onScroll && this.props.onScroll(e)
31-
},
32-
33-
render: function () {
34-
const {
35-
enableOnAndroid,
36-
contentContainerStyle,
37-
} = this.props
38-
39-
const {
40-
keyboardSpace,
41-
} = this.state
42-
43-
let newContentContainerStyle
44-
45-
if (Platform.OS === 'android' && enableOnAndroid) {
46-
newContentContainerStyle = Object.assign({}, contentContainerStyle)
47-
newContentContainerStyle.paddingBottom = (newContentContainerStyle.paddingBottom || 0) + keyboardSpace
48-
}
49-
50-
return (
51-
<FlatList
52-
ref={this.handleRef}
53-
keyboardDismissMode='interactive'
54-
contentInset={{bottom: keyboardSpace}}
55-
automaticallyAdjustContentInsets={false}
56-
showsVerticalScrollIndicator={true}
57-
scrollEventThrottle={1}
58-
{...this.props}
59-
contentContainerStyle={newContentContainerStyle || contentContainerStyle}
60-
onScroll={this.onScroll}
61-
/>
62-
)
63-
},
64-
})
65-
66-
export default KeyboardAwareFlatList
6+
export default listenToKeyboardEvents(FlatList)

0 commit comments

Comments
 (0)