|
| 1 | +import { SendIcon } from '@shared/assets/icons'; |
| 2 | +import { useLayoutEffect, useRef, useState, type ChangeEventHandler, type KeyboardEventHandler } from 'react'; |
| 3 | +import { chatInputVariants } from './ChatInput.variants'; |
| 4 | + |
| 5 | +const TEXTAREA_LINE_HEIGHT = 24; |
| 6 | +const TEXTAREA_MAX_LINES = 4; |
| 7 | + |
| 8 | +export interface ChatInputProps { |
| 9 | + placeholder?: string; |
| 10 | + onSend: (message: string) => void; |
| 11 | + value?: string; |
| 12 | + onChange?: (value: string) => void; |
| 13 | +} |
| 14 | + |
| 15 | +export const ChatInput = ({ placeholder, onSend, value, onChange }: ChatInputProps) => { |
| 16 | + const [internalMessage, setInternalMessage] = useState(''); |
| 17 | + const [isFocused, setFocused] = useState(false); |
| 18 | + const textareaRef = useRef<HTMLTextAreaElement | null>(null); |
| 19 | + |
| 20 | + const isControlled = value !== undefined; |
| 21 | + const message = isControlled ? value : internalMessage; |
| 22 | + const trimmedMessage = message.trim(); |
| 23 | + const isFilled = trimmedMessage.length > 0; |
| 24 | + |
| 25 | + const styles = chatInputVariants({ |
| 26 | + state: isFocused ? (isFilled ? 'filled' : 'focused') : isFilled ? 'filled' : 'default', |
| 27 | + sendState: isFilled ? 'active' : 'inactive', |
| 28 | + }); |
| 29 | + |
| 30 | + const handleChange: ChangeEventHandler<HTMLTextAreaElement> = (event) => { |
| 31 | + const nextValue = event.target.value; |
| 32 | + if (isControlled) { |
| 33 | + onChange?.(nextValue); |
| 34 | + } else { |
| 35 | + setInternalMessage(nextValue); |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + const handleSend = () => { |
| 40 | + if (!trimmedMessage) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + onSend(trimmedMessage); |
| 45 | + |
| 46 | + if (isControlled) { |
| 47 | + onChange?.(''); |
| 48 | + } else { |
| 49 | + setInternalMessage(''); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + const handleKeyDown: KeyboardEventHandler<HTMLTextAreaElement> = (event) => { |
| 54 | + if (event.key === 'Enter' && !event.shiftKey && !event.nativeEvent.isComposing) { |
| 55 | + event.preventDefault(); |
| 56 | + handleSend(); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + useLayoutEffect(() => { |
| 61 | + const element = textareaRef.current; |
| 62 | + if (!element) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + element.style.height = 'auto'; |
| 67 | + const maxHeight = TEXTAREA_LINE_HEIGHT * TEXTAREA_MAX_LINES; |
| 68 | + const nextHeight = Math.min(element.scrollHeight, maxHeight); |
| 69 | + element.style.height = `${nextHeight}px`; |
| 70 | + element.style.overflowY = element.scrollHeight > maxHeight ? 'auto' : 'hidden'; |
| 71 | + }, [message]); |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className={styles.root()}> |
| 75 | + <div className={styles.wrapper()}> |
| 76 | + <textarea |
| 77 | + ref={textareaRef} |
| 78 | + value={message} |
| 79 | + onFocus={() => setFocused(true)} |
| 80 | + onBlur={() => setFocused(false)} |
| 81 | + onChange={handleChange} |
| 82 | + className={styles.input()} |
| 83 | + placeholder={placeholder} |
| 84 | + aria-label={placeholder ?? 'Chat input'} |
| 85 | + onKeyDown={handleKeyDown} |
| 86 | + rows={1} |
| 87 | + /> |
| 88 | + <button type="button" className={styles.button()} onClick={handleSend} disabled={!isFilled} aria-label="Send"> |
| 89 | + <SendIcon className={styles.icon()} aria-hidden="true" /> |
| 90 | + </button> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}; |
0 commit comments