Rocket.Chat.ReactNative/app/components/MessageBox.js

103 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-08-09 13:12:00 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import { View, TextInput, StyleSheet } from 'react-native';
2017-08-10 20:09:54 +00:00
import Icon from 'react-native-vector-icons/MaterialIcons';
import ImagePicker from 'react-native-image-picker';
import RocketChat from '../lib/rocketchat';
2017-08-09 13:12:00 +00:00
const styles = StyleSheet.create({
textBox: {
paddingTop: 1,
2017-08-10 20:09:54 +00:00
borderTopWidth: 1,
borderTopColor: '#ccc',
backgroundColor: '#fff',
flexDirection: 'row',
alignItems: 'center'
2017-08-09 13:12:00 +00:00
},
textBoxInput: {
height: 40,
2017-08-11 18:18:09 +00:00
alignSelf: 'stretch',
2017-08-09 13:12:00 +00:00
backgroundColor: '#fff',
2017-08-10 20:09:54 +00:00
flexGrow: 1
},
fileButton: {
color: '#aaa',
paddingLeft: 23,
paddingRight: 20,
paddingTop: 10,
paddingBottom: 10,
fontSize: 20
2017-08-09 13:12:00 +00:00
}
});
export default class MessageBox extends React.PureComponent {
static propTypes = {
2017-08-10 20:09:54 +00:00
onSubmit: PropTypes.func.isRequired,
rid: PropTypes.string.isRequired
2017-08-09 13:12:00 +00:00
}
constructor(props) {
super(props);
2017-08-17 16:55:47 +00:00
// this._textInput.setNativeProps({ text: '' });
2017-08-09 13:12:00 +00:00
}
2017-08-11 18:18:09 +00:00
submit(message) {
// console.log(this.state);
const text = message;
if (text.trim() === '') {
2017-08-09 13:12:00 +00:00
return;
}
2017-08-17 16:55:47 +00:00
this.component && this.component.setNativeProps({ text: '' });
2017-08-11 18:18:09 +00:00
this.props.onSubmit(text);
}
2017-08-09 13:12:00 +00:00
2017-08-10 20:09:54 +00:00
addFile = () => {
const options = {
customButtons: [{
name: 'import', title: 'Import File From'
}]
};
ImagePicker.showImagePicker(options, (response) => {
// console.log('Response = ', response);
2017-08-10 20:09:54 +00:00
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
const fileInfo = {
name: response.fileName,
size: response.fileSize,
type: response.type || 'image/jpeg',
// description: '',
store: 'Uploads'
};
RocketChat.sendFileMessage(this.props.rid, fileInfo, response.data);
2017-08-10 20:09:54 +00:00
}
});
}
2017-08-09 13:12:00 +00:00
render() {
return (
<View style={styles.textBox}>
2017-08-10 20:09:54 +00:00
<Icon style={styles.fileButton} name='add-circle-outline' onPress={this.addFile} />
2017-08-09 13:12:00 +00:00
<TextInput
2017-08-11 18:18:09 +00:00
ref={component => this.component = component}
2017-08-09 13:12:00 +00:00
style={styles.textBoxInput}
2017-08-17 16:55:47 +00:00
// value={this.state.text}
// onChangeText={text => this.setState({ text })}
2017-08-09 13:12:00 +00:00
returnKeyType='send'
2017-08-11 18:18:09 +00:00
onSubmitEditing={event => this.submit(event.nativeEvent.text)}
2017-08-09 13:12:00 +00:00
blurOnSubmit={false}
placeholder='New message'
2017-08-11 18:18:09 +00:00
underlineColorAndroid='transparent'
2017-08-17 16:55:47 +00:00
defaultValue={''}
2017-08-09 13:12:00 +00:00
/>
</View>
);
}
}