import React, { useEffect, useMemo, useState } from 'react' import useTheme from '../use-theme' import { setChildrenProps } from '../utils/collections' import TreeFile from './tree-file' import Expand from '../shared/expand' import TreeIndents from './tree-indents' import { useTreeContext } from './tree-context' import TreeFolderIcon from './tree-folder-icon' import TreeStatusIcon from './tree-status-icon' import { sortChildren, makeChildPath, stopPropagation } from './tree-help' interface Props { name: string extra?: string parentPath?: string level?: number className?: string } const defaultProps = { level: 0, className: '', parentPath: '', } type NativeAttrs = Omit, keyof Props> export type TreeFolderProps = Props & NativeAttrs const TreeFolder: React.FC> = ({ name, children, parentPath, level: parentLevel, extra, className, ...props }: React.PropsWithChildren & typeof defaultProps) => { const theme = useTheme() const { initialExpand, isImperative } = useTreeContext() const [expanded, setExpanded] = useState(initialExpand) useEffect(() => setExpanded(initialExpand), []) const currentPath = useMemo(() => makeChildPath(name, parentPath), []) const clickHandler = () => setExpanded(!expanded) const nextChildren = setChildrenProps( children, { parentPath: currentPath, level: parentLevel + 1, }, [TreeFolder, TreeFile], ) const sortedChildren = isImperative ? nextChildren : sortChildren(nextChildren, TreeFolder) return (
{name} {extra && {extra}}
{sortedChildren}
) } TreeFolder.defaultProps = defaultProps TreeFolder.displayName = 'GeistTreeFolder' export default TreeFolder