refactor: use handleLinkClick from RA's utils (#5632)

This commit is contained in:
WK 2025-08-25 15:40:43 +08:00 committed by GitHub
parent 743505fbc0
commit f8fbe4ba22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 30 deletions

View File

@ -0,0 +1,6 @@
---
"@heroui/pagination": patch
"@heroui/use-aria-link": patch
---
refactor: use handleLinkClick from RA's utils

View File

@ -4,7 +4,7 @@ import type {LinkDOMProps, PressEvent} from "@react-types/shared";
import type {PaginationItemValue} from "@heroui/use-pagination";
import {useMemo} from "react";
import {shouldClientNavigate, useRouter} from "@react-aria/utils";
import {handleLinkClick, useRouter} from "@react-aria/utils";
import {clsx, dataAttr, chain, mergeProps} from "@heroui/shared-utils";
import {filterDOMProps, useDOMRef} from "@heroui/react-utils";
import {useHover, usePress} from "@react-aria/interactions";
@ -109,20 +109,7 @@ export function usePaginationItem(props: UsePaginationItemProps) {
className: clsx(className, props.className),
onClick: (e: React.MouseEvent<HTMLAnchorElement>) => {
chain(pressProps?.onClick, onClick)(e);
// If a custom router is provided, prevent default and forward if this link should client navigate.
if (
!router.isNative &&
e.currentTarget instanceof HTMLAnchorElement &&
e.currentTarget.href &&
// If props are applied to a router Link component, it may have already prevented default.
!e.isDefaultPrevented() &&
shouldClientNavigate(e.currentTarget, e) &&
props.href
) {
e.preventDefault();
router.open(e.currentTarget, e, props.href, props.routerOptions);
}
handleLinkClick(e, router, props.href, props.routerOptions);
},
};
};

View File

@ -6,8 +6,8 @@ import {
filterDOMProps,
mergeProps,
useRouter,
shouldClientNavigate,
useLinkProps,
handleLinkClick,
} from "@react-aria/utils";
import {useFocusable} from "@react-aria/focus";
import {usePress} from "@react-aria/interactions";
@ -79,20 +79,7 @@ export function useAriaLink(props: AriaLinkOptions, ref: RefObject<FocusableElem
"aria-current": props["aria-current"],
onClick: (e: React.MouseEvent<HTMLAnchorElement>) => {
pressProps.onClick?.(e);
// If a custom router is provided, prevent default and forward if this link should client navigate.
if (
!router.isNative &&
e.currentTarget instanceof HTMLAnchorElement &&
e.currentTarget.href &&
// If props are applied to a router Link component, it may have already prevented default.
!e.isDefaultPrevented() &&
shouldClientNavigate(e.currentTarget, e) &&
props.href
) {
e.preventDefault();
router.open(e.currentTarget, e, props.href, props.routerOptions);
}
handleLinkClick(e, router, props.href, props.routerOptions);
},
}),
};