2018-12-17 19:48:34 +08:00
|
|
|
import React, { PureComponent } 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 cx from 'classnames';
|
|
|
|
import TextareaAutosize from 'react-autosize-textarea';
|
2019-01-03 23:41:20 +08:00
|
|
|
import browser from 'browser-detect';
|
2018-01-08 14:17:18 +08:00
|
|
|
import { styles } from './styles';
|
2017-04-05 21:21:30 +08:00
|
|
|
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',
|
|
|
|
},
|
2017-06-01 22:24:29 +08:00
|
|
|
errorMinMessageLength: {
|
|
|
|
id: 'app.chat.errorMinMessageLength',
|
|
|
|
},
|
|
|
|
errorMaxMessageLength: {
|
|
|
|
id: 'app.chat.errorMaxMessageLength',
|
|
|
|
},
|
2016-06-03 04:32:42 +08:00
|
|
|
});
|
|
|
|
|
2018-12-17 19:48:34 +08:00
|
|
|
class MessageForm extends PureComponent {
|
2016-06-02 00:33:19 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
message: '',
|
2017-06-01 22:24:29 +08:00
|
|
|
error: '',
|
|
|
|
hasErrors: false,
|
2016-06-02 00:33:19 +08:00
|
|
|
};
|
|
|
|
|
2019-01-03 23:41:20 +08:00
|
|
|
this.BROWSER_RESULTS = browser();
|
|
|
|
|
2016-06-02 00:33:19 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-01-03 23:41:20 +08:00
|
|
|
componentDidMount() {
|
|
|
|
const { mobile } = this.BROWSER_RESULTS;
|
|
|
|
|
|
|
|
if (!mobile) {
|
|
|
|
this.textarea.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const { chatName } = this.props;
|
|
|
|
const { mobile } = this.BROWSER_RESULTS;
|
|
|
|
|
|
|
|
if (prevProps.chatName !== chatName && !mobile) {
|
|
|
|
this.textarea.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 02:52:21 +08:00
|
|
|
handleMessageKeyDown(e) {
|
2017-06-03 03:25:02 +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
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
const event = new Event('submit', {
|
2016-07-08 02:52:21 +08:00
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
});
|
|
|
|
|
2017-06-04 07:58:27 +08:00
|
|
|
this.form.dispatchEvent(event);
|
2016-06-02 00:33:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMessageChange(e) {
|
2017-06-01 22:24:29 +08:00
|
|
|
const { intl } = this.props;
|
|
|
|
|
|
|
|
const message = e.target.value;
|
|
|
|
let error = '';
|
|
|
|
|
2017-06-05 21:52:46 +08:00
|
|
|
const { minMessageLength, maxMessageLength } = this.props;
|
2017-06-01 22:24:29 +08:00
|
|
|
|
|
|
|
if (message.length < minMessageLength) {
|
2018-06-29 01:12:50 +08:00
|
|
|
error = intl.formatMessage(
|
|
|
|
messages.errorMinMessageLength,
|
|
|
|
{ 0: minMessageLength - message.length },
|
|
|
|
);
|
2017-06-01 22:24:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message.length > maxMessageLength) {
|
2018-06-29 01:12:50 +08:00
|
|
|
error = intl.formatMessage(
|
|
|
|
messages.errorMaxMessageLength,
|
|
|
|
{ 0: message.length - maxMessageLength },
|
|
|
|
);
|
2017-06-01 22:24:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
message,
|
|
|
|
error,
|
|
|
|
});
|
2016-06-02 00:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2018-12-29 05:48:59 +08:00
|
|
|
const {
|
|
|
|
disabled, minMessageLength, maxMessageLength, handleSendMessage,
|
|
|
|
} = this.props;
|
|
|
|
const { message } = this.state;
|
|
|
|
let msg = message.trim();
|
2016-06-07 22:19:19 +08:00
|
|
|
|
2019-01-18 21:03:09 +08:00
|
|
|
|
2017-06-01 22:24:29 +08:00
|
|
|
if (disabled
|
2018-12-29 05:48:59 +08:00
|
|
|
|| msg.length === 0
|
|
|
|
|| msg.length < minMessageLength
|
|
|
|
|| msg.length > maxMessageLength) {
|
2017-06-05 21:52:46 +08:00
|
|
|
this.setState({ hasErrors: true });
|
2016-06-07 22:19:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-03 03:08:17 +08:00
|
|
|
// Sanitize. See: http://shebang.brandonmintern.com/foolproof-html-escaping-in-javascript/
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
const div = document.createElement('div');
|
2018-12-29 05:48:59 +08:00
|
|
|
div.appendChild(document.createTextNode(msg));
|
|
|
|
msg = div.innerHTML;
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2019-01-18 21:03:09 +08:00
|
|
|
return (
|
|
|
|
handleSendMessage(msg),
|
|
|
|
this.setState({
|
2017-06-01 22:24:29 +08:00
|
|
|
message: '',
|
|
|
|
hasErrors: false,
|
2019-01-18 21:03:09 +08:00
|
|
|
})
|
|
|
|
);
|
2016-06-02 00:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-06-29 01:12:50 +08:00
|
|
|
const {
|
2018-12-29 05:48:59 +08:00
|
|
|
intl, chatTitle, chatName, disabled, className, chatAreaId,
|
2018-06-29 01:12:50 +08:00
|
|
|
} = this.props;
|
2017-06-01 22:24:29 +08:00
|
|
|
|
2018-12-29 05:48:59 +08:00
|
|
|
const { hasErrors, error, message } = this.state;
|
2016-06-03 04:32:42 +08:00
|
|
|
|
2016-06-02 00:33:19 +08:00
|
|
|
return (
|
|
|
|
<form
|
2017-06-04 07:58:27 +08:00
|
|
|
ref={(ref) => { this.form = ref; }}
|
2018-12-29 05:48:59 +08:00
|
|
|
className={cx(className, styles.form)}
|
2017-06-03 03:25:02 +08:00
|
|
|
onSubmit={this.handleSubmit}
|
|
|
|
>
|
2017-06-01 22:24:29 +08:00
|
|
|
<div className={styles.wrapper}>
|
|
|
|
<TextareaAutosize
|
|
|
|
className={styles.input}
|
|
|
|
id="message-input"
|
2019-01-03 23:41:20 +08:00
|
|
|
innerRef={(ref) => { this.textarea = ref; return this.textarea; }}
|
2017-06-01 22:24:29 +08:00
|
|
|
placeholder={intl.formatMessage(messages.inputPlaceholder, { 0: chatName })}
|
2018-12-29 05:48:59 +08:00
|
|
|
aria-controls={chatAreaId}
|
2017-06-01 22:24:29 +08:00
|
|
|
aria-label={intl.formatMessage(messages.inputLabel, { 0: chatTitle })}
|
2017-06-05 21:52:46 +08:00
|
|
|
aria-invalid={hasErrors ? 'true' : 'false'}
|
|
|
|
aria-describedby={hasErrors ? 'message-input-error' : null}
|
2017-06-01 22:24:29 +08:00
|
|
|
autoCorrect="off"
|
|
|
|
autoComplete="off"
|
|
|
|
spellCheck="true"
|
|
|
|
disabled={disabled}
|
2018-12-29 05:48:59 +08:00
|
|
|
value={message}
|
2017-06-01 22:24:29 +08:00
|
|
|
onChange={this.handleMessageChange}
|
|
|
|
onKeyDown={this.handleMessageKeyDown}
|
|
|
|
/>
|
|
|
|
<Button
|
2017-08-05 01:58:55 +08:00
|
|
|
hideLabel
|
|
|
|
circle
|
2017-06-01 22:24:29 +08:00
|
|
|
className={styles.sendButton}
|
|
|
|
aria-label={intl.formatMessage(messages.submitLabel)}
|
|
|
|
type="submit"
|
|
|
|
disabled={disabled}
|
|
|
|
label={intl.formatMessage(messages.submitLabel)}
|
2017-08-05 01:58:55 +08:00
|
|
|
color="primary"
|
2017-06-01 22:24:29 +08:00
|
|
|
icon="send"
|
|
|
|
onClick={() => null}
|
2017-04-05 21:21:30 +08:00
|
|
|
/>
|
2017-06-01 22:24:29 +08:00
|
|
|
</div>
|
|
|
|
<div className={styles.info}>
|
2019-01-18 21:03:09 +08:00
|
|
|
{hasErrors ? <span id="message-input-error">{error}</span> : null}
|
2017-06-01 22:24:29 +08:00
|
|
|
</div>
|
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);
|