2021-03-18 05:52:36 -04:00
|
|
|
import ControlLabel from 'react-bootstrap/lib/ControlLabel'
|
|
|
|
import { Alert, FormControl } from 'react-bootstrap'
|
|
|
|
import FormGroup from 'react-bootstrap/lib/FormGroup'
|
2021-06-23 05:37:08 -04:00
|
|
|
import { useCallback } from 'react'
|
2021-05-07 09:20:55 -04:00
|
|
|
import { Trans, useTranslation } from 'react-i18next'
|
2021-03-18 05:52:36 -04:00
|
|
|
import { useFileTreeCreateName } from '../../contexts/file-tree-create-name'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import {
|
|
|
|
BlockedFilenameError,
|
|
|
|
DuplicateFilenameError,
|
2021-04-27 03:52:58 -04:00
|
|
|
InvalidFilenameError,
|
2021-03-18 05:52:36 -04:00
|
|
|
} from '../../errors'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A form component that renders a text input with label,
|
|
|
|
* plus a validation warning and/or an error message when needed
|
|
|
|
*/
|
|
|
|
export default function FileTreeCreateNameInput({
|
2021-05-07 09:20:55 -04:00
|
|
|
label,
|
2021-03-18 05:52:36 -04:00
|
|
|
focusName = false,
|
|
|
|
classes = {},
|
2021-05-07 09:20:55 -04:00
|
|
|
placeholder,
|
2021-04-27 03:52:58 -04:00
|
|
|
error,
|
2021-03-18 05:52:36 -04:00
|
|
|
}) {
|
2021-05-07 09:20:55 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2021-03-18 05:52:36 -04:00
|
|
|
// the value is stored in a context provider, so it's available elsewhere in the form
|
|
|
|
const { name, setName, touchedName, validName } = useFileTreeCreateName()
|
|
|
|
|
|
|
|
// focus the first part of the filename if needed
|
|
|
|
const inputRef = useCallback(
|
|
|
|
element => {
|
|
|
|
if (element && focusName) {
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
element.focus()
|
|
|
|
element.setSelectionRange(0, element.value.lastIndexOf('.'))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[focusName]
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormGroup controlId="new-doc-name" className={classes.formGroup}>
|
2021-05-07 09:20:55 -04:00
|
|
|
<ControlLabel>{label || t('file_name')}</ControlLabel>
|
2021-03-18 05:52:36 -04:00
|
|
|
|
|
|
|
<FormControl
|
|
|
|
type="text"
|
2021-05-07 09:20:55 -04:00
|
|
|
placeholder={placeholder || t('file_name')}
|
2021-03-18 05:52:36 -04:00
|
|
|
required
|
|
|
|
value={name}
|
|
|
|
onChange={event => setName(event.target.value)}
|
|
|
|
inputRef={inputRef}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<FormControl.Feedback />
|
|
|
|
|
|
|
|
{touchedName && !validName && (
|
|
|
|
<Alert bsStyle="danger" className="row-spaced-small">
|
|
|
|
<Trans i18nKey="files_cannot_include_invalid_characters" />
|
|
|
|
</Alert>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{error && <ErrorMessage error={error} />}
|
|
|
|
</FormGroup>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
FileTreeCreateNameInput.propTypes = {
|
|
|
|
focusName: PropTypes.bool,
|
|
|
|
label: PropTypes.string,
|
|
|
|
classes: PropTypes.shape({
|
2021-04-27 03:52:58 -04:00
|
|
|
formGroup: PropTypes.string,
|
2021-03-18 05:52:36 -04:00
|
|
|
}),
|
|
|
|
placeholder: PropTypes.string,
|
2021-04-27 03:52:58 -04:00
|
|
|
error: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function ErrorMessage({ error }) {
|
|
|
|
// if (typeof error === 'string') {
|
|
|
|
// return error
|
|
|
|
// }
|
|
|
|
|
|
|
|
switch (error.constructor) {
|
|
|
|
case DuplicateFilenameError:
|
|
|
|
return (
|
|
|
|
<Alert bsStyle="danger" className="row-spaced-small">
|
|
|
|
<Trans i18nKey="file_already_exists" />
|
|
|
|
</Alert>
|
|
|
|
)
|
|
|
|
|
|
|
|
case InvalidFilenameError:
|
|
|
|
return (
|
|
|
|
<Alert bsStyle="danger" className="row-spaced-small">
|
|
|
|
<Trans i18nKey="files_cannot_include_invalid_characters" />
|
|
|
|
</Alert>
|
|
|
|
)
|
|
|
|
|
|
|
|
case BlockedFilenameError:
|
|
|
|
return (
|
|
|
|
<Alert bsStyle="danger" className="row-spaced-small">
|
|
|
|
<Trans i18nKey="blocked_filename" />
|
|
|
|
</Alert>
|
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
// return <Trans i18nKey="generic_something_went_wrong" />
|
|
|
|
return null // other errors are displayed elsewhere
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ErrorMessage.propTypes = {
|
2021-04-27 03:52:58 -04:00
|
|
|
error: PropTypes.oneOfType([PropTypes.object, PropTypes.string]).isRequired,
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|