Skip to content

Commit

Permalink
fix(ListItem): Remove autoFocus and set focus on mount in RenameInput
Browse files Browse the repository at this point in the history
to avoid triggering onBlur by default
  • Loading branch information
JF-Cozy committed Jun 20, 2023
1 parent 20534cb commit 04d9985
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions react/MuiCozyTheme/ListItem/ListItemBase/Renaming/RenameInput.jsx
Expand Up @@ -42,22 +42,6 @@ const RenameInput = ({ file, onClose }) => {
const [isModalOpened, setIsModalOpened] = useState(false)
const defaultValue = file.name

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 check = async () => {
const { value } = textInput.current
if (value === '' || value === '.' || value === '..') {
Expand Down Expand Up @@ -96,23 +80,44 @@ const RenameInput = ({ file, onClose }) => {
onClose()
}

useEffect(() => {
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)
}, [defaultValue])
}

useEffect(() => {
textInput.current.focus()
}, [])

return (
<InputGroup className={styles.inputGroup}>
<Input
ref={textInput}
type="text"
defaultValue={defaultValue}
autoFocus
disabled={isModalOpened || isBusy}
onBlur={handleBlur}
onFocus={handleFocus}
onKeyDown={handleKeyDown}
/>
{(isModalOpened || isBusy) && <Spinner className={styles.spinner} />}
Expand Down

0 comments on commit 04d9985

Please sign in to comment.