-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement nested directory browser in CodeBrowser
- Loading branch information
Showing
11 changed files
with
316 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import PropTypes from 'prop-types'; | ||
import { Fragment, useState } from 'react'; | ||
import { Row, Col, Nav } from 'react-bootstrap'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { | ||
faFolder, | ||
faCaretDown, | ||
faCaretRight, | ||
faFile | ||
} from '@fortawesome/free-solid-svg-icons'; | ||
|
||
const getFileName = (path) => path.substring(path.lastIndexOf('/') + 1); | ||
|
||
export default function DirectoryTree({ activeDocument, node, level = 0 }) { | ||
const { path, children, files } = node; | ||
const isFile = !Array.isArray(files); | ||
|
||
const [expanded, setExpanded] = useState(false); | ||
|
||
return ( | ||
<Fragment> | ||
<Row onClick={isFile ? null : () => setExpanded((prevVal) => !prevVal)}> | ||
<Col xs={1}> | ||
{Boolean(children) && ( | ||
<FontAwesomeIcon icon={expanded ? faCaretDown : faCaretRight} /> | ||
)} | ||
</Col> | ||
<Col xs={11} style={level > 0 ? { paddingLeft: (level + 1) * 12 } : {}}> | ||
<Nav.Item className={activeDocument === path && 'text-primary'}> | ||
<Nav.Link eventKey={path}> | ||
<FontAwesomeIcon | ||
icon={isFile ? faFile : faFolder} | ||
className="me-1" | ||
/>{' '} | ||
{path ? getFileName(path) : '.'} | ||
</Nav.Link> | ||
</Nav.Item> | ||
</Col> | ||
</Row> | ||
{expanded && | ||
children.map((child) => ( | ||
<DirectoryTree | ||
key={child.path} | ||
node={child} | ||
level={level + 1} | ||
activeDocument={activeDocument} | ||
/> | ||
))} | ||
{expanded && | ||
files.map((file) => ( | ||
<DirectoryTree | ||
key={file.path} | ||
node={file} | ||
level={level + 1} | ||
activeDocument={activeDocument} | ||
/> | ||
))} | ||
</Fragment> | ||
); | ||
} | ||
|
||
DirectoryTree.propTypes = { | ||
activeDocument: PropTypes.string, | ||
node: PropTypes.object.isRequired, | ||
level: PropTypes.number | ||
}; |
Oops, something went wrong.