bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/chat/message-form/component.jsx

119 lines
2.9 KiB
React
Raw Normal View History

2016-06-02 00:33:19 +08:00
import React, { Component, PropTypes } from 'react';
2016-06-03 04:32:42 +08:00
import { defineMessages, injectIntl } from 'react-intl';
2016-06-02 00:33:19 +08:00
import { findDOMNode } from 'react-dom';
import cx from 'classnames';
import styles from './styles';
import Button from '../../button/component';
import TextareaAutosize from 'react-autosize-textarea';
const propTypes = {
};
const defaultProps = {
};
2016-06-03 04:32:42 +08:00
const messages = defineMessages({
submitLabel: {
id: 'app.chat.submitLabel',
defaultMessage: 'Send Message',
description: 'Chat submit button label',
},
inputLabel: {
id: 'app.chat.inputLabel',
defaultMessage: 'Message input for chat {name}',
description: 'Chat message input label',
},
});
class MessageForm extends Component {
2016-06-02 00:33:19 +08:00
constructor(props) {
super(props);
this.state = {
message: '',
};
this.handleMessageChange = this.handleMessageChange.bind(this);
this.handleMessageKeyUp = this.handleMessageKeyUp.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleMessageKeyUp(e) {
if (e.keyCode === 13 && !e.shiftKey) {
this.refs.btnSubmit.click();
// FIX: I dont know why the live bellow dont trigger the handleSubmit function
// this.refs.form.submit();
}
}
handleMessageChange(e) {
this.setState({ message: e.target.value });
}
handleSubmit(e) {
e.preventDefault();
let message = this.state.message.trim();
// Sanitize. See: http://shebang.brandonmintern.com/foolproof-html-escaping-in-javascript/
let div = document.createElement('div');
div.appendChild(document.createTextNode(message));
message = div.innerHTML;
2016-06-02 00:33:19 +08:00
if (!message) {
return;
}
this.setState({ message: '' });
2016-06-03 02:40:27 +08:00
this.props.handleSendMessage(message);
2016-06-02 00:33:19 +08:00
}
render() {
2016-06-03 04:32:42 +08:00
const { intl, chatTitle } = this.props;
2016-06-02 00:33:19 +08:00
return (
<form
{...this.props}
ref="form"
className={cx(this.props.className, styles.form)}
onSubmit={this.handleSubmit}>
<div className={styles.actions}>
<Button
onClick={() => alert('Not supported yet...')}
icon={'circle-add'}
size={'sm'}
circle={true}
/>
</div>
<TextareaAutosize
className={styles.input}
id="message-input"
maxlength=""
aria-controls={this.props.chatAreaId}
2016-06-03 04:32:42 +08:00
aria-label={ intl.formatMessage(messages.inputLabel, { name: chatTitle }) }
2016-06-02 00:33:19 +08:00
autocorrect="off"
autocomplete="off"
spellcheck="true"
value={this.state.message}
onChange={this.handleMessageChange}
onKeyUp={this.handleMessageKeyUp}
/>
2016-06-03 04:32:42 +08:00
<input
ref="btnSubmit"
className={'sr-only'}
type="submit"
value={ intl.formatMessage(messages.submitLabel) }
/>
2016-06-02 00:33:19 +08:00
</form>
);
}
}
MessageForm.propTypes = propTypes;
MessageForm.defaultProps = defaultProps;
2016-06-03 04:32:42 +08:00
export default injectIntl(MessageForm);