file-tree-doc.js 1.9 KB

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