2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-09-11 17:28:04 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import { devUseWarning } from '../_util/warning';
|
2023-06-18 18:55:36 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { AntAnchor } from './Anchor';
|
2020-04-29 20:22:16 +08:00
|
|
|
import AnchorContext from './context';
|
2016-10-28 14:02:55 +08:00
|
|
|
|
2022-11-30 15:55:43 +08:00
|
|
|
export interface AnchorLinkBaseProps {
|
2016-11-03 16:45:13 +08:00
|
|
|
prefixCls?: string;
|
2017-07-18 18:04:48 +08:00
|
|
|
href: string;
|
2019-09-03 20:40:39 +08:00
|
|
|
target?: string;
|
2017-03-29 10:29:43 +08:00
|
|
|
title: React.ReactNode;
|
2019-01-10 15:22:54 +08:00
|
|
|
className?: string;
|
2023-06-18 18:55:36 +08:00
|
|
|
replace?: boolean;
|
2016-10-28 14:02:55 +08:00
|
|
|
}
|
|
|
|
|
2022-11-30 15:55:43 +08:00
|
|
|
export interface AnchorLinkProps extends AnchorLinkBaseProps {
|
|
|
|
children?: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const AnchorLink: React.FC<AnchorLinkProps> = (props) => {
|
2023-06-18 18:55:36 +08:00
|
|
|
const {
|
|
|
|
href,
|
|
|
|
title,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
target,
|
|
|
|
replace,
|
|
|
|
} = props;
|
2016-11-14 15:43:55 +08:00
|
|
|
|
2022-10-17 21:36:56 +08:00
|
|
|
const context = React.useContext<AntAnchor | undefined>(AnchorContext);
|
2016-11-14 15:43:55 +08:00
|
|
|
|
2022-12-27 17:14:35 +08:00
|
|
|
const { registerLink, unregisterLink, scrollTo, onClick, activeLink, direction } = context || {};
|
2018-07-16 14:20:41 +08:00
|
|
|
|
2022-10-17 21:36:56 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
registerLink?.(href);
|
|
|
|
return () => {
|
|
|
|
unregisterLink?.(href);
|
|
|
|
};
|
2023-03-10 15:54:05 +08:00
|
|
|
}, [href]);
|
2016-11-09 14:22:38 +08:00
|
|
|
|
2022-12-06 18:18:38 +08:00
|
|
|
const handleClick = (e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
|
2024-05-30 20:49:30 +08:00
|
|
|
onClick?.(e, { title, href });
|
|
|
|
scrollTo?.(href);
|
2023-06-18 18:55:36 +08:00
|
|
|
if (replace) {
|
|
|
|
e.preventDefault();
|
|
|
|
window.location.replace(href);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-11-09 14:40:31 +08:00
|
|
|
|
2022-12-27 17:14:35 +08:00
|
|
|
// =================== Warning =====================
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Anchor.Link');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-12-27 17:14:35 +08:00
|
|
|
warning(
|
|
|
|
!children || direction !== 'horizontal',
|
2023-09-11 17:28:04 +08:00
|
|
|
'usage',
|
2022-12-27 17:14:35 +08:00
|
|
|
'`Anchor.Link children` is not supported when `Anchor` direction is horizontal',
|
|
|
|
);
|
|
|
|
}
|
2022-12-25 18:34:42 +08:00
|
|
|
|
2023-03-14 10:54:46 +08:00
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('anchor', customizePrefixCls);
|
|
|
|
|
|
|
|
const active = activeLink === href;
|
|
|
|
|
|
|
|
const wrapperClassName = classNames(`${prefixCls}-link`, className, {
|
|
|
|
[`${prefixCls}-link-active`]: active,
|
|
|
|
});
|
|
|
|
|
|
|
|
const titleClassName = classNames(`${prefixCls}-link-title`, {
|
|
|
|
[`${prefixCls}-link-title-active`]: active,
|
|
|
|
});
|
|
|
|
|
2022-10-17 21:36:56 +08:00
|
|
|
return (
|
2023-03-14 10:54:46 +08:00
|
|
|
<div className={wrapperClassName}>
|
|
|
|
<a
|
|
|
|
className={titleClassName}
|
|
|
|
href={href}
|
|
|
|
title={typeof title === 'string' ? title : ''}
|
|
|
|
target={target}
|
|
|
|
onClick={handleClick}
|
|
|
|
>
|
|
|
|
{title}
|
|
|
|
</a>
|
|
|
|
{direction !== 'horizontal' ? children : null}
|
|
|
|
</div>
|
2022-10-17 21:36:56 +08:00
|
|
|
);
|
|
|
|
};
|
2019-02-02 18:30:40 +08:00
|
|
|
|
|
|
|
export default AnchorLink;
|