diff --git a/KeyboardSpacer.js b/KeyboardSpacer.js index ef0b76e..5f47de4 100644 --- a/KeyboardSpacer.js +++ b/KeyboardSpacer.js @@ -6,6 +6,7 @@ import { Keyboard, LayoutAnimation, View, + ViewPropTypes, Platform, StyleSheet } from 'react-native'; @@ -22,8 +23,9 @@ export default class KeyboardSpacer extends Component { static propTypes = { topSpacing: PropTypes.number, onToggle: PropTypes.func, - style: View.propTypes.style, + style: ViewPropTypes.style, animationConfig: PropTypes.object, + android: PropTypes.bool, }; static defaultProps = { @@ -42,6 +44,7 @@ export default class KeyboardSpacer extends Component { } }, onToggle: () => null, + android: true, }; constructor(props, context) { @@ -58,10 +61,14 @@ export default class KeyboardSpacer extends Component { componentDidMount() { const updateListener = Platform.OS === 'android' ? 'keyboardDidShow' : 'keyboardWillShow'; const resetListener = Platform.OS === 'android' ? 'keyboardDidHide' : 'keyboardWillHide'; - this._listeners = [ - Keyboard.addListener(updateListener, this.updateKeyboardSpace), - Keyboard.addListener(resetListener, this.resetKeyboardSpace) - ]; + const listenAndroid = Platform.OS === 'android' && this.props.android; + const shouldListen = listenAndroid || Platform.OS === 'ios'; + if (shouldListen) { + this._listeners = [ + Keyboard.addListener(updateListener, this.updateKeyboardSpace), + Keyboard.addListener(resetListener, this.resetKeyboardSpace) + ]; + } } componentWillUpdate(props, state) { @@ -71,7 +78,7 @@ export default class KeyboardSpacer extends Component { } componentWillUnmount() { - this._listeners.forEach(listener => listener.remove()); + this._listeners && this._listeners.forEach(listener => listener.remove()); } updateKeyboardSpace(frames) { @@ -93,7 +100,11 @@ export default class KeyboardSpacer extends Component { } render() { + if (Platform.OS === 'android' && !this.props.android) { + return null; + } return ( - ); + + ); } } diff --git a/README.md b/README.md index 3879585..b7526af 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ AppRegistry.registerComponent('DemoApp', () => DemoApp); | :------------ |:---------------:| :---------------:| :-----| | topSpacing | 0 | `number` | Add or subtract additional spacing from keyboard height | | animationConfig | [A default animation](https://github.com/Andr3wHur5t/react-native-keyboard-spacer/blob/expose-layout-animations/KeyboardSpacer.js#L14) | `LayoutAnimationConfig` | [LayoutAnimation](https://facebook.github.io/react-native/docs/layoutanimation.html#content) configuration object | +| android | true | `boolean` | Whether the keyboard should be rendered on android or not (return a view or null) | ### Properties - Methods