Skip to content

Commit 9973a7e

Browse files
Merge pull request APSL#148 from chrisy-lili/flat-list
add keyboard aware support to FlatList
2 parents 0d452fe + dbec23d commit 9973a7e

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

lib/KeyboardAwareFlatList.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* @flow */
2+
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';
8+
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='_rnkasv_keyboardView'
53+
keyboardDismissMode='interactive'
54+
contentInset={{bottom: keyboardSpace}}
55+
automaticallyAdjustContentInsets={false}
56+
showsVerticalScrollIndicator={true}
57+
scrollEventThrottle={0}
58+
{...this.props}
59+
contentContainerStyle={newContentContainerStyle || contentContainerStyle}
60+
onScroll={this.onScroll}
61+
/>
62+
)
63+
},
64+
})
65+
66+
export default KeyboardAwareFlatList

0 commit comments

Comments
 (0)