| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.Menu = void 0; |
| 4 |
const tslib_1 = require("tslib"); |
| 5 |
const jsx_runtime_1 = require("react/jsx-runtime"); |
| 6 |
const react_1 = require("react"); |
| 7 |
const menu_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Menu/menu")); |
| 8 |
const breadcrumb_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Breadcrumb/breadcrumb")); |
| 9 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 10 |
const helpers_1 = require("../../helpers"); |
| 11 |
const SSRSafeIds_1 = require("../../helpers/SSRSafeIds/SSRSafeIds"); |
| 12 |
const MenuContext_1 = require("./MenuContext"); |
| 13 |
const util_1 = require("../../helpers/util"); |
| 14 |
const helpers_2 = require("../../helpers"); |
| 15 |
class MenuBase extends react_1.Component { |
| 16 |
constructor(props) { |
| 17 |
super(props); |
| 18 |
this.menuRef = (0, react_1.createRef)(); |
| 19 |
this.activeMenu = null; |
| 20 |
this.state = { |
| 21 |
transitionMoveTarget: null, |
| 22 |
flyoutRef: null, |
| 23 |
currentDrilldownMenuId: this.props.id |
| 24 |
}; |
| 25 |
this.handleDrilldownTransition = (event) => { |
| 26 |
const current = this.menuRef.current; |
| 27 |
if (!current || |
| 28 |
(current !== event.target.closest(`.${menu_1.default.menu}`) && |
| 29 |
!Array.from(current.getElementsByClassName(menu_1.default.menu)).includes(event.target.closest(`.${menu_1.default.menu}`)))) { |
| 30 |
return; |
| 31 |
} |
| 32 |
if (this.state.transitionMoveTarget) { |
| 33 |
this.state.transitionMoveTarget.focus(); |
| 34 |
this.setState({ transitionMoveTarget: null }); |
| 35 |
} |
| 36 |
else { |
| 37 |
const nextMenu = current.querySelector('#' + this.props.activeMenu) || current || null; |
| 38 |
const nextMenuLists = nextMenu.getElementsByTagName('UL'); |
| 39 |
if (nextMenuLists.length === 0) { |
| 40 |
return; |
| 41 |
} |
| 42 |
const nextMenuChildren = Array.from(nextMenuLists[0].children); |
| 43 |
if (!this.state.currentDrilldownMenuId || nextMenu.id !== this.state.currentDrilldownMenuId) { |
| 44 |
this.setState({ currentDrilldownMenuId: nextMenu.id }); |
| 45 |
} |
| 46 |
else { |
| 47 |
// if the drilldown transition ends on the same menu, do not focus the first item |
| 48 |
return; |
| 49 |
} |
| 50 |
const nextTarget = nextMenuChildren.filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(menu_1.default.divider)))[0].firstChild; |
| 51 |
nextTarget.focus(); |
| 52 |
nextTarget.tabIndex = 0; |
| 53 |
} |
| 54 |
}; |
| 55 |
this.handleExtraKeys = (event) => { |
| 56 |
const isDrilldown = this.props.containsDrilldown; |
| 57 |
const activeElement = document.activeElement; |
| 58 |
if (event.target.closest(`.${menu_1.default.menu}`) !== this.activeMenu && |
| 59 |
!event.target.classList.contains(breadcrumb_1.default.breadcrumbLink)) { |
| 60 |
this.activeMenu = event.target.closest(`.${menu_1.default.menu}`); |
| 61 |
} |
| 62 |
if (event.target.tagName === 'INPUT') { |
| 63 |
return; |
| 64 |
} |
| 65 |
const parentMenu = this.activeMenu; |
| 66 |
const key = event.key; |
| 67 |
const isFromBreadcrumb = activeElement.classList.contains(breadcrumb_1.default.breadcrumbLink); |
| 68 |
if (key === ' ' || key === 'Enter') { |
| 69 |
event.preventDefault(); |
| 70 |
if (isDrilldown && !isFromBreadcrumb) { |
| 71 |
const isDrillingOut = activeElement.closest('li').classList.contains('pf-m-current-path'); |
| 72 |
if (isDrillingOut && parentMenu.parentElement.tagName === 'LI') { |
| 73 |
activeElement.tabIndex = -1; |
| 74 |
parentMenu.parentElement.firstChild.tabIndex = 0; |
| 75 |
this.setState({ transitionMoveTarget: parentMenu.parentElement.firstChild }); |
| 76 |
} |
| 77 |
else { |
| 78 |
if (activeElement.nextElementSibling && activeElement.nextElementSibling.classList.contains(menu_1.default.menu)) { |
| 79 |
const childItems = Array.from(activeElement.nextElementSibling.getElementsByTagName('UL')[0].children).filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(menu_1.default.divider))); |
| 80 |
activeElement.tabIndex = -1; |
| 81 |
childItems[0].firstChild.tabIndex = 0; |
| 82 |
this.setState({ transitionMoveTarget: childItems[0].firstChild }); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
document.activeElement.click(); |
| 87 |
} |
| 88 |
}; |
| 89 |
this.createNavigableElements = () => { |
| 90 |
const isDrilldown = this.props.containsDrilldown; |
| 91 |
if (isDrilldown) { |
| 92 |
return this.activeMenu |
| 93 |
? Array.from(this.activeMenu.getElementsByTagName('UL')[0].children).filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(menu_1.default.divider))) |
| 94 |
: []; |
| 95 |
} |
| 96 |
else { |
| 97 |
return this.menuRef.current |
| 98 |
? Array.from(this.menuRef.current.getElementsByTagName('LI')).filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(menu_1.default.divider))) |
| 99 |
: []; |
| 100 |
} |
| 101 |
}; |
| 102 |
if (props.innerRef) { |
| 103 |
this.menuRef = props.innerRef; |
| 104 |
} |
| 105 |
} |
| 106 |
allowTabFirstItem() { |
| 107 |
// Allow tabbing to first menu item |
| 108 |
const current = this.menuRef.current; |
| 109 |
if (current) { |
| 110 |
const first = current.querySelector('ul button:not(:disabled), ul a:not(:disabled)'); |
| 111 |
if (first) { |
| 112 |
first.tabIndex = 0; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
componentDidMount() { |
| 117 |
if (util_1.canUseDOM && this.props.containsDrilldown && this.props.isRootMenu) { |
| 118 |
window.addEventListener('transitionend', this.handleDrilldownTransition); |
| 119 |
} |
| 120 |
this.allowTabFirstItem(); |
| 121 |
} |
| 122 |
componentWillUnmount() { |
| 123 |
if (util_1.canUseDOM && this.props.containsDrilldown && this.props.isRootMenu) { |
| 124 |
window.removeEventListener('transitionend', this.handleDrilldownTransition); |
| 125 |
} |
| 126 |
} |
| 127 |
componentDidUpdate(prevProps) { |
| 128 |
if (prevProps.children !== this.props.children) { |
| 129 |
this.allowTabFirstItem(); |
| 130 |
} |
| 131 |
} |
| 132 |
render() { |
| 133 |
const _a = this.props, { id, children, className, onSelect, selected = null, onActionClick, ouiaId, ouiaSafe, containsFlyout, containsDrilldown, isMenuDrilledIn, isPlain, isScrollable, drilldownItemPath, drilledInMenus, onDrillIn, onDrillOut, onGetMenuHeight, parentMenu = null, activeItemId = null, |
| 134 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 135 |
innerRef, isRootMenu, activeMenu, role, isNavFlyout } = _a, |
| 136 |
/* eslint-enable @typescript-eslint/no-unused-vars */ |
| 137 |
props = tslib_1.__rest(_a, ["id", "children", "className", "onSelect", "selected", "onActionClick", "ouiaId", "ouiaSafe", "containsFlyout", "containsDrilldown", "isMenuDrilledIn", "isPlain", "isScrollable", "drilldownItemPath", "drilledInMenus", "onDrillIn", "onDrillOut", "onGetMenuHeight", "parentMenu", "activeItemId", "innerRef", "isRootMenu", "activeMenu", "role", "isNavFlyout"]); |
| 138 |
const _isMenuDrilledIn = isMenuDrilledIn || (drilledInMenus && drilledInMenus.includes(id)) || false; |
| 139 |
return ((0, jsx_runtime_1.jsx)(SSRSafeIds_1.SSRSafeIds, { prefix: "pf-", ouiaComponentType: exports.Menu.displayName, children: (_, generatedOuiaId) => { |
| 140 |
var _a, _b; |
| 141 |
return ((0, jsx_runtime_1.jsxs)(MenuContext_1.MenuContext.Provider, { value: { |
| 142 |
menuId: id, |
| 143 |
parentMenu: parentMenu || id, |
| 144 |
onSelect, |
| 145 |
onActionClick, |
| 146 |
activeItemId, |
| 147 |
selected, |
| 148 |
drilledInMenus, |
| 149 |
drilldownItemPath, |
| 150 |
onDrillIn, |
| 151 |
onDrillOut, |
| 152 |
onGetMenuHeight, |
| 153 |
flyoutRef: this.state.flyoutRef, |
| 154 |
setFlyoutRef: (flyoutRef) => this.setState({ flyoutRef }), |
| 155 |
disableHover: (_b = (_a = this.context) === null || _a === void 0 ? void 0 : _a.disableHover) !== null && _b !== void 0 ? _b : false, |
| 156 |
role |
| 157 |
}, children: [isRootMenu && ((0, jsx_runtime_1.jsx)(helpers_2.KeyboardHandler, { containerRef: this.menuRef || null, additionalKeyHandler: this.handleExtraKeys, createNavigableElements: this.createNavigableElements, isActiveElement: (element) => document.activeElement.closest('li') === element || // if element is a basic MenuItem |
| 158 |
document.activeElement.parentElement === element || |
| 159 |
document.activeElement.closest(`.${menu_1.default.menuSearch}`) === element || // if element is a MenuSearch |
| 160 |
(document.activeElement.closest('ol') && document.activeElement.closest('ol').firstChild === element), getFocusableElement: (navigableElement) => { |
| 161 |
var _a, _b; |
| 162 |
return ((navigableElement === null || navigableElement === void 0 ? void 0 : navigableElement.tagName) === 'DIV' && navigableElement.querySelector('input')) || // for MenuSearchInput |
| 163 |
(((_a = navigableElement.firstChild) === null || _a === void 0 ? void 0 : _a.tagName) === 'LABEL' && |
| 164 |
navigableElement.querySelector('input')) || // for MenuItem checkboxes |
| 165 |
(((_b = navigableElement.firstChild) === null || _b === void 0 ? void 0 : _b.tagName) === 'DIV' && |
| 166 |
navigableElement.querySelector('a, button, input')) || // For aria-disabled element that is rendered inside a div with "display: contents" styling |
| 167 |
navigableElement.firstChild; |
| 168 |
}, noHorizontalArrowHandling: document.activeElement && |
| 169 |
(document.activeElement.classList.contains(breadcrumb_1.default.breadcrumbLink) || |
| 170 |
document.activeElement.tagName === 'INPUT'), noEnterHandling: true, noSpaceHandling: true })), (0, jsx_runtime_1.jsx)("div", Object.assign({ id: id, className: (0, react_styles_1.css)(menu_1.default.menu, isPlain && menu_1.default.modifiers.plain, isScrollable && menu_1.default.modifiers.scrollable, containsFlyout && menu_1.default.modifiers.flyout, isNavFlyout && 'pf-m-nav', containsDrilldown && menu_1.default.modifiers.drilldown, _isMenuDrilledIn && menu_1.default.modifiers.drilledIn, className), ref: this.menuRef }, (0, helpers_1.getOUIAProps)(exports.Menu.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe), props, { children: children }))] })); |
| 171 |
} })); |
| 172 |
} |
| 173 |
} |
| 174 |
MenuBase.displayName = 'Menu'; |
| 175 |
MenuBase.contextType = MenuContext_1.MenuContext; |
| 176 |
MenuBase.defaultProps = { |
| 177 |
ouiaSafe: true, |
| 178 |
isRootMenu: true, |
| 179 |
isPlain: false, |
| 180 |
isScrollable: false, |
| 181 |
role: 'menu' |
| 182 |
}; |
| 183 |
exports.Menu = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(MenuBase, Object.assign({}, props, { innerRef: ref })))); |
| 184 |
exports.Menu.displayName = 'Menu'; |
| 185 |
//# sourceMappingURL=Menu.js.map |