Created
November 10, 2023 20:13
-
-
Save joemaffei/5740765aef7f066c3f1ea45f0941ee42 to your computer and use it in GitHub Desktop.
Simple FileTree component
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
const FileTreeStyles = styled.ul` | |
button { | |
font-family: monospace; | |
background: none; | |
border: 0; | |
padding: 0.125em 0; | |
&:hover { | |
background: #f002; | |
} | |
} | |
`; | |
function FileTree({ rootNode, root = true, onClick }) { | |
const [showChildren, setShowChildren] = useState(false); | |
const RootNode = root ? FileTreeStyles : Fragment; | |
return ( | |
<RootNode> | |
<li> | |
<button | |
type="button" | |
className="file-tree" | |
onClick={() => setShowChildren((current) => !current)} | |
> | |
{showChildren ? "▼" : "▶️"} {rootNode.name} | |
</button> | |
</li> | |
{showChildren && ( | |
<ul style={{ paddingLeft: "1em" }}> | |
{rootNode.children.map((childNode) => { | |
if (childNode.children) { | |
return <FileTree rootNode={childNode} root={false} onClick={onClick} />; | |
} | |
return ( | |
<li> | |
<button type="button" onClick={() => onClick(childNode)}> | |
📄 {childNode.name} | |
</button> | |
</li> | |
); | |
})} | |
</ul> | |
)} | |
</RootNode> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment