import React, { JSX } from 'react' import { ConnectDragPreview, ConnectDragSource } from 'react-dnd' import { NodeData, TreeItem } from './types' import { classnames } from './utils/classnames' import { isDescendant } from './utils/tree-data-utils' import './node-renderer-default.css' 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[] className?: string style?: React.CSSProperties title?: (data: NodeData) => JSX.Element subtitle?: (data: NodeData) => JSX.Element icons?: JSX.Element[] lowerSiblingCounts: number[] swapDepth?: number swapFrom?: number swapLength?: number listIndex: number treeId: string rowDirection: 'ltr' | 'rtl' | string | undefined connectDragPreview: ConnectDragPreview connectDragSource: ConnectDragSource parentNode?: TreeItem startDrag: ({ path }: { path: number[] }) => void endDrag: (dropResult: unknown) => void isDragging: boolean didDrop: boolean draggedNode?: TreeItem isOver: boolean canDrop?: boolean } const NodeRendererDefault: React.FC = ({ isSearchMatch = false, isSearchFocus = false, canDrag = false, toggleChildrenVisibility = undefined, buttons = [], className = '', style = {}, parentNode = undefined, draggedNode = undefined, canDrop = false, title = undefined, subtitle = undefined, rowDirection = 'ltr', scaffoldBlockPxWidth, connectDragPreview, connectDragSource, isDragging, node, path, treeIndex, didDrop, treeId: _treeId, isOver: _isOver, ...otherProps }) => { const nodeTitle = title || node.title const nodeSubtitle = subtitle || node.subtitle const rowDirectionClass = rowDirection === 'rtl' ? 'rst__rtl' : undefined let handle if (canDrag) { handle = typeof node.children === 'function' && node.expanded ? (
{Array.from({ length: 12 }).map((_, index) => (
))}
) : (
} className="rst__moveHandle" /> ) } const isDraggedDescendant = draggedNode && isDescendant(draggedNode, node) const isLandingPadActive = !didDrop && isDragging let buttonStyle = { left: -0.5 * scaffoldBlockPxWidth, right: 0 } if (rowDirection === 'rtl') { buttonStyle = { right: -0.5 * scaffoldBlockPxWidth, left: 0 } } return (
{toggleChildrenVisibility && node.children && (node.children.length > 0 || typeof node.children === 'function') && (
) } export default NodeRendererDefault