-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathQuestionInput.tsx
73 lines (62 loc) · 2.27 KB
/
QuestionInput.tsx
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
import { useState, useEffect } from "react";
import { Stack, TextField } from "@fluentui/react";
import { Button, Tooltip } from "@fluentui/react-components";
import { Send28Filled } from "@fluentui/react-icons";
import styles from "./QuestionInput.module.css";
import { SpeechInput } from "./SpeechInput";
interface Props {
onSend: (question: string) => void;
disabled: boolean;
initQuestion?: string;
placeholder?: string;
clearOnSend?: boolean;
}
export const QuestionInput = ({ onSend, disabled, placeholder, clearOnSend, initQuestion }: Props) => {
const [question, setQuestion] = useState<string>("");
useEffect(() => {
initQuestion && setQuestion(initQuestion);
}, [initQuestion]);
const sendQuestion = () => {
if (disabled || !question.trim()) {
return;
}
onSend(question);
if (clearOnSend) {
setQuestion("");
}
};
const onEnterPress = (ev: React.KeyboardEvent<Element>) => {
if (ev.key === "Enter" && !ev.shiftKey) {
ev.preventDefault();
sendQuestion();
}
};
const onQuestionChange = (_ev: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue?: string) => {
if (!newValue) {
setQuestion("");
} else if (newValue.length <= 1000) {
setQuestion(newValue);
}
};
const sendQuestionDisabled = disabled || !question.trim();
return (
<Stack horizontal className={styles.questionInputContainer}>
<TextField
className={styles.questionInputTextArea}
placeholder={placeholder}
multiline
resizable={false}
borderless
value={question}
onChange={onQuestionChange}
onKeyDown={onEnterPress}
/>
<div className={styles.questionInputButtonsContainer}>
<Tooltip content="Submit question" relationship="label">
<Button size="large" icon={<Send28Filled primaryFill="rgba(115, 118, 225, 1)" />} disabled={sendQuestionDisabled} onClick={sendQuestion} />
</Tooltip>
</div>
<SpeechInput updateQuestion={setQuestion} />
</Stack>
);
};