react-icons/fa#FaPaperPlane JavaScript Examples

The following examples show how to use react-icons/fa#FaPaperPlane. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.js    From hackchat-client with Do What The F*ck You Want To Public License 4 votes vote down vote up
export function ChatInput({ channel, onSendChat }) {
  const [chatInputField, setChatInput] = useStateIfMounted();
  const [msgHistory, updateMsgHistory] = useStateIfMounted([]);
  const [searchIndex, setSearchIndex] = useStateIfMounted(0);

  useEffect(() => {
    const listener = (evt) => {
      if (evt.code === 'Enter' || evt.code === 'NumpadEnter') {
        if (!evt.shiftKey) {
          submitInput(evt);
          chatInputField.style.height = 'auto';
        } else {
          chatInputField.style.height = 'inherit';
          chatInputField.style.height = `${chatInputField.scrollHeight}px`;
        }
      } else if (evt.code === 'ArrowUp' && chatInputField) {
        if (chatInputField.selectionStart !== 0) return;

        if (searchIndex === 0 && chatInputField.value) {
          updateMsgHistory(
            [chatInputField.value].concat(msgHistory.slice(0, MaxMsgHistory)),
          );
        }

        setSearchIndex(incSearchIndex(searchIndex, msgHistory.length));
        chatInputField.value = msgHistory[searchIndex] || '';
      } else if (evt.code === 'ArrowDown' && chatInputField) {
        if (chatInputField.selectionStart !== chatInputField.value.length)
          return;

        setSearchIndex(decSearchIndex(searchIndex));
        chatInputField.value = msgHistory[searchIndex] || '';
      }
    };

    document.addEventListener('keydown', listener);
    return () => {
      document.removeEventListener('keydown', listener);
    };
  }, [chatInputField, searchIndex, msgHistory]);

  const submitInput = (evt) => {
    if (evt) {
      if (evt.preventDefault) evt.preventDefault();
      if (evt.stopPropagation) evt.stopPropagation();
      if (evt.nativeEvent && evt.nativeEvent.stopImmediatePropagation)
        evt.nativeEvent.stopImmediatePropagation();
    }

    const chatText = chatInputField.value;
    if (chatText) {
      onSendChat(channel, chatText);
      chatInputField.value = '';
      if (searchIndex !== 0) {
        setSearchIndex(0);
      }
      updateMsgHistory([chatText].concat(msgHistory.slice(0, MaxMsgHistory)));
    }
  };

  const onUserInput = (evt) => {
    const { target } = evt;
    setChatInput(target);
    if (target.value) {
      target.style.height = 'inherit';
      target.style.height = `${target.scrollHeight + 2}px`;
    } else {
      target.style.height = 'auto';
    }
  };

  const inputDom = (
    <Form onSubmit={submitInput}>
      <InputGroup size="sm">
        <FormattedMessage
          id={messages.MainInput.id}
          defaultMessage={messages.MainInput.defaultMessage}
        >
          {(placeholder) => (
            <UserInput
              autoFocus
              rows="1"
              onChange={onUserInput}
              type="textarea"
              placeholder={placeholder}
            />
          )}
        </FormattedMessage>

        <InputGroupAddon className="d-flex" addonType="append">
          <SendButton type="submit">
            <FaPaperPlane />
          </SendButton>
        </InputGroupAddon>
      </InputGroup>
    </Form>
  );

  return inputDom;
}