overleaf/services/web/frontend/js/features/file-tree/components/file-tree-folder.js
Timothée Alby 420aa4a657 Merge pull request #3232 from overleaf/ta-file-tree-react
React File Tree

GitOrigin-RevId: fb3141ba8cd9ca0d68e87edb74764a360144c8fe
2020-11-27 03:05:05 +00:00

78 lines
2.1 KiB
JavaScript

import React, { useState } from 'react'
import PropTypes from 'prop-types'
import { useTranslation } from 'react-i18next'
import classNames from 'classnames'
import Icon from '../../../shared/components/icon'
import { useSelectableEntity } from '../contexts/file-tree-selectable'
import { useDroppable } from '../contexts/file-tree-draggable'
import FileTreeItemInner from './file-tree-item/file-tree-item-inner'
import FileTreeFolderList from './file-tree-folder-list'
function FileTreeFolder({ name, id, folders, docs, files }) {
const { t } = useTranslation()
const { isSelected, props: selectableEntityProps } = useSelectableEntity(id)
const [expanded, setExpanded] = useState(false)
function handleExpandCollapseClick() {
setExpanded(!expanded)
}
const { isOver: isOverRoot, dropRef: dropRefRoot } = useDroppable(id)
const { isOver: isOverList, dropRef: dropRefList } = useDroppable(id)
const icons = (
<>
<button
onClick={handleExpandCollapseClick}
aria-label={expanded ? t('collapse') : t('expand')}
>
<Icon type={expanded ? 'angle-down' : 'angle-right'} modifier="fw" />
</button>
<Icon type={expanded ? 'folder-open' : 'folder'} modifier="fw" />
</>
)
return (
<>
<li
role="treeitem"
{...selectableEntityProps}
aria-expanded={expanded}
aria-label={name}
tabIndex="0"
ref={dropRefRoot}
className={classNames(selectableEntityProps.className, {
'dnd-droppable-hover': isOverRoot || isOverList
})}
>
<FileTreeItemInner
id={id}
name={name}
isSelected={isSelected}
icons={icons}
/>
</li>
{expanded ? (
<FileTreeFolderList
folders={folders}
docs={docs}
files={files}
dropRef={dropRefList}
/>
) : null}
</>
)
}
FileTreeFolder.propTypes = {
name: PropTypes.string.isRequired,
id: PropTypes.string.isRequired,
folders: PropTypes.array.isRequired,
docs: PropTypes.array.isRequired,
files: PropTypes.array.isRequired
}
export default FileTreeFolder