Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add android Prop #25

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions KeyboardSpacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Keyboard,
LayoutAnimation,
View,
ViewPropTypes,
Platform,
StyleSheet
} from 'react-native';
Expand All @@ -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 = {
Expand All @@ -42,6 +44,7 @@ export default class KeyboardSpacer extends Component {
}
},
onToggle: () => null,
android: true,
};

constructor(props, context) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -93,7 +100,11 @@ export default class KeyboardSpacer extends Component {
}

render() {
if (Platform.OS === 'android' && !this.props.android) {
return null;
}
return (
<View style={[styles.container, { height: this.state.keyboardSpace }, this.props.style]} />);
<View style={[styles.container, { height: this.state.keyboardSpace }, this.props.style]} />
);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down