import React from 'react' import { ConnectDragPreview, ConnectDragSource } from 'react-dnd' import { classnames } from './utils/classnames' import { isDescendant } from './utils/tree-data-utils' import './node-renderer-default.css' import { NodeData, TreeItem } from '.' const defaultProps = { isSearchMatch: false, isSearchFocus: false, canDrag: false, toggleChildrenVisibility: undefined, buttons: [], className: '', style: {}, parentNode: undefined, draggedNode: undefined, canDrop: false, title: undefined, subtitle: undefined, } export interface NodeRendererProps { node: TreeItem path: number[] treeIndex: number isSearchMatch: boolean isSearchFocus: boolean canDrag: boolean scaffoldBlockPxWidth: number toggleChildrenVisibility?(data: NodeData): void | undefined buttons?: JSX.Element[] | undefined className?: string | undefined style?: React.CSSProperties | undefined title?: ((data: NodeData) => JSX.Element | JSX.Element) | undefined subtitle?: ((data: NodeData) => JSX.Element | JSX.Element) | undefined icons?: JSX.Element[] | undefined lowerSiblingCounts: number[] swapDepth?: number | undefined swapFrom?: number | undefined swapLength?: number | undefined listIndex: number treeId: string connectDragPreview: ConnectDragPreview connectDragSource: ConnectDragSource parentNode?: TreeItem | undefined startDrag: ({ path }: { path: number[] }) => void endDrag: (dropResult: unknown) => void isDragging: boolean didDrop: boolean draggedNode?: TreeItem | undefined isOver: boolean canDrop?: boolean | undefined } const NodeRendererDefault: React.FC = (props) => { props = { ...defaultProps, ...props } const { scaffoldBlockPxWidth, toggleChildrenVisibility, connectDragPreview, connectDragSource, isDragging, canDrop, canDrag, node, title, subtitle, draggedNode, path, treeIndex, isSearchMatch, isSearchFocus, buttons, className, style, didDrop, treeId: treeIdOut, isOver: isOverOut, // Not needed, but preserved for other renderers parentNode: parentNodeOut, // Needed for dndManager ...otherProps } = props const nodeTitle = title || node.title const nodeSubtitle = subtitle || node.subtitle let handle if (canDrag) { handle = typeof node.children === 'function' && node.expanded ? (
{[...Array.from({ length: 12 })].map((_, index) => (
))}
) : ( connectDragSource(
, { dropEffect: 'copy', }) ) } const isDraggedDescendant = draggedNode && isDescendant(draggedNode, node) const isLandingPadActive = !didDrop && isDragging const buttonStyle = { left: -0.5 * scaffoldBlockPxWidth, right: 0 } return (
{toggleChildrenVisibility && node.children && (node.children.length > 0 || typeof node.children === 'function') && (
) } export default NodeRendererDefault