-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
106 lines (96 loc) · 2.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React, {Component} from "react";
import {Animated, Easing, Keyboard, StyleSheet} from "react-native";
import {KeyboardAwareScrollView} from "react-native-keyboard-aware-scroll-view";
import {isIos, isX} from "../../utils/common";
import {theme} from "../../constants/Theme";
const withKeyboardAwareScrollView = WrappedComponent => {
return class extends Component {
constructor(props) {
super(props);
this.scroll = React.createRef();
this.keyboardHeight = new Animated.Value(0);
this.scrollToInput = this.scrollToInput.bind(this);
this.handleScroll = this.handleScroll.bind(this);
this.keyboardDidShow = this.keyboardDidShow.bind(this);
this.keyboardDidHide = this.keyboardDidHide.bind(this);
}
scrollToInput(node) {
setTimeout(() => {
this.scroll.props.scrollToFocusedInput(node);
}, 300);
}
keyboardDidShow(event) {
if (!isIos) {
if (event && event.endCoordinates && event.endCoordinates.height) {
const keyboardHeight = event.endCoordinates.height;
Animated.timing(this.keyboardHeight, {
toValue: keyboardHeight,
duration: 200,
easing: Easing.linear,
}).start();
}
}
}
keyboardDidHide() {
if (!isIos) {
Animated.timing(this.keyboardHeight, {
toValue: 0,
duration: 200,
easing: Easing.linear,
}).start();
}
}
handleScroll() {
if (!isIos) {
if (this.keyboardHeight.__getValue() > 0) {
Keyboard.dismiss();
this.keyboardDidHide();
}
}
}
componentDidMount() {
this.keyboardDidShowListener = Keyboard.addListener(
"keyboardDidShow",
this.keyboardDidShow,
);
}
componentWillUnmount() {
this.keyboardDidShowListener.remove();
}
render() {
return (
<KeyboardAwareScrollView
innerRef={ref => {
this.scroll = ref;
}}
style={styles.container}
keyboardDismissMode="on-drag"
keyboardShouldPersistTaps="handled"
contentContainerStyle={{flexGrow: 1}}
onMomentumScrollEnd={event => this.handleScroll(event)}
scrollEventThrottle={16}
enableResetScrollToCoords={false}
>
<Animated.View
style={[styles.container, {paddingBottom: this.keyboardHeight}]}
>
<WrappedComponent
{...this.props}
scrollToInput={this.scrollToInput}
onDropdownClose={this.keyboardDidHide}
onDropdownShow={this.keyboardDidShow}
/>
</Animated.View>
</KeyboardAwareScrollView>
);
}
};
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: theme.backgroundPrimary,
paddingBottom: isX ? 30 : 0,
},
});
export default withKeyboardAwareScrollView;