forked from MaheshNandam/react-native-whatsapp-textinput
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (120 loc) · 4.62 KB
/
index.js
File metadata and controls
129 lines (120 loc) · 4.62 KB
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
129
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import { View, TextInput, Image, Animated, Keyboard, StyleSheet, TouchableOpacity, Platform } from 'react-native';
import DeviceInfo from "react-native-device-info";
export default class WhatsAppTextInput extends Component {
constructor(props) {
super(props);
this.state = {
height: 0,
keyboardOffset: new Animated.Value(0)
}
}
componentDidMount() {
this._keyboardWillShowSubscription = Keyboard.addListener("keyboardWillShow", e => this._keyboardWillShow(e));
this._keyboardWillHideSubscription = Keyboard.addListener("keyboardWillHide", e => this._keyboardWillHide(e));
}
_keyboardWillShow(e) {
Animated.spring(this.state.keyboardOffset, {
toValue: DeviceInfo.getModel() === "iPhone X" ? e.endCoordinates.height - 34 : e.endCoordinates.height,
friction: 8
}).start();
}
_keyboardWillHide(e) {
Animated.spring(this.state.keyboardOffset, {
toValue: 0,
friction: 8
}).start();
}
render() {
return (
<Animated.View style={{ marginBottom: this.state.keyboardOffset }}>
<View style={[styles.textInputParentView, {
borderTopColor: this.props.borderTopColor,
backgroundColor: this.props.backgroundColor,
}]}>
<View style={styles.textInputView}>
<TextInput
editable={this.props.editable}
multiline={this.props.multiline}
placeholder={this.props.placeholderText}
placeholderTextColor={this.props.placeholderTextColor}
placeholderStyle={[styles.placeholderStyle, { color: this.props.placeholderTextColor }]}
underlineColorAndroid='transparent'
keyboardType={this.props.keyboardType}
value={this.props.messageText}
onChange={(event)=> { event.target.value }}
onChangeText={editedText => {
this.props.onChange(editedText)
}}
onContentSizeChange={(event) => this.setState({ height: event.nativeEvent.contentSize.height })}
style={[styles.textInputStyle, {
height: Math.min(120, Math.max(35, this.state.height)),
backgroundColor: this.props.textInputBgColor,
color: this.props.messageTextColor
}]}
/>
</View>
<TouchableOpacity
disabled={this.props.validateButton()}
onPress={() => this.props.onPressButton()}>
<View style={styles.buttonPosition}>
<View style={[styles.sendButtonStyle, {
backgroundColor: this.props.validateButton() == true ? this.props.sendButtonDisableColor : this.props.sendButtonEnableColor
}]}>
<Image style={{ width: 30, height: 30 }} source={this.props.sendButtonImage} />
</View>
</View>
</TouchableOpacity>
</View>
</Animated.View>
);
}
}
const styles = StyleSheet.create({
textInputParentView: {
flexDirection: 'row',
paddingHorizontal: 10,
borderTopWidth: 1,
paddingVertical: 5,
},
textInputView: {
flex: 1,
marginRight: 15,
justifyContent: 'center',
},
textInputStyle: {
fontSize: 14,
overflow: 'hidden',
justifyContent: 'center',
alignItems: 'center',
flexWrap: 'wrap',
paddingLeft: 10,
paddingTop: 8,
textAlign: 'left',
borderRadius: 5,
},
buttonPosition: {
justifyContent: 'flex-end',
flex: 1,
...Platform.select({ android: { marginVertical: 1 } })
},
sendButtonStyle: {
paddingVertical: 15,
paddingLeft: 20,
paddingRight: 15,
justifyContent: 'center',
alignItems: 'center',
width: 36,
height: 36,
borderRadius: 18,
},
placeholderStyle: {
fontSize: 12,
textAlignVertical: 'center'
}
});