-
Notifications
You must be signed in to change notification settings - Fork 37
/
RenameInput.jsx
134 lines (117 loc) · 3.19 KB
/
RenameInput.jsx
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
import React, { useEffect, useReducer, useRef, useState } from 'react'
import PropTypes from 'prop-types'
import { useClient } from 'cozy-client'
import { splitFilename } from 'cozy-client/dist/models/file'
import { makeStyles } from '../../../../styles'
import Input from '../../../../Input'
import InputGroup from '../../../../InputGroup'
import Spinner from '../../../../Spinner'
import RenameDialog from './RenameDialog'
import { renameFile } from './helpers'
const KEYS = {
ESCAPE: 'Escape',
ENTER: 'Enter'
}
const useStyles = makeStyles({
inputGroup: {
maxWidth: 'none',
height: '2rem',
margin: '1rem 1rem 1rem 0',
'& > div': {
display: 'flex'
},
'& input': {
maxWidth: 'none',
padding: '.3125rem'
}
},
spinner: {
margin: '.4375rem'
}
})
const RenameInput = ({ file, onClose }) => {
const client = useClient()
const styles = useStyles()
const textInput = useRef()
const [isBusy, toggleBusy] = useReducer(prev => !prev, false)
const [isModalOpened, setIsModalOpened] = useState(false)
const defaultValue = file.name
const check = async () => {
const { value } = textInput.current
if (value === '' || value === '.' || value === '..') {
await submit()
return
}
const oldExtension = splitFilename({
name: defaultValue,
type: 'file'
}).extension
const newExtension = splitFilename({
name: value,
type: 'file'
}).extension
if (oldExtension === newExtension) {
await submit()
return
}
setIsModalOpened(true)
}
const submit = async () => {
const { value } = textInput.current
await close(value)
}
const cancel = async () => {
await close(defaultValue)
}
const close = async value => {
toggleBusy()
if (value !== defaultValue) {
await renameFile(client, file, value)
}
onClose()
}
const handleKeyDown = async event => {
if (event.key === KEYS.ENTER) {
await check()
} else if (event.key === KEYS.ESCAPE) {
await cancel()
}
}
const handleBlur = async () => {
// On top of "normal" blurs, the event happens all the time after a submit or a cancel, because this component is removed from the DOM while having the focus.
// we want to do things only on "normal" blurs, *not* after a cancel
if (!isModalOpened && !isBusy) {
await check()
}
}
const handleFocus = () => {
const { length } = splitFilename({
name: defaultValue,
type: 'file'
}).filename
textInput.current.setSelectionRange(0, length)
}
useEffect(() => {
textInput.current.focus()
}, [])
return (
<InputGroup className={styles.inputGroup}>
<Input
ref={textInput}
type="text"
defaultValue={defaultValue}
disabled={isModalOpened || isBusy}
onBlur={handleBlur}
onFocus={handleFocus}
onKeyDown={handleKeyDown}
/>
{(isModalOpened || isBusy) && <Spinner className={styles.spinner} />}
{isModalOpened && <RenameDialog onSubmit={submit} onCancel={cancel} />}
</InputGroup>
)
}
RenameInput.propTypes = {
file: PropTypes.object.isRequired,
onClose: PropTypes.func.isRequired
}
export default RenameInput