mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-04 17:09:46 +08:00
32 lines
709 B
TypeScript
32 lines
709 B
TypeScript
import type { MouseEvent } from 'react';
|
|
import React, { forwardRef, startTransition } from 'react';
|
|
import { useNavigate } from 'dumi';
|
|
|
|
export type LinkProps = {
|
|
to?: string;
|
|
children?: React.ReactNode;
|
|
className?: string;
|
|
};
|
|
|
|
const Link = forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => {
|
|
const { to, children, ...rest } = props;
|
|
const navigate = useNavigate();
|
|
|
|
const handleClick = (e: MouseEvent<HTMLAnchorElement>) => {
|
|
if (!to.startsWith('http')) {
|
|
e.preventDefault();
|
|
startTransition(() => {
|
|
navigate(to);
|
|
});
|
|
}
|
|
};
|
|
|
|
return (
|
|
<a ref={ref} href={to} onClick={handleClick} {...rest}>
|
|
{children}
|
|
</a>
|
|
);
|
|
});
|
|
|
|
export default Link;
|