2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-11-09 14:43:12 +08:00
|
|
|
import classNames from 'classnames';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { AntAnchor } from './Anchor';
|
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigConsumer } from '../config-provider';
|
2020-04-29 20:22:16 +08:00
|
|
|
import AnchorContext from './context';
|
2016-10-28 14:02:55 +08:00
|
|
|
|
|
|
|
export interface AnchorLinkProps {
|
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-06-16 20:51:47 +08:00
|
|
|
children?: React.ReactNode;
|
2019-01-10 15:22:54 +08:00
|
|
|
className?: string;
|
2016-10-28 14:02:55 +08:00
|
|
|
}
|
|
|
|
|
2020-04-29 20:22:16 +08:00
|
|
|
class AnchorLink extends React.Component<AnchorLinkProps, any, AntAnchor> {
|
2016-11-03 16:45:13 +08:00
|
|
|
static defaultProps = {
|
2017-07-18 18:04:48 +08:00
|
|
|
href: '#',
|
2016-11-03 16:45:13 +08:00
|
|
|
};
|
|
|
|
|
2020-04-29 20:22:16 +08:00
|
|
|
static contextType = AnchorContext;
|
2016-11-03 16:45:13 +08:00
|
|
|
|
2020-04-29 20:22:16 +08:00
|
|
|
context: AntAnchor;
|
2016-11-14 15:43:55 +08:00
|
|
|
|
|
|
|
componentDidMount() {
|
2020-04-29 20:22:16 +08:00
|
|
|
this.context.registerLink(this.props.href);
|
2016-11-14 15:43:55 +08:00
|
|
|
}
|
|
|
|
|
2019-02-02 18:30:40 +08:00
|
|
|
componentDidUpdate({ href: prevHref }: AnchorLinkProps) {
|
|
|
|
const { href } = this.props;
|
|
|
|
if (prevHref !== href) {
|
2020-04-29 20:22:16 +08:00
|
|
|
this.context.unregisterLink(prevHref);
|
|
|
|
this.context.registerLink(href);
|
2018-07-16 14:20:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
componentWillUnmount() {
|
2020-04-29 20:22:16 +08:00
|
|
|
this.context.unregisterLink(this.props.href);
|
2016-11-03 16:45:13 +08:00
|
|
|
}
|
2016-11-09 14:22:38 +08:00
|
|
|
|
2018-08-28 20:20:46 +08:00
|
|
|
handleClick = (e: React.MouseEvent<HTMLElement>) => {
|
2020-04-29 20:22:16 +08:00
|
|
|
const { scrollTo, onClick } = this.context;
|
2018-08-28 20:20:46 +08:00
|
|
|
const { href, title } = this.props;
|
2021-02-19 18:26:53 +08:00
|
|
|
onClick?.(e, { title, href });
|
2018-08-28 20:20:46 +08:00
|
|
|
scrollTo(href);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-11-09 14:40:31 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderAnchorLink = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2019-08-19 11:10:06 +08:00
|
|
|
const { prefixCls: customizePrefixCls, href, title, children, className, target } = this.props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('anchor', customizePrefixCls);
|
2020-04-29 20:22:16 +08:00
|
|
|
const active = this.context.activeLink === href;
|
2020-09-06 13:07:39 +08:00
|
|
|
const wrapperClassName = classNames(
|
|
|
|
`${prefixCls}-link`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-link-active`]: active,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2017-07-18 18:04:48 +08:00
|
|
|
const titleClassName = classNames(`${prefixCls}-link-title`, {
|
|
|
|
[`${prefixCls}-link-title-active`]: active,
|
|
|
|
});
|
2016-11-09 14:22:38 +08:00
|
|
|
return (
|
2017-07-18 18:04:48 +08:00
|
|
|
<div className={wrapperClassName}>
|
2016-11-09 14:40:31 +08:00
|
|
|
<a
|
2017-07-18 18:04:48 +08:00
|
|
|
className={titleClassName}
|
2016-11-09 14:40:31 +08:00
|
|
|
href={href}
|
2016-11-10 11:35:12 +08:00
|
|
|
title={typeof title === 'string' ? title : ''}
|
2019-08-19 11:10:06 +08:00
|
|
|
target={target}
|
2017-07-18 18:04:48 +08:00
|
|
|
onClick={this.handleClick}
|
2016-11-09 14:22:38 +08:00
|
|
|
>
|
2016-11-09 14:40:31 +08:00
|
|
|
{title}
|
|
|
|
</a>
|
2017-07-18 18:04:48 +08:00
|
|
|
{children}
|
2016-11-09 14:22:38 +08:00
|
|
|
</div>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderAnchorLink}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-11-03 16:45:13 +08:00
|
|
|
}
|
2019-02-02 18:30:40 +08:00
|
|
|
|
|
|
|
export default AnchorLink;
|