UNPKG

mui-component

Version:

some custom mui components

308 lines 15.4 kB
var _excluded = ["children", "className", "collapseIcon", "ContentComponent", "ContentProps", "endIcon", "expandIcon", "disabled", "icon", "id", "label", "nodeId", "onClick", "onMouseDown", "TransitionComponent", "TransitionProps"]; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } //@ts-nocheck import React from "react"; import clsx from "classnames"; import Collapse from "@mui/material/Collapse"; import { alpha, styled, useThemeProps } from "@mui/material/styles"; import { ownerDocument, useForkRef } from "@mui/material/utils"; import { unstable_composeClasses as composeClasses } from "@mui/material"; import TreeViewContext from "../TreeView/TreeViewContext"; import { DescendantProvider, useDescendant } from "../TreeView/descendants"; import { TreeItemContent } from "./TreeItemContent"; import { treeItemClasses, getTreeItemUtilityClass } from "./treeItemClasses"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var useUtilityClasses = function useUtilityClasses(ownerState) { var classes = ownerState.classes; var slots = { root: ["root"], content: ["content"], expanded: ["expanded"], selected: ["selected"], focused: ["focused"], disabled: ["disabled"], iconContainer: ["iconContainer"], label: ["label"], group: ["group"] }; return composeClasses(slots, getTreeItemUtilityClass, classes); }; var TreeItemRoot = styled("li", { name: "MuiTreeItem", slot: "Root", overridesResolver: function overridesResolver(props, styles) { return styles.root; } })({ listStyle: "none", margin: 0, padding: 0, outline: 0 }); var StyledTreeItemContent = styled(TreeItemContent, { name: "MuiTreeItem", slot: "Content", overridesResolver: function overridesResolver(props, styles) { return [styles.content, styles.iconContainer && _defineProperty({}, "& .".concat(treeItemClasses.iconContainer), styles.iconContainer), styles.label && _defineProperty({}, "& .".concat(treeItemClasses.label), styles.label)]; } })(function (_ref3) { var theme = _ref3.theme; return _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({ padding: "0 8px", width: "100%", display: "flex", alignItems: "center", cursor: "pointer", WebkitTapHighlightColor: "transparent", "&:hover": { backgroundColor: (theme.vars || theme).palette.action.hover, // Reset on touch devices, it doesn't add specificity "@media (hover: none)": { backgroundColor: "transparent" } } }, "&.".concat(treeItemClasses.disabled), { opacity: (theme.vars || theme).palette.action.disabledOpacity, backgroundColor: "transparent" }), "&.".concat(treeItemClasses.focused), { backgroundColor: (theme.vars || theme).palette.action.focus }), "&.".concat(treeItemClasses.selected), _defineProperty({ backgroundColor: theme.vars ? "rgba(".concat(theme.vars.palette.primary.mainChannel, " / ").concat(theme.vars.palette.action.selectedOpacity, ")") : alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity), "&:hover": { backgroundColor: theme.vars ? "rgba(".concat(theme.vars.palette.primary.mainChannel, " / calc(").concat(theme.vars.palette.action.selectedOpacity, " + ").concat(theme.vars.palette.action.hoverOpacity, "))") : alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity), // Reset on touch devices, it doesn't add specificity "@media (hover: none)": { backgroundColor: theme.vars ? "rgba(".concat(theme.vars.palette.primary.mainChannel, " / ").concat(theme.vars.palette.action.selectedOpacity, ")") : alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity) } } }, "&.".concat(treeItemClasses.focused), { backgroundColor: theme.vars ? "rgba(".concat(theme.vars.palette.primary.mainChannel, " / calc(").concat(theme.vars.palette.action.selectedOpacity, " + ").concat(theme.vars.palette.action.focusOpacity, "))") : alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity) })), "& .".concat(treeItemClasses.iconContainer), { marginRight: 4, width: 15, display: "flex", flexShrink: 0, justifyContent: "center", "& svg": { fontSize: 18 } }), "& .".concat(treeItemClasses.label), _objectSpread({ width: "100%", // fixes overflow - see https://github.com/mui/material-ui/issues/27372 minWidth: 0, paddingLeft: 4, position: "relative" }, theme.typography.body1)); }); var TreeItemGroup = styled(Collapse, { name: "MuiTreeItem", slot: "Group", overridesResolver: function overridesResolver(props, styles) { return styles.group; } })({ margin: 0, padding: 0, marginLeft: 17 }); export var TreeItem = /*#__PURE__*/React.forwardRef(function TreeItem(inProps, ref) { var props = useThemeProps({ props: inProps, name: "MuiTreeItem" }); var children = props.children, className = props.className, collapseIcon = props.collapseIcon, _props$ContentCompone = props.ContentComponent, ContentComponent = _props$ContentCompone === void 0 ? TreeItemContent : _props$ContentCompone, ContentProps = props.ContentProps, endIcon = props.endIcon, expandIcon = props.expandIcon, disabledProp = props.disabled, icon = props.icon, idProp = props.id, label = props.label, nodeId = props.nodeId, onClick = props.onClick, onMouseDown = props.onMouseDown, _props$TransitionComp = props.TransitionComponent, TransitionComponent = _props$TransitionComp === void 0 ? Collapse : _props$TransitionComp, TransitionProps = props.TransitionProps, other = _objectWithoutProperties(props, _excluded); var _React$useContext = React.useContext(TreeViewContext), _React$useContext$ico = _React$useContext.icons, contextIcons = _React$useContext$ico === void 0 ? {} : _React$useContext$ico, focus = _React$useContext.focus, isExpanded = _React$useContext.isExpanded, isFocused = _React$useContext.isFocused, isSelected = _React$useContext.isSelected, isDisabled = _React$useContext.isDisabled, multiSelect = _React$useContext.multiSelect, disabledItemsFocusable = _React$useContext.disabledItemsFocusable, mapFirstChar = _React$useContext.mapFirstChar, unMapFirstChar = _React$useContext.unMapFirstChar, registerNode = _React$useContext.registerNode, unregisterNode = _React$useContext.unregisterNode, treeId = _React$useContext.treeId; var id = null; if (idProp != null) { id = idProp; } else if (treeId && nodeId) { id = "".concat(treeId, "-").concat(nodeId); } var _React$useState = React.useState(null), _React$useState2 = _slicedToArray(_React$useState, 2), treeitemElement = _React$useState2[0], setTreeitemElement = _React$useState2[1]; var contentRef = React.useRef(null); var handleRef = useForkRef(setTreeitemElement, ref); var descendant = React.useMemo(function () { return { element: treeitemElement, id: nodeId }; }, [nodeId, treeitemElement]); var _useDescendant = useDescendant(descendant), index = _useDescendant.index, parentId = _useDescendant.parentId; var expandable = Boolean(Array.isArray(children) ? children.length : children); var expanded = isExpanded ? isExpanded(nodeId) : false; var focused = isFocused ? isFocused(nodeId) : false; var selected = isSelected ? isSelected(nodeId) : false; var disabled = isDisabled ? isDisabled(nodeId) : false; var ownerState = _objectSpread(_objectSpread({}, props), {}, { expanded: expanded, focused: focused, selected: selected, disabled: disabled }); var classes = useUtilityClasses(ownerState); var displayIcon; var expansionIcon; if (expandable) { if (!expanded) { expansionIcon = expandIcon || contextIcons.defaultExpandIcon; } else { expansionIcon = collapseIcon || contextIcons.defaultCollapseIcon; } } if (expandable) { displayIcon = contextIcons.defaultParentIcon; } else { displayIcon = endIcon || contextIcons.defaultEndIcon; } React.useEffect(function () { // On the first render a node's index will be -1. We want to wait for the real index. if (registerNode && unregisterNode && index !== -1) { registerNode({ id: nodeId, idAttribute: id, index: index, parentId: parentId, expandable: expandable, disabled: disabledProp }); return function () { unregisterNode(nodeId); }; } return undefined; }, [registerNode, unregisterNode, parentId, index, nodeId, expandable, disabledProp, id]); React.useEffect(function () { if (mapFirstChar && unMapFirstChar && label) { mapFirstChar(nodeId, contentRef.current.textContent.substring(0, 1).toLowerCase()); return function () { unMapFirstChar(nodeId); }; } }, [mapFirstChar, unMapFirstChar, nodeId, label]); var ariaSelected; if (multiSelect) { ariaSelected = selected; } else if (selected) { /* single-selection trees unset aria-selected on un-selected items. * * If the tree does not support multiple selection, aria-selected * is set to true for the selected node and it is not present on any other node in the tree. * Source: https://www.w3.org/WAI/ARIA/apg/patterns/treeview/ */ ariaSelected = true; } function handleFocus(event) { // DOM focus stays on the tree which manages focus with aria-activedescendant if (event.target === event.currentTarget) { ownerDocument(event.target).getElementById(treeId).focus({ preventScroll: true }); } var unfocusable = !disabledItemsFocusable && disabled; if (!focused && event.currentTarget === event.target && !unfocusable) { focus(event, nodeId); } } return /*#__PURE__*/_jsxs(TreeItemRoot, _objectSpread(_objectSpread({ className: clsx(classes.root, className), role: "treeitem", "aria-expanded": expandable ? expanded : null, "aria-selected": ariaSelected, "aria-disabled": disabled || null //@ts-ignore , ref: handleRef, id: id, tabIndex: -1 }, other), {}, { ownerState: ownerState, onFocus: handleFocus, children: [/*#__PURE__*/_jsx(StyledTreeItemContent, _objectSpread({ as: ContentComponent, ref: contentRef, classes: { root: classes.content, expanded: classes.expanded, selected: classes.selected, focused: classes.focused, disabled: classes.disabled, iconContainer: classes.iconContainer, label: classes.label }, label: label, nodeId: nodeId, onClick: onClick, onMouseDown: onMouseDown, icon: icon, expansionIcon: expansionIcon, displayIcon: displayIcon //@ts-ignore , ownerState: ownerState }, ContentProps)), children && /*#__PURE__*/_jsx(DescendantProvider, { id: nodeId, children: /*#__PURE__*/_jsx(TreeItemGroup, _objectSpread(_objectSpread({ as: TransitionComponent, unmountOnExit: true, className: classes.group, in: expanded, component: "ul", role: "group" }, TransitionProps), {}, { children: children })) })] })); }); TreeItem.displayName = "MuiTreeItem";