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

69 lines
1.5 KiB
JavaScript
Raw Normal View History

import React, { memo, useState } 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
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
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);
} 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
}
add(e.target.value);
reset();
setValue('');
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) {
setValue(e.target.value);
reset();
2015-12-28 23:34:32 +00:00
}
};
2015-12-28 23:34:32 +00:00
const handleChange = e => setValue(e.target.value);
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>
<input
className="message-input"
type="text"
value={currentHistoryEntry || value}
onKeyDown={handleKey}
onChange={handleChange}
/>
</div>
);
};
export default memo(MessageInput);