{"version":3,"file":"TreeViewTextItem.mjs","names":[],"sources":["../../../src/tree-view/TreeViewTextItem.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 React from 'react';\n\nimport { Checkbox } from '../checkbox';\nimport { CleanIconButton } from '../clean-icon-button';\nimport { ConditionalWrap } from '../conditional-wrap';\nimport { ChevronDownIconOutline } from '../icons/generated/heroIcons/ChevronDownIconOutline';\nimport { ChevronRightIconOutline } from '../icons/generated/heroIcons/ChevronRightIconOutline';\nimport { Skeleton } from '../skeleton';\nimport { Typography } from '../typography';\nimport {\n  type FlattenedTreeItem,\n  type TextItemData,\n  type TreeItemComponentProps,\n} from './tree-view-types';\nimport { TreeItemWrapper } from './TreeItemWrapper';\n\n/**\n * checks if any parent is checked\n * @param item - Tree item\n * @param items - Flattened tree items\n * @returns\n */\nconst parentIsCheckedRecursively = (\n  item: FlattenedTreeItem<TextItemData>,\n  items: FlattenedTreeItem<TextItemData>[],\n): boolean => {\n  const parent = items.find((parentItem) => parentItem.id === item.parentId);\n  if (parent) {\n    if (parent.isSelected) {\n      return true;\n    }\n    return parentIsCheckedRecursively(parent, items);\n  }\n  return false;\n};\n\n/**\n * checks if any children are checked\n * @param item - Tree item\n * @param items - Flattened tree items\n * @returns\n */\nconst childrenCheckedRecursively = (\n  item: FlattenedTreeItem<TextItemData>,\n  items: FlattenedTreeItem<TextItemData>[],\n): boolean => {\n  const children = items.filter((childItem) => childItem.parentId === item.id);\n  if (children.length === 0) {\n    return false;\n  }\n  return children.some(\n    (childItem) =>\n      childItem.isSelected || childrenCheckedRecursively(childItem, items),\n  );\n};\n\nexport const TreeViewTextItem = <T extends TextItemData>({\n  depth,\n  shouldDisableInteraction,\n  shouldDisableSorting,\n  isGhost,\n  indentationWidth,\n  isIndicator,\n  isCollapsed,\n  onCollapse,\n  item,\n  parent,\n  isOver,\n  isOverParent,\n  setNodeRef,\n  setActivatorNodeRef,\n  dragHandleProps,\n  style,\n  trails,\n  onItemsChanged,\n  items,\n  tabIndex,\n  ref,\n  ...restProps\n}: TreeItemComponentProps<T> & { ref?: React.Ref<HTMLElement> }) => {\n  const isAnyParentChecked = parentIsCheckedRecursively(item, items);\n  const isAnyChildChecked = childrenCheckedRecursively(item, items);\n\n  const isIndeterminate =\n    isAnyChildChecked === true &&\n    isAnyParentChecked === false &&\n    item.isSelected === false\n      ? true\n      : false;\n\n  return (\n    <TreeItemWrapper\n      depth={depth}\n      shouldDisableInteraction={shouldDisableInteraction}\n      shouldDisableSorting={shouldDisableSorting}\n      isGhost={isGhost}\n      indentationWidth={indentationWidth}\n      isIndicator={isIndicator}\n      isCollapsed={isCollapsed}\n      onCollapse={onCollapse}\n      item={item}\n      parent={parent}\n      isOver={isOver}\n      isOverParent={isOverParent}\n      setNodeRef={setNodeRef}\n      setActivatorNodeRef={setActivatorNodeRef}\n      dragHandleProps={dragHandleProps}\n      style={style}\n      trails={trails}\n      // oxlint-disable-next-line @typescript-eslint/no-empty-function\n      onItemsChanged={() => {}}\n      ref={ref}\n      items={items}\n      tabIndex={tabIndex}\n      {...restProps}\n    >\n      {item.canHaveSubItems && onCollapse && (\n        <button\n          type=\"button\"\n          className=\"ndl-tree-view-collapse-button\"\n          onClick={() => onCollapse()}\n          aria-label={item.isCollapsed ? 'Expand' : 'Collapse'}\n          aria-expanded={item.isCollapsed ? 'false' : 'true'}\n          /**\n           * Tab index is set to -1 to prevent the item from being focused when the tree is navigated with tab\n           * since we use the arrow keys instead\n           */\n          tabIndex={tabIndex}\n        >\n          {item.isCollapsed && <ChevronRightIconOutline className=\"ndl-icon\" />}\n          {!item.isCollapsed && <ChevronDownIconOutline className=\"ndl-icon\" />}\n        </button>\n      )}\n      {item.isSelected !== undefined && (\n        <Skeleton\n          onBackground=\"weak\"\n          shape=\"rectangular\"\n          {...item.skeletonProps}\n          isLoading={item.isSkeletonLoading}\n        >\n          <Checkbox\n            isIndeterminate={isIndeterminate}\n            isChecked={item.isSelected}\n            ariaLabel={item.data.text}\n            htmlAttributes={{\n              tabIndex,\n            }}\n            onChange={(event) => {\n              const newItems = items.map((treeItem) => {\n                if (treeItem.id === item.id) {\n                  return {\n                    ...treeItem,\n                    isSelected: event.target.checked,\n                  };\n                }\n                return treeItem;\n              });\n\n              // Uncheck parents if a child is unchecked\n              if (!event.target.checked) {\n                let parentId = item.parentId;\n                while (parentId !== null) {\n                  const parent = newItems.find(\n                    (newItem) => newItem.id === parentId,\n                  );\n                  if (parent) {\n                    newItems.forEach((newItem) => {\n                      if (newItem.id === parent.id) {\n                        newItem.isSelected = false;\n                      }\n                    });\n                    parentId = parent.parentId;\n                  } else {\n                    parentId = null;\n                  }\n                }\n              }\n              // Check children if a parent is checked\n              if (item.canHaveSubItems) {\n                const childIdsToUncheck = items\n                  .filter((childItem) => childItem.parentId === item.id)\n                  .map((childItem) => childItem.id);\n                while (childIdsToUncheck.length > 0) {\n                  const childId = childIdsToUncheck.pop();\n                  const child = items.find(\n                    (childItem) => childItem.id === childId,\n                  );\n                  if (child) {\n                    newItems.forEach((newItem) => {\n                      if (newItem.id === child.id) {\n                        newItem.isSelected = event.target.checked;\n                      }\n                    });\n                    newItems\n                      .filter((childItem) => childItem.parentId === child.id)\n                      .forEach((childItem) => {\n                        childIdsToUncheck.push(childItem.id);\n                      });\n                  }\n                }\n              }\n              onItemsChanged(newItems, { item, reason: 'selected' });\n            }}\n            aria-label={item.data.text}\n          />\n        </Skeleton>\n      )}\n\n      <Skeleton\n        onBackground=\"weak\"\n        shape=\"rectangular\"\n        height=\"20px\"\n        {...item.skeletonProps}\n        isLoading={item.isSkeletonLoading}\n      >\n        <ConditionalWrap\n          shouldWrap={item.data.onTextClick !== undefined}\n          wrap={(children) => (\n            <button\n              type=\"button\"\n              onClick={item.data.onTextClick}\n              className=\"ndl-tree-view-text-clickable\"\n              tabIndex={tabIndex}\n            >\n              {children}\n            </button>\n          )}\n        >\n          <Typography\n            variant={item.canHaveSubItems ? 'subheading-small' : 'body-medium'}\n            htmlAttributes={{\n              style: {\n                overflow: 'hidden',\n                textOverflow: 'ellipsis',\n                whiteSpace: 'nowrap',\n              },\n            }}\n          >\n            {item.data.text}\n          </Typography>\n        </ConditionalWrap>\n      </Skeleton>\n      <div className=\"ndl-tree-view-actions\">\n        {item.data.actions &&\n          item.data.actions.map((action, index) => {\n            return (\n              <CleanIconButton\n                key={index}\n                className=\"ndl-tree-view-action\"\n                size=\"small\"\n                {...action.buttonProps}\n                htmlAttributes={{\n                  tabIndex,\n                  ...action.buttonProps.htmlAttributes,\n                }}\n              >\n                {action.icon}\n              </CleanIconButton>\n            );\n          })}\n      </div>\n    </TreeItemWrapper>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;CA6EE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAvDF,MAAM,8BACJ,MACA,UACY;CACZ,MAAM,SAAS,MAAM,MAAM,eAAe,WAAW,OAAO,KAAK,QAAQ;CACzE,IAAI,QAAQ;EACV,IAAI,OAAO,YACT,OAAO;EAET,OAAO,2BAA2B,QAAQ,KAAK;CACjD;CACA,OAAO;AACT;;;;;;;AAQA,MAAM,8BACJ,MACA,UACY;CACZ,MAAM,WAAW,MAAM,QAAQ,cAAc,UAAU,aAAa,KAAK,EAAE;CAC3E,IAAI,SAAS,WAAW,GACt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