Spaces:
Sleeping
Sleeping
File size: 1,789 Bytes
fca46f5 | 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 | import React, { useRef, useEffect } from 'react';
import './MessageInput.css';
/**
* MessageInput — text input + send button for the chat.
*
* - Large input field (min 56px height)
* - Prominent send button
* - Submits on Enter (Shift+Enter adds a newline)
* - Disabled while isTyping
* - Auto-focused on mount
*/
function MessageInput({ onSend, isTyping }) {
const inputRef = useRef(null);
// Auto-focus on mount
useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, []);
// Re-focus after response arrives
useEffect(() => {
if (!isTyping && inputRef.current) {
inputRef.current.focus();
}
}, [isTyping]);
const handleSend = () => {
const text = inputRef.current?.value ?? '';
if (!text.trim() || isTyping) return;
onSend(text);
if (inputRef.current) {
inputRef.current.value = '';
}
};
const handleKeyDown = (e) => {
// Enter submits; Shift+Enter adds newline (for textarea)
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSend();
}
};
return (
<div className="message-input-bar">
<label htmlFor="chat-input" className="sr-only">
Type your message
</label>
<input
id="chat-input"
ref={inputRef}
type="text"
className="message-input-field"
placeholder="Type your question here..."
disabled={isTyping}
onKeyDown={handleKeyDown}
autoComplete="off"
aria-label="Type your question here"
/>
<button
className="message-send-btn btn-primary"
onClick={handleSend}
disabled={isTyping}
aria-label="Send message"
>
Send
</button>
</div>
);
}
export default MessageInput;
|