{"version":3,"file":"TreeViewItem.mjs","names":[],"sources":["../../../src/tree-view/TreeViewItem.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport { type UniqueIdentifier } from '@dnd-kit/core';\nimport {\n  type AnimateLayoutChanges,\n  useSortable,\n  type UseSortableArguments,\n} from '@dnd-kit/sortable';\nimport React, { type CSSProperties, useMemo } from 'react';\n\nimport {\n  type FlattenedTreeItem,\n  type ItemChangedReason,\n  type TreeItemComponent,\n} from './tree-view-types';\nimport { transformToString } from './tree-view-utils';\n\nexport interface TreeItemProps<T> {\n  /** Number of child items */\n  childCount?: number;\n  /** Whether this is a clone item */\n  // eslint-disable-next-line @typescript-eslint/naming-convention\n  clone?: boolean;\n  /** Whether the item is collapsed */\n  // eslint-disable-next-line @typescript-eslint/naming-convention\n  collapsed?: boolean;\n  /** Depth of the item in the tree */\n  depth: number;\n  /** Whether interaction should be disabled */\n  shouldDisableInteraction?: boolean;\n  /** Whether this is an indicator item */\n  // eslint-disable-next-line @typescript-eslint/naming-convention\n  indicator?: boolean;\n  /** Width of the indentation */\n  indentationWidth: number;\n  /** The tree item data */\n  item: FlattenedTreeItem<T>;\n  /** Whether this is the last item in its parent */\n  isLast: boolean;\n  /** Parent item of this item */\n  parent: FlattenedTreeItem<T> | null;\n  /** Function to call when item is collapsed */\n  onCollapse?(id: UniqueIdentifier): void;\n  /** Function to call when item is removed */\n  onRemove?(id: UniqueIdentifier): void;\n  /** Function to set the wrapper ref */\n  wrapperRef?(node: HTMLLIElement): void;\n}\n\nconst animateLayoutChanges: AnimateLayoutChanges = ({\n  isSorting,\n  isDragging,\n}) => (isSorting || isDragging ? false : true);\n\ntype SortableTreeItemProps<\n  T extends Record<string, unknown>,\n  E extends HTMLElement,\n> = TreeItemProps<T> & {\n  id: UniqueIdentifier;\n  /** The component to use for rendering the tree item */\n  TreeItemComponent: TreeItemComponent<T, E>;\n  /** Whether sorting should be disabled for this item */\n  shouldDisableSorting?: boolean;\n  /** Props for the sortable functionality */\n  sortableProps?: Omit<UseSortableArguments, 'id'>;\n  /** Whether to keep the ghost item in place while dragging */\n  // eslint-disable-next-line @typescript-eslint/naming-convention\n  keepGhostInPlace?: boolean;\n  /** A list of trails to render before the item */\n  trails: ('none' | 'straight' | 'straight-curved' | 'curved')[];\n  /** Function to call when the item has changed */\n  onItemsChanged: (\n    newItems: FlattenedTreeItem<T>[],\n    itemChangedReason: ItemChangedReason<T>,\n  ) => void;\n  /** All items in the tree */\n  items: FlattenedTreeItem<T>[];\n  ariaLevel?: number;\n  ariaPosInSet?: number;\n  ariaSetSize?: number;\n  tabIndex?: number;\n  /** Function to call when the item receives focus */\n  onFocus?: () => void;\n};\n\nconst SortableTreeViewItemNotMemoized = function SortableTreeViewItem<\n  T extends Record<string, unknown>,\n  E extends HTMLElement,\n>({\n  id,\n  depth,\n  isLast,\n  TreeItemComponent,\n  parent,\n  shouldDisableSorting,\n  // need to destruct to avoid passing it to the component\n  // oxlint-disable-next-line no-unused-vars\n  sortableProps,\n  keepGhostInPlace,\n  item,\n  onCollapse,\n  trails,\n  onItemsChanged,\n  items,\n  ariaLevel,\n  ariaPosInSet,\n  ariaSetSize,\n  tabIndex,\n  onFocus,\n  ...restProps\n}: SortableTreeItemProps<T, E>) {\n  const {\n    attributes,\n    listeners,\n    setNodeRef,\n    transform,\n    setActivatorNodeRef,\n    transition,\n    isDragging,\n    isSorting,\n    isOver,\n  } = useSortable({\n    animateLayoutChanges,\n    id: id,\n    transition: {\n      duration: 150, // milliseconds\n      easing: 'cubic-bezier(0.25, 1, 0.5, 1)',\n    },\n  });\n\n  const style: CSSProperties = {\n    transform: transform ? transformToString(transform) : undefined,\n    transition: transition ?? undefined,\n  };\n  const localCollapse = useMemo(() => {\n    if (!onCollapse) {\n      return undefined;\n    }\n    return () => onCollapse?.(item.id);\n  }, [item.id, onCollapse]);\n\n  return (\n    <TreeItemComponent\n      item={item}\n      isOverParent={false}\n      {...restProps}\n      setActivatorNodeRef={setActivatorNodeRef}\n      setNodeRef={setNodeRef}\n      style={style}\n      depth={depth}\n      isGhost={keepGhostInPlace}\n      shouldDisableInteraction={isSorting}\n      isLastInParent={isLast}\n      parent={parent}\n      dragHandleProps={{\n        ...attributes,\n        ...listeners,\n      }}\n      isIndicator={isDragging}\n      onCollapse={localCollapse}\n      onItemsChanged={onItemsChanged}\n      items={items}\n      ariaLevel={ariaLevel}\n      ariaPosInSet={ariaPosInSet}\n      ariaSetSize={ariaSetSize}\n      shouldDisableSorting={shouldDisableSorting}\n      isOver={isOver}\n      trails={trails}\n      tabIndex={tabIndex}\n      onFocus={onFocus}\n    />\n  );\n};\n\nexport const SortableTreeViewItem = React.memo(\n  SortableTreeViewItemNotMemoized,\n) as typeof SortableTreeViewItemNotMemoized;\n"],"mappings":";;;;;;;;CA2GE;CACA;CACA;CACA;CACA;CACA;CAGA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;;;;;;;;;;;;;;;;;AA3DF,MAAM,wBAA8C,EAClD,WACA,iBACK,aAAa,aAAa,QAAQ;AA0HzC,MAAa,uBAAuB,MAAM,KACxC,SA1F+C,qBAG/C,MAsB8B;CAtB9B,IAAA,EACA,IACA,OACA,QACA,mBACA,QACA,sBAGA,eACA,kBACA,MACA,YACA,QACA,gBACA,OACA,WACA,cACA,aACA,UACA,YAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,EACJ,YACA,WACA,YACA,WACA,qBACA,YACA,YACA,WACA,WACE,YAAY;EACd;EACI;EACJ,YAAY;GACV,UAAU;GACV,QAAQ;EACV;CACF,CAAC;CAED,MAAM,QAAuB;EAC3B,WAAW,YAAY,kBAAkB,SAAS,IAAI,KAAA;EACtD,YAAY,eAAA,QAAA,eAAA,KAAA,IAAA,aAAc,KAAA;CAC5B;CACA,MAAM,gBAAgB,cAAc;EAClC,IAAI,CAAC,YACH;EAEF,aAAA,eAAA,QAAA,eAAA,KAAA,IAAA,KAAA,IAAa,WAAa,KAAK,EAAE;CACnC,GAAG,CAAC,KAAK,IAAI,UAAU,CAAC;CAExB,OACE,oBAAC,mBAAA,eAAA,eAAA;EACO;EACN,cAAc;CACV,GAAA,SAAA,GAAA,CAAA,GAAA;EACiB;EACT;EACL;EACA;EACP,SAAS;EACT,0BAA0B;EAC1B,gBAAgB;EACR;EACR,iBAAA,eAAA,eAAA,CAAA,GACK,UAAA,GACA,SACL;EACA,aAAa;EACb,YAAY;EACI;EACT;EACI;EACG;EACD;EACS;EACd;EACA;EACE;EACD;CACV,CAAA,CAAA;AAEL,CAIA"}