1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-09 23:40:06 +00:00

fix: autofocus input field and submit on enter

This commit is contained in:
2022-07-28 23:55:59 +05:00
parent 8de032b543
commit 7681722e5a
3 changed files with 53 additions and 25 deletions

View File

@@ -32,6 +32,14 @@ const NameInputModal = ({
if (defaultName) setName(defaultName)
}, [defaultName])
const handleFocus = (
event: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement, Element>
) => {
if (defaultName) {
event.target.select()
}
}
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const value = event.target.value
@@ -55,21 +63,32 @@ const NameInputModal = ({
setName(value)
}
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
if (hasError || !name) return
action(name)
}
return (
<BootstrapDialog fullWidth onClose={() => setOpen(false)} open={open}>
<BootstrapDialogTitle id="abort-modal" handleOpen={setOpen}>
{title}
</BootstrapDialogTitle>
<DialogContent dividers>
<TextField
fullWidth
variant="outlined"
label={isFolder ? 'Folder Name' : 'File Name'}
value={name}
onChange={handleChange}
error={hasError}
helperText={errorText}
/>
<form onSubmit={handleSubmit}>
<TextField
id="input-box"
fullWidth
autoFocus
onFocus={handleFocus}
variant="outlined"
label={isFolder ? 'Folder Name' : 'File Name'}
value={name}
onChange={handleChange}
error={hasError}
helperText={errorText}
/>
</form>
</DialogContent>
<DialogActions>
<Button variant="contained" onClick={() => setOpen(false)}>
@@ -77,9 +96,7 @@ const NameInputModal = ({
</Button>
<Button
variant="contained"
onClick={() => {
action(name)
}}
onClick={() => action(name)}
disabled={hasError || !name}
>
{actionLabel}