1 | 'use client';
|
2 |
|
3 | import _extends from "@babel/runtime/helpers/esm/extends";
|
4 | import * as React from 'react';
|
5 | import { unstable_useId as useId, unstable_useForkRef as useForkRef } from '@mui/utils';
|
6 | import { useButton } from '../useButton';
|
7 | import { useListItem } from '../useList';
|
8 | import { DropdownActionTypes } from '../useDropdown';
|
9 | import { DropdownContext } from '../useDropdown/DropdownContext';
|
10 | import { combineHooksSlotProps } from '../utils/combineHooksSlotProps';
|
11 | import { useCompoundItem } from '../useCompound';
|
12 | import { extractEventHandlers } from '../utils/extractEventHandlers';
|
13 | function idGenerator(existingKeys) {
|
14 | return `menu-item-${existingKeys.size}`;
|
15 | }
|
16 | const FALLBACK_MENU_CONTEXT = {
|
17 | dispatch: () => {},
|
18 | popupId: '',
|
19 | registerPopup: () => {},
|
20 | registerTrigger: () => {},
|
21 | state: {
|
22 | open: true,
|
23 | changeReason: null
|
24 | },
|
25 | triggerElement: null
|
26 | };
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | export function useMenuItem(params) {
|
39 | var _React$useContext;
|
40 | const {
|
41 | disabled = false,
|
42 | id: idParam,
|
43 | rootRef: externalRef,
|
44 | label,
|
45 | disableFocusOnHover = false
|
46 | } = params;
|
47 | const id = useId(idParam);
|
48 | const itemRef = React.useRef(null);
|
49 | const itemMetadata = React.useMemo(() => ({
|
50 | disabled,
|
51 | id: id != null ? id : '',
|
52 | label,
|
53 | ref: itemRef
|
54 | }), [disabled, id, label]);
|
55 | const {
|
56 | dispatch
|
57 | } = (_React$useContext = React.useContext(DropdownContext)) != null ? _React$useContext : FALLBACK_MENU_CONTEXT;
|
58 | const {
|
59 | getRootProps: getListRootProps,
|
60 | highlighted
|
61 | } = useListItem({
|
62 | item: id,
|
63 | handlePointerOverEvents: !disableFocusOnHover
|
64 | });
|
65 | const {
|
66 | index,
|
67 | totalItemCount
|
68 | } = useCompoundItem(id != null ? id : idGenerator, itemMetadata);
|
69 | const {
|
70 | getRootProps: getButtonProps,
|
71 | focusVisible,
|
72 | rootRef: buttonRefHandler
|
73 | } = useButton({
|
74 | disabled,
|
75 | focusableWhenDisabled: true
|
76 | });
|
77 | const handleRef = useForkRef(buttonRefHandler, externalRef, itemRef);
|
78 | React.useDebugValue({
|
79 | id,
|
80 | highlighted,
|
81 | disabled,
|
82 | label
|
83 | });
|
84 | const createHandleClick = otherHandlers => event => {
|
85 | var _otherHandlers$onClic;
|
86 | (_otherHandlers$onClic = otherHandlers.onClick) == null || _otherHandlers$onClic.call(otherHandlers, event);
|
87 | if (event.defaultMuiPrevented) {
|
88 | return;
|
89 | }
|
90 | dispatch({
|
91 | type: DropdownActionTypes.close,
|
92 | event
|
93 | });
|
94 | };
|
95 | const getOwnHandlers = (otherHandlers = {}) => _extends({}, otherHandlers, {
|
96 | onClick: createHandleClick(otherHandlers)
|
97 | });
|
98 | function getRootProps(externalProps = {}) {
|
99 | const externalEventHandlers = extractEventHandlers(externalProps);
|
100 | const getCombinedRootProps = combineHooksSlotProps(getOwnHandlers, combineHooksSlotProps(getButtonProps, getListRootProps));
|
101 | return _extends({}, externalProps, externalEventHandlers, getCombinedRootProps(externalEventHandlers), {
|
102 | id,
|
103 | ref: handleRef,
|
104 | role: 'menuitem'
|
105 | });
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 | if (id === undefined) {
|
111 | return {
|
112 | getRootProps,
|
113 | disabled: false,
|
114 | focusVisible,
|
115 | highlighted: false,
|
116 | index: -1,
|
117 | totalItemCount: 0,
|
118 | rootRef: handleRef
|
119 | };
|
120 | }
|
121 | return {
|
122 | getRootProps,
|
123 | disabled,
|
124 | focusVisible,
|
125 | highlighted,
|
126 | index,
|
127 | totalItemCount,
|
128 | rootRef: handleRef
|
129 | };
|
130 | } |
\ | No newline at end of file |