Skip to content

Commit 6f2b771

Browse files
author
alvaromb
committed
Added new files.
1 parent 2bd8c20 commit 6f2b771

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import KeyboardAwareMixin from './lib/KeyboardAwareMixin'
2+
import KeyboardAwareScrollView from './lib/KeyboardAwareScrollView'
3+
import KeyboardAwareListView from './lib/KeyboardAwareListView'
4+
5+
export default {
6+
KeyboardAwareMixin,
7+
KeyboardAwareScrollView,
8+
KeyboardAwareListView
9+
}

lib/KeyboardAwareListView.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, { ListView } from 'react-native'
2+
import KeyboardAwareMixin from './KeyboardAwareMixin'
3+
4+
const KeyboardAwareListView = React.createClass({
5+
propTypes: {
6+
...ListView.propTypes,
7+
},
8+
mixins: [KeyboardAwareMixin],
9+
10+
render: function () {
11+
return (
12+
<ListView
13+
ref='keyboardView'
14+
keyboardDismissMode='interactive'
15+
contentInset={{bottom: this.state.keyboardSpace}}
16+
showsVerticalScrollIndicator={true}
17+
{...this.props}
18+
/>
19+
)
20+
},
21+
})
22+
23+
export default KeyboardAwareListView

lib/KeyboardAwareMixin.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React, { DeviceEventEmitter } from 'react-native'
2+
import TimerMixin from 'react-timer-mixin'
3+
4+
const KeyboardAwareMixin = {
5+
propTypes: {
6+
viewIsInsideTabBar: React.PropTypes.bool,
7+
},
8+
mixins: [TimerMixin],
9+
10+
getInitialState: function (props) {
11+
return {
12+
keyboardSpace: 0,
13+
}
14+
},
15+
16+
// Keyboard actions
17+
// TODO: automatically handle TabBar height instead of using props
18+
updateKeyboardSpace: function (frames) {
19+
const keyboardSpace = (this.props.viewIsInsideTabBar) ? frames.endCoordinates.height - 49 : frames.endCoordinates.height
20+
this.setState({
21+
keyboardSpace: keyboardSpace,
22+
})
23+
},
24+
25+
resetKeyboardSpace: function () {
26+
this.setState({
27+
keyboardSpace: 0,
28+
})
29+
},
30+
31+
componentDidMount: function () {
32+
// Keyboard events
33+
DeviceEventEmitter.addListener('keyboardWillShow', this.updateKeyboardSpace)
34+
DeviceEventEmitter.addListener('keyboardWillHide', this.resetKeyboardSpace)
35+
},
36+
37+
componentWillUnmount: function () {
38+
// TODO: figure out if removeAllListeners is the right thing to do
39+
DeviceEventEmitter.removeAllListeners('keyboardWillShow')
40+
DeviceEventEmitter.removeAllListeners('keyboardWillHide')
41+
},
42+
43+
/**
44+
* @param extraHeight: takes an extra height in consideration.
45+
*/
46+
scrollToFocusedInput: function (event, reactNode, extraHeight = 49) {
47+
const scrollView = this.refs.keyboardView.getScrollResponder()
48+
this.setTimeout(() => {
49+
scrollView.scrollResponderScrollNativeHandleToKeyboard(
50+
reactNode, extraHeight, true
51+
)
52+
}, 220)
53+
},
54+
}
55+
56+
export default KeyboardAwareMixin

0 commit comments

Comments
 (0)