mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-11 23:00:20 +08:00
78923c541a
* docs: Link support open in new tab or window
* docs: apply more Linki
* Revert "docs: apply more Linki"
This reverts commit e19ddd7479
.
35 lines
818 B
TypeScript
35 lines
818 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')) {
|
|
// Should support open in new tab
|
|
if (!e.metaKey && !e.ctrlKey && !e.shiftKey) {
|
|
e.preventDefault();
|
|
startTransition(() => {
|
|
navigate(to);
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
return (
|
|
<a ref={ref} href={to} onClick={handleClick} {...rest}>
|
|
{children}
|
|
</a>
|
|
);
|
|
});
|
|
|
|
export default Link;
|