Skip to content

Commit

Permalink
Merge pull request #4 from RSSfeed/fix-android
Browse files Browse the repository at this point in the history
fix android adjustresize and ios when used with react navigation
  • Loading branch information
ardaogulcan authored Mar 14, 2018
2 parents ec7c03e + fbab087 commit 3c1917d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import KeyboardAccessoryView from './lib/KeyboardAccessoryView';
import KeyboardAccessoryNavigation from './lib/KeyboardAccessoryNavigation';
import KeyboardAwareTabBarComponent from './lib/KeyboardAwareTabBarComponent';

export {
KeyboardAccessoryView,
KeyboardAccessoryNavigation,
KeyboardAwareTabBarComponent
}
59 changes: 44 additions & 15 deletions lib/KeyboardAccessoryView.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ const accessoryAnimation = (duration, easing, animationConfig = null) => {

if (Platform.OS === 'android') {
return {
duration: 200,
create: {
duration: 200,
type: LayoutAnimation.Types.linear,
property: LayoutAnimation.Properties.opacity
},
update: {
type: LayoutAnimation.Types.linear,
}
}
duration: 200,
create: {
duration: 200,
type: LayoutAnimation.Types.linear,
property: LayoutAnimation.Properties.opacity
},
update: {
type: LayoutAnimation.Types.linear,
}
}
}

return LayoutAnimation.create(
Expand Down Expand Up @@ -79,12 +79,34 @@ class KeyboardAccessoryView extends Component {
return;
}

const { animationConfig, animateOn } = this.props;
const keyboardHeight = Platform.select({
ios: keyboardEvent.endCoordinates.height,
android: this.props.androidWindowSoftInputAdjustResize
? this.props.bumperHeight
: keyboardEvent.endCoordinates.height
});

if (animateOn === 'all' || Platform.OS === animateOn) {
LayoutAnimation.configureNext(
accessoryAnimation(keyboardEvent.duration, keyboardEvent.easing, animationConfig)
);
const keyboardAnimate = () => {
const { animationConfig, animateOn } = this.props;

if (animateOn === 'all' || Platform.OS === animateOn) {
LayoutAnimation.configureNext(
accessoryAnimation(keyboardEvent.duration, keyboardEvent.easing, animationConfig)
);
}

this.setState({
isKeyboardVisible: true,
keyboardHeight: keyboardHeight
})
};

if (Platform.OS === 'ios' || typeof this.props.onKeyboardShowDelay !== 'number') {
keyboardAnimate();
} else {
setTimeout(() => {
keyboardAnimate()
}, this.props.onKeyboardShowDelay);
}

this.setState({
Expand Down Expand Up @@ -113,6 +135,7 @@ class KeyboardAccessoryView extends Component {
render() {
const { isKeyboardVisible , accessoryHeight, keyboardHeight } = this.state;
const { bumperHeight, alwaysVisible, visibleOpacity, hiddenOpacity, hideBorder, style } = this.props;

return (
<View style={{ height: (isKeyboardVisible || alwaysVisible ? accessoryHeight : 0) }}>
<View style={[
Expand Down Expand Up @@ -144,6 +167,11 @@ KeyboardAccessoryView.propTypes = {
bumperHeight: PropTypes.number,
visibleOpacity: PropTypes.number,
hiddenOpacity: PropTypes.number,
onKeyboardShowDelay: PropTypes.oneOfType([
PropTypes.number,
PropTypes.bool
]),
androidWindowSoftInputAdjustResize: PropTypes.bool,
alwaysVisible: PropTypes.bool,
hideBorder: PropTypes.bool,
}
Expand All @@ -153,6 +181,7 @@ KeyboardAccessoryView.defaultProps = {
bumperHeight: 15,
visibleOpacity: 1,
hiddenOpacity: 0,
androidWindowSoftInputAdjustResize: false,
alwaysVisible: false,
hideBorder: false,
}
Expand Down
60 changes: 60 additions & 0 deletions lib/KeyboardAwareTabBarComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react'
import PropTypes from 'prop-types';
import { Keyboard, Platform } from 'react-native'
import { TabBarBottom } from 'react-navigation'

export default class KeyboardAwareTabBarComponent extends React.PureComponent {

constructor(props) {
super(props);

this.keyboardWillShow = this.keyboardWillShow.bind(this);
this.keyboardWillHide = this.keyboardWillHide.bind(this);

this.state = {
isVisible: true
}
}

componentWillMount() {
const keyboardShowEvent = Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow';
const keyboardHideEvent = Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide';

this.keyboardWillShowSub = Keyboard.addListener(keyboardShowEvent, this.keyboardWillShow);
this.keyboardWillHideSub = Keyboard.addListener(keyboardHideEvent, this.keyboardWillHide);
}

componentWillUnmount() {
this.keyboardWillShowSub.remove();
this.keyboardWillHideSub.remove();
}

keyboardWillShow(event) {
this.setState({
isVisible: false
})
}

keyboardWillHide(event) {
this.setState({
isVisible: true
})
}

render() {
const TabBarComponent = this.props.tabBarComponent;

return this.state.isVisible
? <TabBarComponent {...this.props} />
: null
}
}

KeyboardAwareTabBarComponent.propTypes = {
tabBarComponent: PropTypes.func
};

KeyboardAwareTabBarComponent.defaultProps = {
tabBarComponent: TabBarBottom
};

0 comments on commit 3c1917d

Please sign in to comment.