-
Notifications
You must be signed in to change notification settings - Fork 9
/
InlineTextEdit.tsx
165 lines (147 loc) · 5 KB
/
InlineTextEdit.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
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
155
156
157
158
159
160
161
162
163
164
165
import React, { useEffect, useRef, useState } from 'react'
import { styled } from '@mui/material/styles'
import { Button, Grid, TextField, Typography } from '@mui/material'
import { Edit as EditIcon } from '@mui/icons-material'
import { CODE_ENTER, CODE_NUMPAD_ENTER, CODE_ESCAPE } from 'keycode-js'
import { ValidationResult } from '../utils/validation.js'
const PREFIX = 'InlineTextEdit'
const classes = {
root: `${PREFIX}-root`,
editIcon: `${PREFIX}-editIcon`,
buttonSep: `${PREFIX}-buttonSep`,
button: `${PREFIX}-button`
}
const Root = styled('div')(() => ({
[`&.${classes.root}`]: {
'& .MuiInputBase-root.Mui-disabled': {
color: 'rgba(0, 0, 0, 0.6)' // (default alpha is 0.38)
},
'& input:disabled': {
cursor: 'pointer'
}
},
[`& .${classes.editIcon}`]: {
cursor: 'pointer'
},
[`& .${classes.buttonSep}`]: {
marginRight: 15
},
[`& .${classes.button}`]: {
margin: 5
}
}))
interface InlineTextEditProps {
text: string
placeholderText: string
fontSize: string
isSaving: boolean
validate: (value: string | undefined) => ValidationResult
save: (text: string) => Promise<void>
}
function InlineTextEdit (props: InlineTextEditProps): JSX.Element {
const [isEditing, setIsEditing] = useState(false)
const [tempTextValue, setTempTextValue] = useState(props.text)
const [validationResult, setValidationResult] = useState<ValidationResult | undefined>(undefined)
const textInput = useRef(null)
// Resets tempTextValue in case when save fails
useEffect(() => {
if (isEditing) setTempTextValue(props.text)
}, [isEditing])
const save = async (): Promise<void> => {
if (props.text === tempTextValue) return
const result = props.validate(tempTextValue)
if (!result.isValid) {
return setValidationResult(result)
}
await props.save(tempTextValue)
setIsEditing(false)
}
const cancel = (): void => {
setIsEditing(false)
setTempTextValue(props.text)
setValidationResult(undefined)
}
const toggleEdit = (): void => {
if (!isEditing) {
setIsEditing(!isEditing)
}
}
const keyPress = async (e: React.KeyboardEvent<HTMLDivElement>): Promise<void> => {
if (e.code === CODE_ENTER || e.code === CODE_NUMPAD_ENTER) {
e.preventDefault()
await save()
} else if (e.code === CODE_ESCAPE) {
cancel()
}
}
const error = validationResult !== undefined && !validationResult.isValid
const helpMessage = validationResult === undefined || validationResult.isValid
? undefined
: validationResult.messages.length > 0
? validationResult.messages[0]
: 'Value for the field is invalid.'
return (
<Root className={classes.root}>
{
isEditing
? (
<form noValidate autoComplete='off'>
<Grid container>
<Grid item md={9} sm={7} xs={7}>
<TextField
className={classes.buttonSep}
variant='standard'
aria-readonly={false}
onClick={toggleEdit}
fullWidth={true}
inputProps={{ style: { fontSize: props.fontSize } }}
ref={textInput}
id='standard-basic'
autoFocus={true}
placeholder={props.placeholderText}
value={tempTextValue}
onKeyDown={keyPress}
onChange={(e) => {
setValidationResult(undefined)
setTempTextValue(e.target.value)
}}
disabled={!isEditing && props.isSaving}
error={error}
helperText={helpMessage}
/>
</Grid>
<Grid item md={3} sm={5} xs={5}>
<Button
className={classes.button}
disabled={props.text === tempTextValue || props.isSaving}
onClick={save}
aria-label='Save course name'
>
Save
</Button>
<Button
className={classes.button}
disabled={props.isSaving}
onClick={cancel}
aria-label='Cancel editing course name'
>
Cancel
</Button>
</Grid>
</Grid>
</form>
)
: (
<>
<Typography className={classes.buttonSep} variant='inherit' component="span">{props.text}</Typography>
<Button onClick={toggleEdit} disabled={props.isSaving} aria-label='Edit course name'>
<EditIcon className={classes.editIcon} style={{ fontSize: props.fontSize }} />
</Button>
</>
)
}
</Root>
)
}
export type { InlineTextEditProps }
export default InlineTextEdit