-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathnew-text.component.tsx
More file actions
94 lines (86 loc) · 2.61 KB
/
new-text.component.tsx
File metadata and controls
94 lines (86 loc) · 2.61 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 React from 'react';
import * as classes from './new-text.styles';
// Material UI ~ components
import Button from '@material-ui/core/Button';
import TextareaAutosize from '@material-ui/core/TextareaAutosize';
// Code Editor
import AceEditor from 'react-ace';
import 'ace-builds/src-noconflict/mode-typescript';
import 'ace-builds/src-noconflict/theme-monokai';
interface Props {
handleAppendTrainerText: (trainerText: string) => void;
}
export const NewTextComponent: React.FC<Props> = props => {
const { handleAppendTrainerText } = props;
const [trainerText, setTrainerText] = React.useState<string>('');
const {
newTextContainer,
labelTextarea,
editTextArea,
sendBtn,
sendBtnDisabled,
} = classes;
const trainerTextRef = React.useRef<string>(trainerText);
const handleAppendTrainerTextInternal = (): void => {
handleAppendTrainerText(trainerTextRef.current);
setTrainerText('');
trainerTextRef.current = '';
};
const handleOnChange = (
value: string,
e: React.ChangeEvent<HTMLTextAreaElement>
): void => {
trainerTextRef.current = value;
setTrainerText(value);
};
React.useEffect(() => {
const listener = (e: KeyboardEvent) => {
if (e.key === 'Enter' && e.ctrlKey && Boolean(trainerTextRef.current)) {
handleAppendTrainerTextInternal();
}
};
window.addEventListener('keypress', listener);
return () => window.removeEventListener('keypress', listener);
}, []);
return (
<div className={newTextContainer}>
<form>
<label className={labelTextarea} htmlFor="new-text">
New text
</label>
<AceEditor
placeholder=""
mode="typescript"
theme="monokai"
name="blah2"
onChange={(value, e) => handleOnChange(value, e)}
fontSize={14}
showPrintMargin={true}
showGutter={true}
highlightActiveLine={true}
value={trainerText}
setOptions={{
enableBasicAutocompletion: false,
enableLiveAutocompletion: true,
enableSnippets: false,
showLineNumbers: true,
tabSize: 2,
showPrintMargin: false,
}}
className={editTextArea}
width="auto"
/>
<Button
variant="contained"
color="primary"
className={trainerText ? sendBtn : sendBtnDisabled}
onClick={() => trainerText && handleAppendTrainerTextInternal()}
aria-disabled={!trainerText}
disableRipple={!trainerText}
>
Send
</Button>
</form>
</div>
);
};