jiti-meet/react/features/chat/components/native/ChatInputBar.js

152 lines
3.8 KiB
JavaScript
Raw Normal View History

// @flow
import React, { Component } from 'react';
import { Platform, TextInput, TouchableOpacity, View } from 'react-native';
2019-11-21 14:26:57 +00:00
import { translate } from '../../../base/i18n';
2019-08-30 16:39:06 +00:00
import { Icon, IconChatSend } from '../../../base/icons';
import BaseTheme from '../../../base/ui/components/BaseTheme.native';
import styles from './styles';
type Props = {
/**
* Callback to invoke on message send.
*/
2019-11-21 14:26:57 +00:00
onSend: Function,
/**
* Function to be used to translate i18n labels.
*/
t: Function
};
type State = {
/**
* Boolean to show if an extra padding needs to be added to the bar.
*/
addPadding: boolean,
/**
* The value of the input field.
*/
2019-08-21 15:47:08 +00:00
message: string,
/**
* Boolean to show or hide the send button.
*/
showSend: boolean
};
/**
* Implements the chat input bar with text field and action(s).
*/
2019-11-21 14:26:57 +00:00
class ChatInputBar extends Component<Props, State> {
/**
* Instantiates a new instance of the component.
*
* @inheritdoc
*/
constructor(props: Props) {
super(props);
this.state = {
addPadding: false,
2019-08-21 15:47:08 +00:00
message: '',
showSend: false
};
this._onChangeText = this._onChangeText.bind(this);
this._onFocused = this._onFocused.bind(this);
this._onSubmit = this._onSubmit.bind(this);
}
/**
* Implements {@code Component#render}.
*
* @inheritdoc
*/
render() {
return (
<View
style = { [
styles.inputBar,
this.state.addPadding ? styles.extraBarPadding : null
] }>
<TextInput
blurOnSubmit = { false }
multiline = { false }
onBlur = { this._onFocused(false) }
onChangeText = { this._onChangeText }
onFocus = { this._onFocused(true) }
onSubmitEditing = { this._onSubmit }
2019-11-21 14:26:57 +00:00
placeholder = { this.props.t('chat.fieldPlaceHolder') }
placeholderTextColor = { BaseTheme.palette.text03 }
returnKeyType = 'send'
selectionColor = { BaseTheme.palette.text03 }
style = { styles.inputField }
value = { this.state.message } />
2019-08-21 15:47:08 +00:00
{
this.state.showSend && <TouchableOpacity onPress = { this._onSubmit }>
<Icon
2019-08-30 16:39:06 +00:00
src = { IconChatSend }
2019-08-21 15:47:08 +00:00
style = { styles.sendButtonIcon } />
</TouchableOpacity>
}
</View>
);
}
_onChangeText: string => void;
/**
* Callback to handle the change of the value of the text field.
*
* @param {string} text - The current value of the field.
* @returns {void}
*/
_onChangeText(text) {
this.setState({
2019-08-21 15:47:08 +00:00
message: text,
showSend: Boolean(text)
});
}
_onFocused: boolean => Function;
/**
* Constructs a callback to be used to update the padding of the field if necessary.
*
* @param {boolean} focused - True of the field is focused.
* @returns {Function}
*/
_onFocused(focused) {
return () => {
Platform.OS === 'android' && this.setState({
addPadding: focused
});
};
}
_onSubmit: () => void;
/**
* Callback to handle the submit event of the text field.
*
* @returns {void}
*/
_onSubmit() {
const message = this.state.message.trim();
message && this.props.onSend(message);
2019-08-21 15:47:08 +00:00
this.setState({
message: '',
showSend: false
});
}
}
2019-11-21 14:26:57 +00:00
export default translate(ChatInputBar);