-
Notifications
You must be signed in to change notification settings - Fork 9
/
script-text-input.js
154 lines (145 loc) · 4.72 KB
/
script-text-input.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// https://github.com/EnCiv/undebate-ssp/issues/10
import React, { useState, useEffect, useRef } from 'react'
import { createUseStyles } from 'react-jss'
import TextareaAutosize from 'react-textarea-autosize'
import moment from 'moment'
import cx from 'classnames'
import SvgVideo from '../svgr/video'
import SvgClock from '../svgr/clock'
export default function ScriptTextInput({
questionNumber,
questionName,
maxWordCount = 600,
wordsPerMinute = 120,
defaultValue,
onDone,
disabled,
}) {
const classes = useStyles()
const inputRef = useRef(null)
const getWordCount = inputText => {
if (!inputText) return 0
return inputText.split(' ').filter(word => {
return word !== '' && word !== '\n'
}).length
}
const isValid = inputText => {
const wc = getWordCount(inputText)
return wc > 0 && wc <= maxWordCount
}
const [wordCount, setWordCount] = useState(getWordCount(defaultValue))
const formatDuration = minutes => {
const dur = moment.duration(minutes, 'minutes')
const formatted = moment.utc(dur.asMilliseconds()).format('mm:ss')
return formatted
}
const updateState = event => {
setWordCount(getWordCount(event.target.value))
}
// onDone for the initial render
useEffect(() => onDone({ valid: isValid(defaultValue), value: defaultValue }), [])
// onDone for when the defaultValue is changed from top down
useEffect(() => {
if (!inputRef.current || inputRef.current.value === defaultValue) return
inputRef.current.value = defaultValue
setWordCount(getWordCount(defaultValue))
onDone({ valid: isValid(defaultValue), value: defaultValue })
}, [defaultValue]) // initiall report if default value is valid or not
return (
<div className={classes.container}>
<div className={classes.scriptInfo}>
<div className={classes.question}>
<SvgVideo />
<h5 className={classes.questionNum}>{questionNumber}</h5>
<h5 className={classes.questionName}>{questionName}</h5>
</div>
<div className={classes.quantity}>
<div className={classes.time}>
<span className={classes.duration}>{formatDuration(wordCount / wordsPerMinute)}</span>
<SvgClock />
</div>
<div className={classes.words}>
<span>
{wordCount}/{maxWordCount}
</span>
</div>
</div>
</div>
<TextareaAutosize
key='area'
minRows={4}
className={cx(classes.input, !(wordCount > 0 && wordCount <= maxWordCount) && classes.inputError)}
onChange={event => {
updateState(event)
}}
onBlur={event => {
updateState(event)
onDone({
value: event.target.value,
valid: isValid(event.target.value),
})
}}
defaultValue={defaultValue}
disabled={disabled}
ref={inputRef}
/>
</div>
)
}
const useStyles = createUseStyles(theme => ({
container: {
display: 'flex',
flexDirection: 'column',
fontFamily: 'sans-serif',
color: theme.colorSecondary,
},
scriptInfo: {
display: 'flex',
justifyContent: 'space-between',
padding: '.625rem',
fontSize: theme.secondaryTextFontSize,
},
question: {
display: 'flex',
alignItems: 'center',
gap: '.4rem',
},
questionNum: {
margin: 0,
},
questionName: {
margin: '0rem 0rem 0rem .4rem',
fontSize: theme.secondaryTextFontSize,
},
quantity: {
display: 'flex',
alignItems: 'center',
},
time: {
display: 'flex',
alignItems: 'center',
gap: '0.438rem',
},
duration: {
opacity: theme.secondaryTextOpacity,
},
words: {
opacity: theme.secondaryTextOpacity,
marginLeft: '2.5rem',
},
input: {
backgroundColor: theme.backgroundColorComponent,
padding: theme.inputFieldPadding,
resize: 'none',
border: 'none',
borderRadius: theme.defaultBorderRadius,
fontFamily: theme.defaultFontFamily,
fontSize: theme.inputFieldFontSize,
fontWeight: 'normal',
lineHeight: '1.875rem',
margin: '0.375rem 0rem',
},
inputError: {
border: '.3rem solid red',
},
}))