ant-design/components/anchor/AnchorLink.tsx

114 lines
2.7 KiB
TypeScript
Raw Normal View History

2016-10-28 14:02:55 +08:00
import React from 'react';
import classNames from 'classnames';
2016-11-03 16:45:13 +08:00
import AnchorHelper, { scrollTo } from './anchorHelper';
2016-10-28 14:02:55 +08:00
export interface AnchorLinkProps {
href: string;
2016-11-15 15:33:13 +08:00
onClick: (href: string, component: Element) => void;
2016-11-03 16:45:13 +08:00
active?: boolean;
prefixCls?: string;
children?: any;
title?: Element;
bounds: number;
target?: () => HTMLElement | Window;
2016-11-09 21:06:43 +08:00
affix?: boolean;
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 contextTypes = {
anchorHelper: React.PropTypes.any,
};
2016-10-28 16:53:59 +08:00
2016-11-03 16:45:13 +08:00
static childContextTypes = {
anchorHelper: React.PropTypes.any,
};
static defaultProps = {
href: '#',
prefixCls: 'ant-anchor',
};
context: {
anchorHelper: AnchorHelper;
};
2016-11-15 15:33:13 +08:00
private _component: Element;
2016-11-03 16:45:13 +08:00
getChildContext() {
return {
anchorHelper: this.context.anchorHelper,
};
2016-11-15 15:33:13 +08:00
}
setActiveAnchor() {
const { bounds, href, affix } = this.props;
const { anchorHelper } = this.context;
const active = affix && anchorHelper && anchorHelper.getCurrentAnchor(bounds) === href;
if (active && anchorHelper) {
anchorHelper.setActiveAnchor(this._component);
}
}
componentDidMount() {
this.setActiveAnchor();
}
componentDidUpdate() {
this.setActiveAnchor();
2016-11-03 16:45:13 +08:00
}
2016-11-09 14:22:38 +08:00
2016-11-03 16:45:13 +08:00
renderAnchorLink = (child) => {
const { href } = child.props;
if (href) {
this.context.anchorHelper.addLink(href);
return React.cloneElement(child, {
onClick: this.props.onClick,
2016-11-03 16:45:13 +08:00
prefixCls: this.props.prefixCls,
affix: this.props.affix,
2016-11-03 16:45:13 +08:00
});
}
return child;
}
2016-11-09 14:22:38 +08:00
refsTo = (component) => {
this._component = component;
}
scrollTo = (e) => {
2016-11-09 14:40:31 +08:00
const { onClick, href } = this.props;
const { anchorHelper } = this.context;
e.preventDefault();
2016-11-09 14:40:31 +08:00
if (onClick) {
onClick(href, this._component);
2016-11-09 14:40:31 +08:00
} else {
e.stopPreventDefault();
const scrollToFn = anchorHelper ? anchorHelper.scrollTo : scrollTo;
scrollToFn(href);
}
}
2016-11-03 16:45:13 +08:00
render() {
2016-11-09 21:06:43 +08:00
const { prefixCls, href, children, title, bounds, affix } = this.props;
2016-11-03 16:45:13 +08:00
const { anchorHelper } = this.context;
2016-11-09 21:06:43 +08:00
const active = affix && anchorHelper && anchorHelper.getCurrentAnchor(bounds) === href;
const cls = classNames({
2016-11-03 16:45:13 +08:00
[`${prefixCls}-link`]: true,
[`${prefixCls}-link-active`]: active,
});
2016-11-09 14:22:38 +08:00
return (
<div className={cls}>
2016-11-09 14:40:31 +08:00
<a
ref={this.refsTo}
2016-11-09 14:22:38 +08:00
className={`${prefixCls}-link-title`}
2016-11-09 14:40:31 +08:00
onClick={this.scrollTo}
href={href}
title={typeof title === 'string' ? title : ''}
2016-11-09 14:22:38 +08:00
>
2016-11-09 14:40:31 +08:00
{title}
</a>
2016-11-09 14:22:38 +08:00
{React.Children.map(children, this.renderAnchorLink)}
</div>
);
2016-11-03 16:45:13 +08:00
}
}