file-tree-doc.jsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import PropTypes from 'prop-types'
  2. import { useSelectableEntity } from '../contexts/file-tree-selectable'
  3. import FileTreeItemInner from './file-tree-item/file-tree-item-inner'
  4. import { useTranslation } from 'react-i18next'
  5. import Icon from '../../../shared/components/icon'
  6. import iconTypeFromName from '../util/icon-type-from-name'
  7. import classnames from 'classnames'
  8. function FileTreeDoc({ name, id, isFile, isLinkedFile }) {
  9. const { isSelected, props: selectableEntityProps } = useSelectableEntity(
  10. id,
  11. isFile ? 'file' : 'doc'
  12. )
  13. return (
  14. <li
  15. // eslint-disable-next-line jsx-a11y/role-has-required-aria-props
  16. role="treeitem"
  17. // aria-selected is provided in selectableEntityProps
  18. {...selectableEntityProps}
  19. aria-label={name}
  20. tabIndex="0"
  21. >
  22. <FileTreeItemInner
  23. id={id}
  24. name={name}
  25. isSelected={isSelected}
  26. icons={<FileTreeIcon isLinkedFile={isLinkedFile} name={name} />}
  27. />
  28. </li>
  29. )
  30. }
  31. FileTreeDoc.propTypes = {
  32. name: PropTypes.string.isRequired,
  33. id: PropTypes.string.isRequired,
  34. isFile: PropTypes.bool,
  35. isLinkedFile: PropTypes.bool,
  36. }
  37. export const FileTreeIcon = ({ isLinkedFile, name }) => {
  38. const { t } = useTranslation()
  39. const className = classnames('spaced', 'file-tree-icon', {
  40. 'linked-file-icon': isLinkedFile,
  41. })
  42. return (
  43. <>
  44. &nbsp;
  45. <Icon type={iconTypeFromName(name)} fw className={className} />
  46. {isLinkedFile && (
  47. <Icon
  48. type="external-link-square"
  49. modifier="rotate-180"
  50. className="linked-file-highlight"
  51. accessibilityLabel={t('linked_file')}
  52. />
  53. )}
  54. </>
  55. )
  56. }
  57. FileTreeIcon.propTypes = {
  58. name: PropTypes.string.isRequired,
  59. isLinkedFile: PropTypes.bool,
  60. }
  61. export default FileTreeDoc