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