UNPKG

@alpernative/tree

Version:

[`@alpernative/tree`](https://www.npmjs.com/package/@alpernative/tree) is a high-performance, customizable tree component for React. It provides a wide range of features, including virtualization, drag-and-drop, and nested drag-and-drop support.

63 lines (62 loc) 2.55 kB
import { ReactNode } from 'react'; import { DraggableLocation, DraggableId, DroppableId, DraggableProvided, DraggableStateSnapshot } from '@hello-pangea/dnd'; import { RenderItemParams } from '../TreeItem/TreeItem-types'; import { TreeData, Path, ItemId, FlattenedTree, TreeSourcePosition, TreeDestinationPosition, TreeItem, FlattenedItem } from '../../types'; export type Props = { /** The tree data structure. */ tree: TreeData; /** Function that will be called when a parent item needs to be expanded. */ onExpand: (ItemId: ItemId, path: Path) => void; /** Function that will be called when a parent item needs to be collapsed. */ onCollapse: (ItemId: ItemId, path: Path) => void; /** Function that will be called when the user starts dragging. */ onDragStart: (ItemId: ItemId) => void; /** Function that will be called when the user finishes dragging. */ onDragEnd: (sourcePosition: TreeSourcePosition, destinationPosition?: TreeDestinationPosition) => void; /** Function that will be called to render a single item. */ renderItem: (item: RenderItemParams) => ReactNode; /** Number of pixel is used to scaffold the tree by the consumer. */ offsetPerLevel: number; /** Boolean to turn on drag&drop re-ordering on the tree */ isDragEnabled: boolean | ((item: TreeItem) => boolean); /** Boolean to turn on hovering while dragging */ isNestingEnabled: boolean; /** Boolean to turn on virtualization support */ isVirtualizationEnabled: boolean; /** Virtual item's height. If the isVirtualizationEnabled is true, please set it manually. */ virtualItemHeight: number; }; export type State = { /** The flattened tree data structure transformed from props.tree */ flattenedTree: FlattenedTree; draggedItemId?: ItemId; }; export type Combine = { draggableId: DraggableId; droppableId: DroppableId; }; export type DragState = { source: DraggableLocation; mode: string; destination: DraggableLocation | null; horizontalLevel?: number; combine?: Combine | null; }; export interface VirtualItemProps { provided: DraggableProvided; snapshot: DraggableStateSnapshot; style?: React.CSSProperties; isDragging?: boolean; flatItem: FlattenedItem; } export interface VirtualRowProps { data: FlattenedTree; index: number; style: React.CSSProperties; isDragging?: boolean; } export interface VirtualItemStyle { provided: DraggableProvided; style?: React.CSSProperties; isDragging?: boolean; }