Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(use-aria-menu): link logic in useMenuItem #3229

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-toes-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/use-aria-menu": patch
---

fix link logic in useMenuItem (#2935)
38 changes: 31 additions & 7 deletions packages/hooks/use-aria-menu/src/use-menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import {
PressEvents,
RouterOptions,
} from "@react-types/shared";
import {chain, filterDOMProps, mergeProps, useRouter, useSlotId} from "@react-aria/utils";
import {
chain,
filterDOMProps,
mergeProps,
useLinkProps,
useRouter,
useSlotId,
} from "@react-aria/utils";
import {getItemCount} from "@react-stately/collections";
import {isFocusVisible, useFocus, useHover, useKeyboard, usePress} from "@react-aria/interactions";
import {RefObject} from "react";
Expand Down Expand Up @@ -135,7 +142,7 @@ export function useMenuItem<T>(

let isTrigger = !!hasPopup;
// @ts-ignore
let isDisabled = props.isDisabled ?? state.disabledKeys.has(key);
let isDisabled = props.isDisabled ?? state.selectionManager.isDisabled(key);
// @ts-ignore
let isSelected = props.isSelected ?? state.selectionManager.isSelected(key);
let data = menuData.get(state);
Expand All @@ -144,16 +151,28 @@ export function useMenuItem<T>(
// @ts-ignore
let onClose = props.onClose || data.onClose;
// @ts-ignore
let onAction = isTrigger ? () => {} : props.onAction || data.onAction;
let router = useRouter();
let performAction = (e: PressEvent) => {
if (onAction) {
if (isTrigger) {
return;
}

if (item?.props?.onAction) {
item.props.onAction();
}

if (props.onAction) {
// @ts-ignore
onAction(key);
props.onAction(key);
// @ts-ignore
} else if (data.onAction) {
// @ts-ignore
data.onAction(key);
}

if (e.target instanceof HTMLAnchorElement) {
router.open(e.target, e, item?.props.href, item?.props.routerOptions as RouterOptions);
// @ts-ignore
router.open(e.target, e, item.props.href, item.props.routerOptions as RouterOptions);
}
};

Expand Down Expand Up @@ -308,19 +327,24 @@ export function useMenuItem<T>(
let domProps = filterDOMProps(item.props, {isLink: !!item?.props?.href});

delete domProps.id;
// @ts-ignore
let linkProps = useLinkProps(item.props);
wingkwong marked this conversation as resolved.
Show resolved Hide resolved

return {
menuItemProps: {
...ariaProps,
...mergeProps(
domProps,
linkProps,
isTrigger ? {onFocus: itemProps.onFocus} : itemProps,
pressProps,
hoverProps,
keyboardProps,
focusProps,
{
onClick: chain(onClick, pressProps.onClick),
},
),
onClick: chain(onClick, pressProps.onClick),
tabIndex: itemProps.tabIndex != null ? -1 : undefined,
},
labelProps: {
Expand Down
Loading