-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathInputAccessory.js
128 lines (121 loc) · 3.09 KB
/
InputAccessory.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
import React, { Component } from 'react';
import {
Dimensions,
TouchableOpacity,
LayoutAnimation,
StyleSheet,
Keyboard,
View,
Text,
} from 'react-native';
// --------------------------------------------------
const SCREEN_SIZE = Dimensions.get('window');
const INPUT_ACCESSORY_HEIGHT = 40;
// --------------------------------------------------
export default class InputAccessory extends Component {
state = {
visibleHeight: SCREEN_SIZE.height,
opacity: 0,
}
// --------------------------------------------------
componentWillMount() {
this.keyboardDidShowListener =
Keyboard.addListener('keyboardWillShow', this.keyboardWillShow.bind(this));
this.keyboardDidHideListener =
Keyboard.addListener('keyboardWillHide', this.keyboardWillHide.bind(this));
}
componentWillUnmount() {
this.keyboardDidShowListener.remove();
this.keyboardDidHideListener.remove();
const newSize = SCREEN_SIZE.height;
this.setState({
visibleHeight: newSize,
opacity: 0,
});
}
// --------------------------------------------------
keyboardWillShow(e) {
const newSize = e.endCoordinates.screenY - (INPUT_ACCESSORY_HEIGHT - 1);
LayoutAnimation.configureNext({
duration: 500,
create: {
type: LayoutAnimation.Types.linear,
property: LayoutAnimation.Properties.scaleXY,
},
update: {
type: LayoutAnimation.Types.linear,
property: LayoutAnimation.Properties.scaleXY,
},
});
this.setState({
visibleHeight: newSize,
opacity: 1,
});
}
keyboardWillHide() {
this.setState({
visibleHeight: SCREEN_SIZE.height,
opacity: 0,
});
}
dismissKeyboardHandler() {
LayoutAnimation.configureNext({
duration: 100,
create: {
type: LayoutAnimation.Types.linear,
},
update: {
type: LayoutAnimation.Types.linear,
},
});
this.setState({
visibleHeight: SCREEN_SIZE.height,
opacity: 0,
});
Keyboard.dismiss();
}
// --------------------------------------------------
render() {
return (
<View
style={[
styles.InputAccessory,
{ opacity: this.state.opacity, top: this.state.visibleHeight - 1 },
]}
>
<TouchableOpacity
style={{ backgroundColor: '#f000' }}
onPress={() => this.dismissKeyboardHandler()}
>
<Text style={[styles.InputAccessoryButtonText]}>
{'Đóng bàn phím'}
</Text>
</TouchableOpacity>
</View>
);
}
}
// --------------------------------------------------
const styles = StyleSheet.create({
InputAccessory: {
position: 'absolute',
left: 0,
right: 0,
justifyContent: 'center',
alignItems: 'flex-end',
height: INPUT_ACCESSORY_HEIGHT,
backgroundColor: '#fff',
},
InputAccessoryButtonText: {
paddingHorizontal: 9,
paddingVertical: 12,
marginLeft: 12,
marginRight: 8,
letterSpacing: 0.5,
fontSize: 16,
fontWeight: '400',
color: '#316b6f',
backgroundColor: 'transparent',
},
});