2018-11-04 06:22:46 +00:00
|
|
|
import React, { memo, useState } from 'react';
|
2018-06-03 04:18:03 +00:00
|
|
|
import classnames from 'classnames';
|
2017-06-21 06:40:28 +00:00
|
|
|
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
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
const MessageInput = ({
|
|
|
|
nick,
|
|
|
|
currentHistoryEntry,
|
|
|
|
onNickChange,
|
|
|
|
onNickEditDone,
|
|
|
|
tab,
|
|
|
|
onCommand,
|
|
|
|
onMessage,
|
|
|
|
add,
|
|
|
|
reset,
|
|
|
|
increment,
|
|
|
|
decrement
|
|
|
|
}) => {
|
|
|
|
const [value, setValue] = useState('');
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
const handleKey = e => {
|
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] === '/') {
|
2020-06-15 08:58:51 +00:00
|
|
|
onCommand(e.target.value, tab.name, tab.network);
|
2017-04-19 23:51:55 +00:00
|
|
|
} else if (tab.name) {
|
2020-06-15 08:58:51 +00:00
|
|
|
onMessage(e.target.value, tab.name, tab.network);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 06:20:00 +00:00
|
|
|
add(e.target.value);
|
|
|
|
reset();
|
2018-11-04 06:22:46 +00:00
|
|
|
setValue('');
|
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) {
|
2018-11-04 06:22:46 +00:00
|
|
|
setValue(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
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
const handleChange = e => setValue(e.target.value);
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2018-11-04 06:22:46 +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>
|
|
|
|
<input
|
|
|
|
className="message-input"
|
|
|
|
type="text"
|
|
|
|
value={currentHistoryEntry || value}
|
|
|
|
onKeyDown={handleKey}
|
|
|
|
onChange={handleChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2018-06-03 04:18:03 +00:00
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
export default memo(MessageInput);
|