2017-02-17 00:46:39 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
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 => {
|
|
|
|
const { tab, runCommand, sendMessage, addInputHistory, incrementInputHistory,
|
|
|
|
decrementInputHistory, resetInputHistory } = this.props;
|
|
|
|
|
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-04-19 23:51:55 +00:00
|
|
|
runCommand(e.target.value, tab.name, tab.server);
|
|
|
|
} else if (tab.name) {
|
|
|
|
sendMessage(e.target.value, tab.name, tab.server);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addInputHistory(e.target.value);
|
|
|
|
resetInputHistory();
|
|
|
|
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();
|
|
|
|
incrementInputHistory();
|
2017-05-21 21:23:21 +00:00
|
|
|
} else if (e.key === 'ArrowDown') {
|
2015-12-28 23:34:32 +00:00
|
|
|
decrementInputHistory();
|
2017-05-21 21:23:21 +00:00
|
|
|
} else {
|
2015-12-28 23:34:32 +00:00
|
|
|
this.setState({ value: e.target.value });
|
|
|
|
resetInputHistory();
|
|
|
|
}
|
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-05-15 20:51:21 +00:00
|
|
|
const { nick } = this.props;
|
2015-12-28 23:34:32 +00:00
|
|
|
return (
|
|
|
|
<div className="message-input-wrap">
|
2017-05-15 20:51:21 +00:00
|
|
|
<span className="message-input-nick">{nick}</span>
|
2015-12-28 23:34:32 +00:00
|
|
|
<input
|
|
|
|
className="message-input"
|
|
|
|
type="text"
|
|
|
|
value={this.props.history || this.state.value}
|
|
|
|
onKeyDown={this.handleKey}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|