UNPKG

@ducor/react

Version:

admin template ui interface

83 lines (82 loc) 3.83 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { createElement, useState } from "react"; import { twMerge } from "tailwind-merge"; import { FiChevronDown, FiChevronRight } from "react-icons/fi"; import { useTreeItem, useTreeView } from "./hook"; import { TreeItemContext } from "./context"; import { usePlacement } from "@ducor/hooks"; const TreeItem = (_a) => { var { as: component = "a", label = "untitled", children, icon } = _a, props = __rest(_a, ["as", "label", "children", "icon"]); const [isOpen, setIsOpen] = useState(false); const { refs, styles } = usePlacement({ isOpen, // strategy: 'absolute', // allowedPlacements: [ 'left','right', 'bottom', 'top'], defaultPlacement: "right", defaultAlignment: "end", strategy: "absolute", }); const { expandedPaths, activePath, setActivePath, addExpandedPath, removeExpandedPath, } = useTreeView(); const parentitem = useTreeItem(); const dept = (parentitem ? parentitem.dept : -1) + 1; const value = { ref: refs.reference, dept, // isExpanded, removeExpandedPath: () => { // removeExpandedPath(path); if (parentitem) { parentitem.removeExpandedPath(); } }, addExpandedPath: () => { // addExpandedPath(path); if (parentitem) { parentitem.addExpandedPath(); } }, }; const [childPosition, setChildPosition] = useState({}); const handleClick = (e) => { if ((props.href === null && props.to === null) || (props.href === undefined && props.to === undefined)) { e.preventDefault(); } setIsOpen(!isOpen); }; return (_jsx(TreeItemContext.Provider, { value: value, children: _jsxs("li", { className: twMerge("tree-item"), role: 'treeitem', "tab-index": '-1', // data-active={isActive} // data-expanded={isExpanded} onMouseEnter: (e) => { const rect = e.currentTarget.getBoundingClientRect(); if (rect) { // setIsTop(rect.top < window.innerHeight / 2); setChildPosition(rect); } else { // console.log("error"); } }, onMouseLeave: () => { setChildPosition({}); }, children: [createElement(component, Object.assign({ // className: 'flex justify-between items-center gap-2', onClick: handleClick, ref: refs.reference }, props), [ icon && _jsx("span", { className: 'tree-item-suffix', children: icon }), _jsx("span", { className: 'tree-item-label', children: label }), children ? (_jsx("span", { className: twMerge("tree-subitem-arrow" // isExpanded ? "rotate-90" : "rotate-0", ), children: expandedPaths.includes(label) ? (_jsx(FiChevronDown, {})) : (_jsx(FiChevronRight, {})) })) : null, ]), children ? (_jsx("ul", { ref: refs.content, "data-dept": dept + 1, className: 'tree-group', role: 'group', style: styles, children: children })) : null] }) })); }; export default TreeItem;