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

138 lines
3.4 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';
2016-06-14 01:00:38 +08:00
import MessageFormActions from './message-form-actions/component';
2016-06-02 00:33:19 +08:00
import TextareaAutosize from 'react-autosize-textarea';
import Button from '../../button/component';
2016-06-02 00:33:19 +08:00
const propTypes = {
};
const defaultProps = {
};
2016-06-03 04:32:42 +08:00
const messages = defineMessages({
submitLabel: {
id: 'app.chat.submitLabel',
description: 'Chat submit button label',
},
inputLabel: {
id: 'app.chat.inputLabel',
description: 'Chat message input label',
},
2016-12-07 01:07:22 +08:00
inputPlaceholder: {
id: 'app.chat.inputPlaceholder',
description: 'Chat message input placeholder',
},
2016-06-03 04:32:42 +08:00
});
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);
2016-07-08 02:52:21 +08:00
this.handleMessageKeyDown = this.handleMessageKeyDown.bind(this);
2016-06-02 00:33:19 +08:00
this.handleSubmit = this.handleSubmit.bind(this);
2016-06-02 00:33:19 +08:00
}
2016-07-08 02:52:21 +08:00
handleMessageKeyDown(e) {
2017-04-05 22:23:05 +08:00
2017-04-10 23:50:03 +08:00
//TODO Prevent send message pressing enter on mobile and/or virtual keyboard
2016-06-02 00:33:19 +08:00
if (e.keyCode === 13 && !e.shiftKey) {
2016-07-08 02:52:21 +08:00
e.preventDefault();
2016-06-02 00:33:19 +08:00
2016-07-08 02:52:21 +08:00
let event = new Event('submit', {
bubbles: true,
cancelable: true,
});
this.refs.form.dispatchEvent(event);
2016-06-02 00:33:19 +08:00
}
}
handleMessageChange(e) {
this.setState({ message: e.target.value });
}
handleSubmit(e) {
e.preventDefault();
2016-06-07 22:19:19 +08:00
const { disabled } = this.props;
if (disabled) {
return false;
}
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
2016-07-08 02:52:21 +08:00
if (message) {
this.props.handleSendMessage(message);
2016-06-02 00:33:19 +08:00
}
this.setState({ message: '' });
}
render() {
const { intl, chatTitle, chatName, disabled } = this.props;
2016-06-03 04:32:42 +08:00
2016-06-02 00:33:19 +08:00
return (
<form
ref="form"
className={cx(this.props.className, styles.form)}
onSubmit={this.handleSubmit}>
2016-12-07 01:07:22 +08:00
{
// <MessageFormActions
// onClick={() => alert('Not supported yet...')}
// className={styles.actions}
// disabled={disabled}
// label={'More actions'}
// />
}
2016-06-02 00:33:19 +08:00
<TextareaAutosize
className={styles.input}
id="message-input"
2017-05-17 08:00:16 +08:00
placeholder={intl.formatMessage(messages.inputPlaceholder, { 0: chatName })}
2016-06-02 00:33:19 +08:00
aria-controls={this.props.chatAreaId}
2017-05-17 08:00:16 +08:00
aria-label={intl.formatMessage(messages.inputLabel, { 0: chatTitle })}
2016-07-12 04:10:55 +08:00
autoCorrect="off"
autoComplete="off"
spellCheck="true"
2016-06-07 22:19:19 +08:00
disabled={disabled}
2016-06-02 00:33:19 +08:00
value={this.state.message}
onChange={this.handleMessageChange}
2016-07-08 02:52:21 +08:00
onKeyDown={this.handleMessageKeyDown}
2016-06-02 00:33:19 +08:00
/>
<Button
className={styles.sendButton}
aria-label={intl.formatMessage(messages.submitLabel)}
2016-06-03 04:32:42 +08:00
type="submit"
2016-06-07 22:19:19 +08:00
disabled={disabled}
label={intl.formatMessage(messages.submitLabel)}
hideLabel={true}
icon={"send"}
2017-04-05 22:23:05 +08:00
onClick={()=>{}}
/>
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);