ant-design/.dumi/theme/common/Link.tsx

32 lines
709 B
TypeScript
Raw Normal View History

import type { MouseEvent } from 'react';
import React, { forwardRef, startTransition } from 'react';
import { useNavigate } from 'dumi';
export type LinkProps = {
to?: string;
children?: React.ReactNode;
2023-04-18 15:29:34 +08:00
className?: string;
};
const Link = forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => {
2023-04-18 15:29:34 +08:00
const { to, children, ...rest } = props;
const navigate = useNavigate();
const handleClick = (e: MouseEvent<HTMLAnchorElement>) => {
if (!to.startsWith('http')) {
e.preventDefault();
startTransition(() => {
navigate(to);
});
}
};
return (
2023-04-18 15:29:34 +08:00
<a ref={ref} href={to} onClick={handleClick} {...rest}>
{children}
</a>
);
});
export default Link;