import React from 'react'; import classnames from 'classnames'; import { CaretDownOutlined } from '@easyv/react-icons'; import { useDrag, useDrop } from 'react-dnd'; import { Input } from 'antd'; import { DraggableTypeEnum } from '@/constants'; export type TreeNodeDataSource = T & { deep: number; }; export interface TreeNodeProp { dataSource: TreeNodeDataSource; keyPropName: string; titlePropName: string; isGroupPropName: string; expandPropName: string; visible?: boolean; isSelected?: boolean; isEditing?: boolean; iconRender?: React.ReactNode; extendRender?: React.ReactNode; dragType: string; onClick?: (e: React.MouseEvent) => void; onRightClick?: (e: React.MouseEvent) => void; onExpandClick?: (e: React.MouseEvent) => void; onDragStart?: (e: React.DragEvent) => void; onDragEnd?: (e: React.DragEvent) => void; onDragHover?: (sourceKey: string, targetKey: string) => void; onDrop?: (sourceKey: string, targetKey: string) => void; onNameEditChange?: (name: string) => void; } const TreeNode: React.FC = function ({ dataSource, keyPropName, titlePropName, isGroupPropName, expandPropName, visible = true, isSelected = false, isEditing = false, iconRender, extendRender, dragType, onClick, onRightClick, onExpandClick, onDragStart, onDragEnd, onDragHover, onDrop, onNameEditChange, }) { const ref = React.useRef(null); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [_, drop] = useDrop( () => ({ accept: DraggableTypeEnum.TREE, hover: (item: TreeNodeDataSource) => { if (!ref.current || !ref.current.dataset.key) return; onDragHover?.(item[keyPropName], ref.current.dataset.key); }, drop: (item: TreeNodeDataSource) => { if (!ref.current || !ref.current.dataset.key) return; onDrop?.(item[keyPropName], ref.current.dataset.key); }, }), [keyPropName], ); const [{ isDragging }, drag] = useDrag( () => ({ type: dragType, item: dataSource, options: { dropEffect: 'copy', }, collect: (monitor) => ({ isDragging: monitor.isDragging(), }), }), [dragType], ); drag(drop(ref)); const [editing, setEditing] = React.useState(false); React.useEffect(() => { setEditing(isEditing); }, [isEditing]); const handelEditChange = (name: string) => { onNameEditChange?.(name); setEditing(false); }; return (
{dataSource[isGroupPropName] ? ( ) : null} {iconRender} {editing ? ( handelEditChange(value)} onBlur={({ currentTarget: { value } }) => handelEditChange(value)} /> ) : ( setEditing(true)}> {dataSource[titlePropName]} )} {extendRender}
); }; export default TreeNode;