import React from 'react'; import { SilkeBox } from '../silke-box'; import { SilkeIcon } from '../silke-icon'; import styles from './silke-nested-sortable-list.scss'; import { getIndentLevel, hasState, Item, ItemState, RenderFunction, SilkeSortableIconComponent, } from './silke-nested-sortable-utils'; type SilkeNestedSortableItemProps = { item: Item; state: number; index: number; className?: string; replaceIcon?: SilkeSortableIconComponent; children: RenderFunction; onMouseDown?: (e: React.MouseEvent, item: Item) => void; onDoubleClick?: (e: React.MouseEvent, item: Item) => void; onCollapseToggle?: (item: Item) => void; onMouseEnter?: (e: React.MouseEvent, item: Item) => void; onMouseLeave?: (e: React.MouseEvent, item: Item) => void; }; export function SilkeNestedSortableItem({ item, index, state, children, replaceIcon, className, onCollapseToggle, onMouseDown, onDoubleClick, onMouseEnter, onMouseLeave, }: SilkeNestedSortableItemProps) { let cl = styles.item; const hasChildren = hasState(state, ItemState.canHaveChildren); const indent = getIndentLevel(state); const isSelected = hasState(state, ItemState.selected); const isChildOfSelected = hasState(state, ItemState.childOfSelected); if (isSelected) cl += ' ' + styles.selected; if (isChildOfSelected) cl += ' ' + styles.childOfSelected; if (hasState(state, ItemState.roundTop)) cl += ' ' + styles.roundTop; if (hasState(state, ItemState.roundBottom)) cl += ' ' + styles.roundBottom; if (hasState(state, ItemState.childOfSelected)) cl += ' ' + styles.childOfSelected; if (hasState(state, ItemState.placeAfter)) cl += ' ' + styles.placeAfter; if (hasState(state, ItemState.placeBefore)) cl += ' ' + styles.placeBefore; if (hasState(state, ItemState.placeInside)) cl += ' ' + styles.placeInside; if (hasState(state, ItemState.placementInvalid)) cl += ' ' + styles.placementInvalid; if (hasChildren) cl += ' ' + styles.hasChildren; if (item.disabled) cl += ' ' + styles.disabled; if (indent) cl += ' ' + styles[`indent${indent}`]; if (item.className) cl += ' ' + item.className; if (className) cl += ' ' + className; const content = children({ ...item, expanded: !hasState(state, ItemState.collapsed) }, index); if (!content) return null; const shouldShowExpandIcon = item.showExpandIcon !== undefined ? item.showExpandIcon : (item.children?.length || 0) > 0; const IconComponent = typeof item.replaceIcon === 'function' ? item.replaceIcon : replaceIcon; const handleIconClick = (e: React.MouseEvent) => { if (hasChildren) e.stopPropagation(); onCollapseToggle?.(item); }; return (
onMouseDown(e, item) : undefined} onDoubleClick={!item.disabled && onDoubleClick ? (e) => onDoubleClick(e, item) : undefined} onMouseEnter={ onMouseEnter && !item.disabled ? (e: React.MouseEvent) => onMouseEnter(e, item) : undefined } onMouseLeave={ onMouseLeave && !item.disabled ? (e: React.MouseEvent) => onMouseLeave(e, item) : undefined } > {IconComponent ? ( ) : ( onCollapseToggle && !item.disableCollapse && ( {!shouldShowExpandIcon ? null : ( )} ) )} {content}
); }