dispatch/client/src/js/components/pages/Chat/MessageInput.js

79 lines
1.8 KiB
JavaScript
Raw Normal View History

import React, { PureComponent } from 'react';
import classnames from 'classnames';
import Editable from 'components/ui/Editable';
2018-05-25 02:12:02 +00:00
import { isValidNick } from 'utils';
2015-12-28 23:34:32 +00:00
export default class MessageInput extends PureComponent {
2015-12-28 23:34:32 +00:00
state = {
value: ''
};
2015-12-28 23:34:32 +00:00
handleKey = e => {
2018-04-05 23:46:22 +00:00
const {
tab,
onCommand,
onMessage,
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] === '/') {
onCommand(e.target.value, tab.name, tab.server);
} else if (tab.name) {
onMessage(e.target.value, tab.name, tab.server);
2015-12-28 23:34:32 +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();
increment();
2017-05-21 21:23:21 +00:00
} else if (e.key === 'ArrowDown') {
decrement();
} else if (currentHistoryEntry) {
2015-12-28 23:34:32 +00:00
this.setState({ value: e.target.value });
reset();
2015-12-28 23:34:32 +00:00
}
};
2015-12-28 23:34:32 +00:00
handleChange = e => {
this.setState({ value: e.target.value });
};
2015-12-28 23:34:32 +00:00
render() {
2018-04-05 23:46:22 +00:00
const {
nick,
currentHistoryEntry,
onNickChange,
onNickEditDone
} = this.props;
2015-12-28 23:34:32 +00:00
return (
<div className="message-input-wrap">
<Editable
className={classnames('message-input-nick', {
invalid: !isValidNick(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"
value={currentHistoryEntry || this.state.value}
2015-12-28 23:34:32 +00:00
onKeyDown={this.handleKey}
onChange={this.handleChange}
/>
</div>
);
}
}