Skip to content

Commit 8fabf24

Browse files
authored
Merge pull request #139 from manudous/feature/implement-autoScroll
implement-autoScroll
2 parents d8cc293 + 9e7d98b commit 8fabf24

3 files changed

Lines changed: 73 additions & 20 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
3+
export const useAutoScroll = () => {
4+
const [isAutoScrollEnabled, setIsAutoScrollEnabled] = React.useState(true);
5+
const textAreaRef = React.useRef(null);
6+
7+
const doAutoScroll = () => {
8+
if(isAutoScrollEnabled && textAreaRef.current) {
9+
textAreaRef.current.scrollTop = textAreaRef.current.scrollHeight;
10+
}
11+
}
12+
13+
return {isAutoScrollEnabled, setIsAutoScrollEnabled, textAreaRef, doAutoScroll}
14+
}
Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import React from 'react';
22
import TextareaAutosize from '@material-ui/core/TextareaAutosize';
33
import Typography from '@material-ui/core/Typography';
4+
5+
import Checkbox from '@material-ui/core/Checkbox';
6+
import FormControlLabel from '@material-ui/core/FormControlLabel';
7+
48
import * as innerClasses from './student.styles';
9+
import { useAutoScroll } from 'common/hooks/auto-scroll.hook';
510

611
interface Props {
712
room: string;
@@ -11,25 +16,41 @@ interface Props {
1116
export const StudentComponent: React.FC<Props> = props => {
1217
const { room, log } = props;
1318

19+
const {isAutoScrollEnabled, setIsAutoScrollEnabled, textAreaRef, doAutoScroll} = useAutoScroll();
20+
21+
React.useEffect(() => {
22+
doAutoScroll();
23+
}, [log]);
24+
1425
return (
15-
<main className={innerClasses.root}>
16-
<Typography
17-
className={innerClasses.sessionName}
18-
variant="body1"
19-
role="heading"
20-
>
21-
Session name: {room ?? ''}
22-
</Typography>
23-
<label className={innerClasses.label} htmlFor="session">
24-
Content
25-
</label>
26-
<TextareaAutosize
27-
id="session"
28-
rowsMax={30}
29-
rowsMin={30}
30-
className={innerClasses.textarea}
31-
value={log ?? ''}
32-
/>
33-
</main>
26+
27+
<>
28+
<main className={innerClasses.root}>
29+
<Typography className={innerClasses.sessionName} variant="body1" role="heading">
30+
Session name: {room ?? ''}
31+
</Typography>
32+
<label className={innerClasses.label} htmlFor="session">
33+
Content
34+
</label>
35+
<TextareaAutosize
36+
ref={textAreaRef}
37+
id="session"
38+
rowsMax={30}
39+
rowsMin={30}
40+
className={innerClasses.textarea}
41+
value={log ?? ''}
42+
/>
43+
<FormControlLabel
44+
label="Disable AutoScroll"
45+
control={
46+
<Checkbox
47+
checked={isAutoScrollEnabled}
48+
onChange={e => setIsAutoScrollEnabled(e.target.checked)}
49+
color="primary"
50+
/>
51+
}
52+
/>
53+
</main>
54+
</>
3455
);
3556
};

‎front/src/pods/trainer/components/session.component.tsx‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import TextareaAutosize from '@material-ui/core/TextareaAutosize';
44
import ArrowForwardRoundedIcon from '@material-ui/icons/ArrowForwardRounded';
55
import UndoIcon from '@material-ui/icons/Undo';
66
import Button from '@material-ui/core/Button';
7-
import * as innerClasses from './session.styles';
87

8+
import FormControlLabel from '@material-ui/core/FormControlLabel';
9+
import Checkbox from '@material-ui/core/Checkbox';
10+
11+
import * as innerClasses from './session.styles';
12+
import { useAutoScroll } from 'common/hooks/auto-scroll.hook';
913
interface Props {
1014
log: string;
1115
handleSendFullContentLog: (fullContent: string) => void;
@@ -30,8 +34,11 @@ const getFullContent = (currenSessionContent: string) => {
3034
export const SessionComponent: React.FC<Props> = props => {
3135
const { log, handleSendFullContentLog, className } = props;
3236

37+
const {isAutoScrollEnabled, setIsAutoScrollEnabled, textAreaRef, doAutoScroll} = useAutoScroll();
38+
3339
React.useEffect(() => {
3440
handleSetSessionContent(log);
41+
doAutoScroll();
3542
}, [log]);
3643

3744
return (
@@ -41,11 +48,22 @@ export const SessionComponent: React.FC<Props> = props => {
4148
</label>
4249

4350
<TextareaAutosize
51+
ref={textAreaRef}
4452
id="session"
4553
rowsMax={20}
4654
rowsMin={20}
4755
className={innerClasses.textarea}
4856
/>
57+
<FormControlLabel
58+
label="Disable AutoScroll"
59+
control={
60+
<Checkbox
61+
checked={isAutoScrollEnabled}
62+
onChange={e => setIsAutoScrollEnabled(e.target.checked)}
63+
color="primary"
64+
/>
65+
}
66+
/>
4967
<Button
5068
variant="contained"
5169
color="secondary"

0 commit comments

Comments
 (0)