22 lines
566 B
TypeScript
Raw Normal View History

2023-12-06 13:56:43 +08:00
import { AnchorHTMLAttributes } from "react";
interface IAnchorProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
useRef?: React.LegacyRef<HTMLAnchorElement>
}
/** 阻止a標籤的href產生網頁跳轉 */
export default function A(props: IAnchorProps) {
const { useRef, children, onClick } = props;
function handleClick(event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) {
event.preventDefault(); // 阻止默认点击行为
onClick && onClick(event);
}
return (
<a {...props} ref={useRef} onClick={handleClick}>
{children}
</a>
);
}