|
| 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