Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Dropdown/Dropdown.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.Dropdown = 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 react_styles_1 = require("@patternfly/react-styles");
8 const Menu_1 = require("../Menu");
9 const Popper_1 = require("../../helpers/Popper/Popper");
10 const helpers_1 = require("../../helpers");
11 const DropdownBase = (_a) => {
12 var { children, className, onSelect, isOpen, toggle, shouldFocusToggleOnSelect = false, onOpenChange, onToggleKeydown, isPlain, isScrollable, innerRef, ouiaId, ouiaSafe = true, zIndex = 9999, popperProps, onOpenChangeKeys = ['Escape', 'Tab'], menuHeight, maxMenuHeight, shouldFocusFirstItemOnOpen = false, shouldPreventScrollOnItemFocus = true, focusTimeoutDelay = 0, containerOuiaId, containerOuiaSafe = true, containerComponent = 'span' } = _a, props = tslib_1.__rest(_a, ["children", "className", "onSelect", "isOpen", "toggle", "shouldFocusToggleOnSelect", "onOpenChange", "onToggleKeydown", "isPlain", "isScrollable", "innerRef", "ouiaId", "ouiaSafe", "zIndex", "popperProps", "onOpenChangeKeys", "menuHeight", "maxMenuHeight", "shouldFocusFirstItemOnOpen", "shouldPreventScrollOnItemFocus", "focusTimeoutDelay", "containerOuiaId", "containerOuiaSafe", "containerComponent"]);
13 const localMenuRef = (0, react_1.useRef)(undefined);
14 const localToggleRef = (0, react_1.useRef)(undefined);
15 const ouiaProps = (0, helpers_1.useOUIAProps)(exports.Dropdown.displayName, ouiaId, ouiaSafe);
16 const ContainerComponent = containerComponent;
17 const containerOuiaProps = (0, helpers_1.useOUIAProps)('Dropdown container', containerOuiaId, containerOuiaSafe);
18 const menuRef = innerRef || localMenuRef;
19 const toggleRef = typeof toggle === 'function' || (typeof toggle !== 'function' && !toggle.toggleRef)
20 ? localToggleRef
21 : toggle === null || toggle === void 0 ? void 0 : toggle.toggleRef;
22 const prevIsOpen = (0, react_1.useRef)(isOpen);
23 (0, react_1.useEffect)(() => {
24 // menu was opened, focus on first menu item
25 if (prevIsOpen.current === false && isOpen === true && shouldFocusFirstItemOnOpen) {
26 setTimeout(() => {
27 var _a;
28 const firstElement = (_a = menuRef === null || menuRef === void 0 ? void 0 : menuRef.current) === null || _a === void 0 ? void 0 : _a.querySelector('li button:not(:disabled),li input:not(:disabled),li a:not([aria-disabled="true"])');
29 firstElement && firstElement.focus({ preventScroll: shouldPreventScrollOnItemFocus });
30 }, focusTimeoutDelay);
31 }
32 prevIsOpen.current = isOpen;
33 // eslint-disable-next-line react-hooks/exhaustive-deps
34 }, [isOpen]);
35 (0, react_1.useEffect)(() => {
36 const handleMenuKeys = (event) => {
37 var _a, _b, _c, _d;
38 // Close the menu on tab or escape if onOpenChange is provided
39 if (isOpen &&
40 onOpenChange &&
41 (((_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target)) || ((_b = toggleRef.current) === null || _b === void 0 ? void 0 : _b.contains(event.target)))) {
42 if (onOpenChangeKeys.includes(event.key)) {
43 onOpenChange(false);
44 (_c = toggleRef.current) === null || _c === void 0 ? void 0 : _c.focus();
45 }
46 }
47 if ((_d = toggleRef.current) === null || _d === void 0 ? void 0 : _d.contains(event.target)) {
48 if (onToggleKeydown) {
49 onToggleKeydown(event);
50 }
51 else if (isOpen) {
52 (0, helpers_1.onToggleArrowKeydownDefault)(event, menuRef);
53 }
54 }
55 };
56 const handleClick = (event) => {
57 var _a, _b;
58 // If the event is not on the toggle and onOpenChange callback is provided, close the menu
59 if (isOpen && onOpenChange && !((_a = toggleRef === null || toggleRef === void 0 ? void 0 : toggleRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target))) {
60 if (isOpen && !((_b = menuRef.current) === null || _b === void 0 ? void 0 : _b.contains(event.target))) {
61 onOpenChange(false);
62 }
63 }
64 };
65 window.addEventListener('keydown', handleMenuKeys);
66 window.addEventListener('click', handleClick);
67 return () => {
68 window.removeEventListener('keydown', handleMenuKeys);
69 window.removeEventListener('click', handleClick);
70 };
71 }, [
72 isOpen,
73 menuRef,
74 toggleRef,
75 onOpenChange,
76 onOpenChangeKeys,
77 onToggleKeydown,
78 shouldPreventScrollOnItemFocus,
79 shouldFocusFirstItemOnOpen,
80 focusTimeoutDelay
81 ]);
82 const scrollable = maxMenuHeight !== undefined || menuHeight !== undefined || isScrollable;
83 const menu = ((0, jsx_runtime_1.jsx)(Menu_1.Menu, Object.assign({ className: (0, react_styles_1.css)(className), ref: menuRef, onSelect: (event, value) => {
84 var _a;
85 onSelect && onSelect(event, value);
86 shouldFocusToggleOnSelect && ((_a = toggleRef.current) === null || _a === void 0 ? void 0 : _a.focus());
87 }, isPlain: isPlain, isScrollable: scrollable }, props, ouiaProps, { children: (0, jsx_runtime_1.jsx)(Menu_1.MenuContent, { menuHeight: menuHeight, maxMenuHeight: maxMenuHeight, children: children }) })));
88 const popper = ((0, jsx_runtime_1.jsx)(Popper_1.Popper, Object.assign({ trigger: typeof toggle === 'function' ? toggle(toggleRef) : toggle.toggleNode, triggerRef: toggleRef, popper: menu, popperRef: menuRef, isVisible: isOpen, zIndex: zIndex }, popperProps)));
89 return containerOuiaId ? (0, jsx_runtime_1.jsx)(ContainerComponent, Object.assign({}, containerOuiaProps, { children: popper })) : popper;
90 };
91 exports.Dropdown = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(DropdownBase, Object.assign({ innerRef: ref }, props))));
92 exports.Dropdown.displayName = 'Dropdown';
93 //# sourceMappingURL=Dropdown.js.map