2017-02-17 00:46:39 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2017-06-21 06:40:28 +00:00
|
|
|
import Editable from 'components/ui/Editable';
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-02-17 00:46:39 +00:00
|
|
|
export default class MessageInput extends PureComponent {
|
2015-12-28 23:34:32 +00:00
|
|
|
state = {
|
|
|
|
value: ''
|
2016-01-06 23:28:11 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
|
|
|
|
handleKey = e => {
|
2017-05-27 05:30:22 +00:00
|
|
|
const { tab, onCommand, onMessage,
|
2017-05-26 06:20:00 +00:00
|
|
|
add, reset, increment, decrement, currentHistoryEntry } = this.props;
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-05-21 21:23:21 +00:00
|
|
|
if (e.key === 'Enter' && e.target.value) {
|
2015-12-28 23:34:32 +00:00
|
|
|
if (e.target.value[0] === '/') {
|
2017-05-27 05:30:22 +00:00
|
|
|
onCommand(e.target.value, tab.name, tab.server);
|
2017-04-19 23:51:55 +00:00
|
|
|
} else if (tab.name) {
|
2017-05-27 05:30:22 +00:00
|
|
|
onMessage(e.target.value, tab.name, tab.server);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 06:20:00 +00:00
|
|
|
add(e.target.value);
|
|
|
|
reset();
|
2015-12-28 23:34:32 +00:00
|
|
|
this.setState({ value: '' });
|
2017-05-21 21:23:21 +00:00
|
|
|
} else if (e.key === 'ArrowUp') {
|
2015-12-28 23:34:32 +00:00
|
|
|
e.preventDefault();
|
2017-05-26 06:20:00 +00:00
|
|
|
increment();
|
2017-05-21 21:23:21 +00:00
|
|
|
} else if (e.key === 'ArrowDown') {
|
2017-05-26 06:20:00 +00:00
|
|
|
decrement();
|
|
|
|
} else if (currentHistoryEntry) {
|
2015-12-28 23:34:32 +00:00
|
|
|
this.setState({ value: e.target.value });
|
2017-05-26 06:20:00 +00:00
|
|
|
reset();
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
2016-01-06 23:28:11 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
|
|
|
|
handleChange = e => {
|
|
|
|
this.setState({ value: e.target.value });
|
2016-01-06 23:28:11 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
|
|
|
|
render() {
|
2017-06-21 05:23:07 +00:00
|
|
|
const { nick, currentHistoryEntry, onNickChange, onNickEditDone } = this.props;
|
2015-12-28 23:34:32 +00:00
|
|
|
return (
|
|
|
|
<div className="message-input-wrap">
|
2017-06-21 05:23:07 +00:00
|
|
|
<Editable
|
|
|
|
className="message-input-nick"
|
|
|
|
value={nick}
|
|
|
|
onBlur={onNickEditDone}
|
|
|
|
onChange={onNickChange}
|
|
|
|
>
|
|
|
|
<span className="message-input-nick">{nick}</span>
|
|
|
|
</Editable>
|
2015-12-28 23:34:32 +00:00
|
|
|
<input
|
|
|
|
className="message-input"
|
|
|
|
type="text"
|
2017-05-26 06:20:00 +00:00
|
|
|
value={currentHistoryEntry || this.state.value}
|
2015-12-28 23:34:32 +00:00
|
|
|
onKeyDown={this.handleKey}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|