Debian Codegraph @

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

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