{"version":3,"file":"TreeItemWrapper.mjs","names":["DragIcon"],"sources":["../../../src/tree-view/TreeItemWrapper.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 classNames from 'classnames';\nimport type React from 'react';\nimport { useRef } from 'react';\n\nimport { findFocusableChildren, findFocusableSibling } from '../_common/utils';\nimport DragIcon from '../icons/generated/custom/Drag';\nimport {\n  focusCellInNextRow,\n  focusCellInPreviousRow,\n  focusNextRow,\n  focusPreviousRow,\n} from './tree-view-keyboard-helpers';\nimport { type TreeItem, type TreeItemComponentProps } from './tree-view-types';\nimport { Trail } from './TreeViewTrail';\n\n/**\n * Handles keydown events for when the item itself is focused (wrapperRef.current)\n * Moves the focus between the item and its children as well as toggles collapsing of the item\n * @param event - Keyboard event\n * @param shouldDisableInteraction - Whether interaction is disabled\n * @param wrapperRef - Ref to the wrapper element\n * @param onCollapse - Collapse handler\n * @param item - Tree item\n * @returns\n */\n\nconst onWrapperKeyDown = (\n  event: React.KeyboardEvent,\n  shouldDisableInteraction: boolean,\n  wrapperRef: React.RefObject<HTMLElement | null>,\n  onCollapse: (() => void) | undefined,\n  item: TreeItem<Record<string, unknown>>,\n) => {\n  if (shouldDisableInteraction || wrapperRef.current === null) {\n    return;\n  }\n\n  if (event.key === 'Enter' && onCollapse) {\n    onCollapse();\n  } else if (event.key === 'ArrowRight') {\n    event.preventDefault();\n    if (item.canHaveSubItems && item.isCollapsed && onCollapse) {\n      onCollapse();\n    } else {\n      const focusable = findFocusableChildren(wrapperRef.current);\n      const [firstElement] = focusable;\n      if (firstElement instanceof HTMLElement) {\n        firstElement.focus();\n      }\n    }\n\n    event.preventDefault();\n  } else if (event.key === 'ArrowLeft') {\n    event.preventDefault();\n    if (item.canHaveSubItems && !item.isCollapsed && onCollapse) {\n      onCollapse();\n    } else {\n      focusPreviousRow(wrapperRef.current);\n    }\n  } else if (event.key === 'ArrowDown') {\n    event.preventDefault();\n    focusNextRow(wrapperRef.current);\n  } else if (event.key === 'ArrowUp') {\n    event.preventDefault();\n    focusPreviousRow(wrapperRef.current);\n  }\n};\n\n/**\n * Handles keydown events for when one of the items children is focused\n * Moves the focus between the children of the item and up to the parent if needed.\n * Stops the event from propagating to the parent since it has it own keydown handler\n * The parent is the wrapperRef.current and uses onWrapperKeyDown\n * @param event - Keyboard event\n * @param shouldDisableInteraction - Whether interaction is disabled\n * @param wrapperRef - Ref to the wrapper element\n */\nconst onInnerDivKeyDown = (\n  event: React.KeyboardEvent,\n  shouldDisableInteraction: boolean,\n  wrapperRef: React.RefObject<HTMLElement | null>,\n) => {\n  if (shouldDisableInteraction) {\n    return;\n  }\n  event.stopPropagation();\n  if (event.key === 'ArrowRight') {\n    event.preventDefault();\n    const nextElement = findFocusableSibling(wrapperRef, 'next');\n    if (nextElement) {\n      nextElement.focus();\n    }\n  } else if (event.key === 'ArrowLeft') {\n    event.preventDefault();\n    const previousElement = findFocusableSibling(wrapperRef, 'prev');\n    if (previousElement) {\n      previousElement.focus();\n    } else {\n      wrapperRef.current?.focus();\n    }\n  } else if (event.key === 'ArrowDown' && wrapperRef.current !== null) {\n    event.preventDefault();\n    focusCellInNextRow(wrapperRef.current);\n  } else if (event.key === 'ArrowUp' && wrapperRef.current !== null) {\n    event.preventDefault();\n    focusCellInPreviousRow(wrapperRef.current);\n  } else if (event.shiftKey && event.key === 'Tab') {\n    const previousElement = findFocusableSibling(wrapperRef, 'prev');\n    if (previousElement) {\n      event.preventDefault();\n      previousElement.focus();\n    }\n  } else if (event.key === 'Tab') {\n    if (event.shiftKey) {\n      return;\n    }\n\n    const nextElement = findFocusableSibling(wrapperRef, 'next');\n    if (nextElement) {\n      nextElement.focus();\n      event.preventDefault();\n    }\n  }\n};\n\n/**\n * Used as a helper function to wrap the TreeItem component\n * This adds the drag handle and trail to the start of a tree item.\n * Useful if you want to add additional functionality to the tree item but still keep the drag handle and trail\n * Is also used internally to create the TreeViewTextItem component\n */\nfunction TreeItemWrapperInner<T extends Record<string, unknown>>({\n  depth,\n  shouldDisableInteraction,\n  shouldDisableSorting,\n  isGhost,\n  isIndicator,\n  onCollapse,\n  item,\n  setNodeRef,\n  setActivatorNodeRef,\n  dragHandleProps,\n  style,\n  className,\n  trails,\n  children,\n  indentationWidth,\n  ariaLevel,\n  ariaPosInSet,\n  ariaSetSize,\n  tabIndex,\n  onFocus,\n  ref,\n  // Need to destruct to avoid passing it to the component\n  // oxlint-disable-next-line no-unused-vars\n  ...restProps\n}: React.PropsWithChildren<TreeItemComponentProps<T>>) {\n  const wrapperRef = useRef<HTMLElement | null>(null);\n\n  return (\n    <li\n      ref={(el) => {\n        wrapperRef.current = el;\n\n        if (el === null) {\n          return;\n        }\n        setNodeRef(el);\n      }}\n      className={classNames({\n        className,\n        indicator: isIndicator,\n        'ndl-tree-view-list-item': true,\n        'ndl-tree-view-list-item-disable-interaction': shouldDisableInteraction,\n        'ndl-tree-view-list-item-placeholder': isGhost,\n      })}\n      style={{\n        ...style,\n        paddingLeft: isIndicator ? `${depth * indentationWidth}px` : 0,\n      }}\n      role=\"treeitem\"\n      onFocus={onFocus}\n      aria-level={ariaLevel}\n      aria-posinset={ariaPosInSet}\n      aria-setsize={ariaSetSize}\n      aria-selected={undefined}\n      aria-checked={item.isSelected && !item.isSkeletonLoading}\n      aria-expanded={item.canHaveSubItems ? !item.isCollapsed : undefined}\n      onKeyDown={(event) =>\n        onWrapperKeyDown(\n          event,\n          shouldDisableInteraction ?? false,\n          wrapperRef,\n          onCollapse,\n          item,\n        )\n      }\n    >\n      <div\n        role=\"presentation\"\n        className=\"ndl-tree-view-list-item-content\"\n        ref={ref}\n        onKeyDown={(event) =>\n          onInnerDivKeyDown(\n            event,\n            shouldDisableInteraction ?? false,\n            wrapperRef,\n          )\n        }\n      >\n        {!shouldDisableSorting && (\n          <button\n            type=\"button\"\n            className=\"ndl-tree-view-drag-handle\"\n            ref={setActivatorNodeRef}\n            aria-label=\"Pick up\"\n            {...dragHandleProps}\n            tabIndex={tabIndex}\n          >\n            <DragIcon />\n          </button>\n        )}\n        {trails.map((trail, index) => {\n          return <Trail key={index} variant={trail} />;\n        })}\n\n        {children}\n      </div>\n    </li>\n  );\n}\n\nexport const TreeItemWrapper = TreeItemWrapperInner;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwJE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;;;;;;;AA9HF,MAAM,oBACJ,OACA,0BACA,YACA,YACA,SACG;CACH,IAAI,4BAA4B,WAAW,YAAY,MACrD;CAGF,IAAI,MAAM,QAAQ,WAAW,YAC3B,WAAW;MACN,IAAI,MAAM,QAAQ,cAAc;EACrC,MAAM,eAAe;EACrB,IAAI,KAAK,mBAAmB,KAAK,eAAe,YAC9C,WAAW;OACN;GAEL,MAAM,CAAC,gBADW,sBAAsB,WAAW,OACpB;GAC/B,IAAI,wBAAwB,aAC1B,aAAa,MAAM;EAEvB;EAEA,MAAM,eAAe;CACvB,OAAO,IAAI,MAAM,QAAQ,aAAa;EACpC,MAAM,eAAe;EACrB,IAAI,KAAK,mBAAmB,CAAC,KAAK,eAAe,YAC/C,WAAW;OAEX,iBAAiB,WAAW,OAAO;CAEvC,OAAO,IAAI,MAAM,QAAQ,aAAa;EACpC,MAAM,eAAe;EACrB,aAAa,WAAW,OAAO;CACjC,OAAO,IAA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