2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
2016-11-09 14:43:12 +08:00
|
|
|
import classNames from 'classnames';
|
2018-08-28 20:20:46 +08:00
|
|
|
import { AntAnchor } from './Anchor';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
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;
|
2017-03-29 10:29:43 +08:00
|
|
|
title: React.ReactNode;
|
2017-07-18 18:04:48 +08:00
|
|
|
children?: any;
|
2019-01-10 15:22:54 +08:00
|
|
|
className?: string;
|
2016-10-28 14:02:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-03 16:45:13 +08:00
|
|
|
export default class AnchorLink extends React.Component<AnchorLinkProps, any> {
|
|
|
|
static defaultProps = {
|
2017-07-18 18:04:48 +08:00
|
|
|
href: '#',
|
2016-11-03 16:45:13 +08:00
|
|
|
};
|
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
static contextTypes = {
|
|
|
|
antAnchor: PropTypes.object,
|
2016-11-03 16:45:13 +08:00
|
|
|
};
|
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
context: {
|
2018-08-28 20:20:46 +08:00
|
|
|
antAnchor: AntAnchor;
|
2017-07-18 18:04:48 +08:00
|
|
|
};
|
2016-11-14 15:43:55 +08:00
|
|
|
|
|
|
|
componentDidMount() {
|
2017-07-18 18:04:48 +08:00
|
|
|
this.context.antAnchor.registerLink(this.props.href);
|
2016-11-14 15:43:55 +08:00
|
|
|
}
|
|
|
|
|
2018-07-16 14:20:41 +08:00
|
|
|
componentWillReceiveProps(nextProps: AnchorLinkProps) {
|
|
|
|
const { href } = nextProps;
|
|
|
|
if (this.props.href !== href) {
|
|
|
|
this.context.antAnchor.unregisterLink(this.props.href);
|
|
|
|
this.context.antAnchor.registerLink(href);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.context.antAnchor.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>) => {
|
|
|
|
const { scrollTo, onClick } = this.context.antAnchor;
|
|
|
|
const { href, title } = this.props;
|
|
|
|
if (onClick) {
|
|
|
|
onClick(e, { title, href });
|
|
|
|
}
|
|
|
|
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-01-10 15:22:54 +08:00
|
|
|
const { prefixCls: customizePrefixCls, href, title, children, className } = this.props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('anchor', customizePrefixCls);
|
2017-07-18 18:04:48 +08:00
|
|
|
const active = this.context.antAnchor.activeLink === href;
|
2019-01-10 15:22:54 +08:00
|
|
|
const wrapperClassName = classNames(className, `${prefixCls}-link`, {
|
2016-11-03 16:45:13 +08:00
|
|
|
[`${prefixCls}-link-active`]: active,
|
|
|
|
});
|
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 : ''}
|
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
|
|
|
}
|