import { forwardRef, useCallback } from "react"; import cn from "classnames"; import { useIcon } from "@react-md/icon"; import type { ListElement } from "@react-md/list"; import { ListItemChildren } from "@react-md/list"; import { useInteractionStates } from "@react-md/states"; import { bem } from "@react-md/utils"; import { TreeGroup } from "./TreeGroup"; import { TreeItemExpanderIcon } from "./TreeItemExpanderIcon"; import type { TreeItemProps, TreeItemWithContentComponentProps } from "./types"; const block = bem("rmd-tree-item"); /** * This component renders an item within a tree with optional child items. This * should almost always be used from the `itemRenderer` prop from the `Tree` * component as it provides a lot of the required a11y props for you. */ export const TreeItem = forwardRef< HTMLLIElement, TreeItemProps | TreeItemWithContentComponentProps >(function TreeItem( { id, className: propClassName, liStyle, liClassName, liRef, depth, listSize, itemIndex, renderChildItems, expanded, selected, focused, expanderIcon: propExpanderIcon, expanderLeft = false, textClassName, secondaryTextClassName, textChildren = true, primaryText, secondaryText, leftAddon: propLeftAddon, leftAddonType = "icon", leftAddonPosition = "middle", rightAddon: propRightAddon, rightAddonType = "icon", rightAddonPosition = "middle", forceAddonWrap, height = "auto", threeLines = false, children, contentComponent: Content = "span", isLink: propIsLink, disabled = false, readOnly, onFocus, onKeyUp, onKeyDown, onClick, onMouseUp, onMouseDown, onMouseLeave, onTouchMove, onTouchEnd, onTouchStart, ...props }, ref ) { const expanderIcon = useIcon("expander", propExpanderIcon); const isLink = typeof propIsLink === "boolean" ? propIsLink : typeof Content !== "string" || Content === "a"; const { ripples, className, handlers } = useInteractionStates({ disabled, className: propClassName, handlers: { onKeyUp, onKeyDown, onClick, onMouseUp, onMouseDown, onMouseLeave, onTouchMove, onTouchEnd, onTouchStart, }, disableSpacebarClick: isLink, }); let group; let leftAddon = propLeftAddon; let rightAddon = propRightAddon; if (renderChildItems) { const icon = ( {expanderIcon} ); if (expanderLeft) { leftAddon = icon; } else { rightAddon = icon; } group = {renderChildItems()}; } const handleFocus = useCallback( (event: React.FocusEvent) => { if (onFocus) { onFocus(event); } event.preventDefault(); event.currentTarget.closest('[role="tree"]')?.focus(); }, [onFocus] ); const a11y = { "aria-expanded": renderChildItems ? expanded : undefined, "aria-selected": selected, "aria-level": depth + 1, "aria-setsize": listSize, "aria-posinset": itemIndex + 1, "aria-disabled": disabled || undefined, id, role: "treeitem", tabIndex: -1, ...handlers, onFocus: handleFocus, } as const; const noA11y = { role: "none" } as const; return (
  • {children} {ripples} {group}
  • ); });