pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+130
@@ -0,0 +1,130 @@
|
||||
'use client';
|
||||
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import * as React from 'react';
|
||||
import { unstable_useId as useId, unstable_useForkRef as useForkRef } from '@mui/utils';
|
||||
import { useButton } from '../useButton';
|
||||
import { useListItem } from '../useList';
|
||||
import { DropdownActionTypes } from '../useDropdown';
|
||||
import { DropdownContext } from '../useDropdown/DropdownContext';
|
||||
import { combineHooksSlotProps } from '../utils/combineHooksSlotProps';
|
||||
import { useCompoundItem } from '../useCompound';
|
||||
import { extractEventHandlers } from '../utils/extractEventHandlers';
|
||||
function idGenerator(existingKeys) {
|
||||
return `menu-item-${existingKeys.size}`;
|
||||
}
|
||||
const FALLBACK_MENU_CONTEXT = {
|
||||
dispatch: () => {},
|
||||
popupId: '',
|
||||
registerPopup: () => {},
|
||||
registerTrigger: () => {},
|
||||
state: {
|
||||
open: true,
|
||||
changeReason: null
|
||||
},
|
||||
triggerElement: null
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Menu](https://mui.com/base-ui/react-menu/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useMenuItem API](https://mui.com/base-ui/react-menu/hooks-api/#use-menu-item)
|
||||
*/
|
||||
export function useMenuItem(params) {
|
||||
var _React$useContext;
|
||||
const {
|
||||
disabled = false,
|
||||
id: idParam,
|
||||
rootRef: externalRef,
|
||||
label,
|
||||
disableFocusOnHover = false
|
||||
} = params;
|
||||
const id = useId(idParam);
|
||||
const itemRef = React.useRef(null);
|
||||
const itemMetadata = React.useMemo(() => ({
|
||||
disabled,
|
||||
id: id != null ? id : '',
|
||||
label,
|
||||
ref: itemRef
|
||||
}), [disabled, id, label]);
|
||||
const {
|
||||
dispatch
|
||||
} = (_React$useContext = React.useContext(DropdownContext)) != null ? _React$useContext : FALLBACK_MENU_CONTEXT;
|
||||
const {
|
||||
getRootProps: getListRootProps,
|
||||
highlighted
|
||||
} = useListItem({
|
||||
item: id,
|
||||
handlePointerOverEvents: !disableFocusOnHover
|
||||
});
|
||||
const {
|
||||
index,
|
||||
totalItemCount
|
||||
} = useCompoundItem(id != null ? id : idGenerator, itemMetadata);
|
||||
const {
|
||||
getRootProps: getButtonProps,
|
||||
focusVisible,
|
||||
rootRef: buttonRefHandler
|
||||
} = useButton({
|
||||
disabled,
|
||||
focusableWhenDisabled: true
|
||||
});
|
||||
const handleRef = useForkRef(buttonRefHandler, externalRef, itemRef);
|
||||
React.useDebugValue({
|
||||
id,
|
||||
highlighted,
|
||||
disabled,
|
||||
label
|
||||
});
|
||||
const createHandleClick = otherHandlers => event => {
|
||||
var _otherHandlers$onClic;
|
||||
(_otherHandlers$onClic = otherHandlers.onClick) == null || _otherHandlers$onClic.call(otherHandlers, event);
|
||||
if (event.defaultMuiPrevented) {
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: DropdownActionTypes.close,
|
||||
event
|
||||
});
|
||||
};
|
||||
const getOwnHandlers = (otherHandlers = {}) => _extends({}, otherHandlers, {
|
||||
onClick: createHandleClick(otherHandlers)
|
||||
});
|
||||
function getRootProps(externalProps = {}) {
|
||||
const externalEventHandlers = extractEventHandlers(externalProps);
|
||||
const getCombinedRootProps = combineHooksSlotProps(getOwnHandlers, combineHooksSlotProps(getButtonProps, getListRootProps));
|
||||
return _extends({}, externalProps, externalEventHandlers, getCombinedRootProps(externalEventHandlers), {
|
||||
id,
|
||||
ref: handleRef,
|
||||
role: 'menuitem'
|
||||
});
|
||||
}
|
||||
|
||||
// If `id` is undefined (during SSR in React < 18), we fall back to rendering a simplified menu item
|
||||
// which does not have access to infortmation about its position or highlighted state.
|
||||
if (id === undefined) {
|
||||
return {
|
||||
getRootProps,
|
||||
disabled: false,
|
||||
focusVisible,
|
||||
highlighted: false,
|
||||
index: -1,
|
||||
totalItemCount: 0,
|
||||
rootRef: handleRef
|
||||
};
|
||||
}
|
||||
return {
|
||||
getRootProps,
|
||||
disabled,
|
||||
focusVisible,
|
||||
highlighted,
|
||||
index,
|
||||
totalItemCount,
|
||||
rootRef: handleRef
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user