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';
|
|
|
|
|
|
|
|
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);
|
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-07-08 02:52:21 +08:00
|
|
|
handleMessageKeyDown(e) {
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-03 03:08:17 +08:00
|
|
|
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() {
|
2016-06-07 22:19:19 +08:00
|
|
|
const { intl, chatTitle, 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-06-14 01:00:38 +08:00
|
|
|
<MessageFormActions
|
|
|
|
onClick={() => alert('Not supported yet...')}
|
|
|
|
className={styles.actions}
|
|
|
|
disabled={disabled}
|
2016-07-08 02:52:21 +08:00
|
|
|
label={'More actions'}
|
2016-06-14 01:00:38 +08:00
|
|
|
/>
|
2016-06-02 00:33:19 +08:00
|
|
|
<TextareaAutosize
|
|
|
|
className={styles.input}
|
|
|
|
id="message-input"
|
|
|
|
aria-controls={this.props.chatAreaId}
|
2016-06-03 04:32:42 +08:00
|
|
|
aria-label={ intl.formatMessage(messages.inputLabel, { name: 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
|
|
|
/>
|
2016-06-03 04:32:42 +08:00
|
|
|
<input
|
|
|
|
ref="btnSubmit"
|
|
|
|
className={'sr-only'}
|
|
|
|
type="submit"
|
2016-06-07 22:19:19 +08:00
|
|
|
disabled={disabled}
|
2016-06-03 04:32:42 +08:00
|
|
|
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);
|