2021-03-10 07:19:54 -05:00
|
|
|
import React, { useEffect, useState, useRef } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2021-04-27 05:06:44 -04:00
|
|
|
import { OverlayTrigger, Tooltip } from 'react-bootstrap'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2021-03-10 07:19:54 -05:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import Icon from '../../../shared/components/icon'
|
|
|
|
|
|
|
|
function ProjectNameEditableLabel({
|
|
|
|
projectName,
|
|
|
|
userIsAdmin,
|
|
|
|
onChange,
|
2021-04-27 03:52:58 -04:00
|
|
|
className,
|
2021-03-10 07:19:54 -05:00
|
|
|
}) {
|
2021-04-27 05:06:44 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2021-03-10 07:19:54 -05:00
|
|
|
const [isRenaming, setIsRenaming] = useState(false)
|
|
|
|
|
|
|
|
const canRename = userIsAdmin && !isRenaming
|
|
|
|
|
|
|
|
const [inputContent, setInputContent] = useState(projectName)
|
|
|
|
|
|
|
|
const inputRef = useRef(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isRenaming) {
|
|
|
|
inputRef.current.select()
|
|
|
|
}
|
|
|
|
}, [isRenaming])
|
|
|
|
|
|
|
|
function startRenaming() {
|
|
|
|
setInputContent(projectName)
|
|
|
|
setIsRenaming(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleKeyDown(event) {
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
event.preventDefault()
|
|
|
|
setIsRenaming(false)
|
|
|
|
onChange(event.target.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleOnChange(event) {
|
|
|
|
setInputContent(event.target.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleBlur() {
|
|
|
|
setIsRenaming(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classNames('project-name', className)}>
|
|
|
|
{!isRenaming && (
|
|
|
|
<span className="name" onDoubleClick={startRenaming}>
|
|
|
|
{projectName}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
{isRenaming && (
|
|
|
|
<input
|
|
|
|
ref={inputRef}
|
|
|
|
type="text"
|
|
|
|
className="form-control"
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
onChange={handleOnChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
value={inputContent}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{canRename && (
|
2021-04-27 05:06:44 -04:00
|
|
|
<OverlayTrigger
|
|
|
|
placement="bottom"
|
|
|
|
trigger={['hover', 'focus']}
|
|
|
|
overlay={<Tooltip id="tooltip-online-user">{t('rename')}</Tooltip>}
|
|
|
|
>
|
|
|
|
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid, jsx-a11y/click-events-have-key-events, jsx-a11y/interactive-supports-focus */}
|
|
|
|
<a className="rename" role="button" onClick={startRenaming}>
|
|
|
|
<Icon type="pencil" modifier="fw" />
|
|
|
|
</a>
|
|
|
|
</OverlayTrigger>
|
2021-03-10 07:19:54 -05:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectNameEditableLabel.propTypes = {
|
|
|
|
projectName: PropTypes.string.isRequired,
|
|
|
|
userIsAdmin: PropTypes.bool,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
className: PropTypes.string,
|
2021-03-10 07:19:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ProjectNameEditableLabel
|