Skip to content

Commit 02cf6aa

Browse files
committed
First commit of the component
1 parent 83dad77 commit 02cf6aa

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

KeyboardAwareScrollView.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React, { ScrollView, PropTypes, DeviceEventEmitter } from 'react-native'
2+
import StyleSheetPropType from 'react-native/Libraries/StyleSheet/StyleSheetPropType'
3+
import ViewStylePropTypes from 'react-native/Libraries/Components/View/ViewStylePropTypes'
4+
import TimerMixin from 'react-timer-mixin'
5+
6+
const KeyboardAwareScrollView = React.createClass({
7+
propTypes: {
8+
style: StyleSheetPropType(ViewStylePropTypes),
9+
children: PropTypes.node,
10+
viewIsInsideTabBar: PropTypes.bool,
11+
},
12+
mixins: [TimerMixin],
13+
14+
getInitialState: function (props) {
15+
return {
16+
keyboardSpace: 0,
17+
}
18+
},
19+
20+
// Keyboard actions
21+
// TODO: automatically handle TabBar height instead of using props
22+
updateKeyboardSpace: function (frames) {
23+
const keyboardSpace = (this.props.viewIsInsideTabBar) ? frames.endCoordinates.height - 49 : frames.endCoordinates.height
24+
this.setState({
25+
keyboardSpace: keyboardSpace,
26+
})
27+
},
28+
29+
resetKeyboardSpace: function () {
30+
this.setState({
31+
keyboardSpace: 0,
32+
})
33+
},
34+
35+
componentDidMount: function () {
36+
// Keyboard events
37+
DeviceEventEmitter.addListener('keyboardWillShow', this.updateKeyboardSpace)
38+
DeviceEventEmitter.addListener('keyboardWillHide', this.resetKeyboardSpace)
39+
},
40+
41+
componentWillUnmount: function () {
42+
// TODO: figure out if removeAllListeners is the right thing to do
43+
DeviceEventEmitter.removeAllListeners('keyboardWillShow')
44+
DeviceEventEmitter.removeAllListeners('keyboardWillHide')
45+
},
46+
47+
/**
48+
* @param extraHeight: takes an extra height in consideration.
49+
*/
50+
scrollToFocusedInput: function (event, reactNode, extraHeight = 49) {
51+
const scrollView = this.refs.keyboardScrollView.getScrollResponder()
52+
this.setTimeout(() => {
53+
scrollView.scrollResponderScrollNativeHandleToKeyboard(
54+
reactNode, extraHeight, true
55+
)
56+
}, 220)
57+
},
58+
59+
render: function () {
60+
return (
61+
<ScrollView
62+
ref='keyboardScrollView'
63+
keyboardDismissMode='interactive'
64+
contentInset={{bottom: this.state.keyboardSpace}}
65+
showsVerticalScrollIndicator={true}
66+
style={this.props.style}>
67+
{this.props.children}
68+
</ScrollView>
69+
)
70+
},
71+
})
72+
73+
export default KeyboardAwareScrollView

0 commit comments

Comments
 (0)