file-tree-doc.jsx 1.8 KB

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