interstellar_ai/app/InputFrontend.tsx

47 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-09-18 14:52:04 +02:00
import React, { useState, ForwardedRef } from 'react';
interface InputProps {
message: string;
onSendClick: (message: string) => void;
onMicClick: () => void;
}
const InputFrontend = React.forwardRef<HTMLDivElement, InputProps>(
({ message, onSendClick, onMicClick }, ref: ForwardedRef<HTMLDivElement>) => {
const [inputValue, setInputValue] = useState('');
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(e.target.value);
};
2024-09-19 16:52:16 +02:00
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter') {
onSendClick(inputValue); // Call the function passed via props
setInputValue(''); // Optionally clear input after submission
event.preventDefault(); // Prevent default action (e.g., form submission)
}
};
2024-09-18 14:52:04 +02:00
return (
2024-09-19 16:52:16 +02:00
<div className="input" id="inputForm" ref={ref}>
2024-09-18 14:52:04 +02:00
<input
type="text"
name="user_message"
placeholder="Type your message here..."
value={inputValue}
onChange={handleInputChange}
2024-09-19 16:52:16 +02:00
onKeyDown={handleKeyDown}
2024-09-18 14:52:04 +02:00
/>
2024-09-19 16:52:16 +02:00
<button type="button" onClick={() => onSendClick(inputValue)}>
2024-09-18 14:52:04 +02:00
<img src="/img/send.svg" alt="send" />
</button>
2024-09-19 16:52:16 +02:00
<button type="button" onClick={onMicClick}>
2024-09-18 14:52:04 +02:00
<img src="/img/microphone.svg" alt="microphone" />
</button>
</div>
);
}
);
export default InputFrontend;