ant-design/components/anchor/index.tsx

108 lines
2.5 KiB
TypeScript
Raw Normal View History

2016-10-28 14:02:55 +08:00
import React from 'react';
2016-11-03 16:45:13 +08:00
import className from 'classnames';
2016-10-28 14:02:55 +08:00
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import AnchorLink from './AnchorLink';
import Affix from '../affix';
2016-11-03 16:45:13 +08:00
import AnchorHelper, { getDefaultTarget } from './anchorHelper';
2016-10-28 14:02:55 +08:00
export interface AnchorProps {
target: () => HTMLElement | Window;
children: React.ReactNode;
2016-10-28 16:53:59 +08:00
prefixCls?: string;
2016-11-03 16:45:13 +08:00
offsetTop?: number;
bounds?: number;
}
2016-10-28 14:02:55 +08:00
export default class Anchor extends React.Component<AnchorProps, any> {
2016-11-07 11:04:36 +08:00
static Link = AnchorLink;
2016-11-03 16:45:13 +08:00
2016-10-28 16:53:59 +08:00
static defaultProps = {
2016-11-03 16:45:13 +08:00
prefixCls: 'ant-anchor',
};
static childContextTypes = {
anchorHelper: React.PropTypes.any,
};
refs: {
ink?: any;
2016-10-28 16:53:59 +08:00
};
2016-10-28 14:02:55 +08:00
private scrollEvent: any;
2016-11-03 16:45:13 +08:00
private anchorHelper: AnchorHelper;
2016-10-28 14:02:55 +08:00
constructor(props) {
super(props);
this.state = {
activeAnchor: null,
};
2016-11-03 16:45:13 +08:00
this.anchorHelper = new AnchorHelper();
2016-10-28 14:02:55 +08:00
}
handleScroll = () => {
2016-10-28 16:53:59 +08:00
this.setState({
2016-11-03 16:45:13 +08:00
activeAnchor: this.anchorHelper.getCurrentAnchor(this.props.bounds),
2016-10-28 16:53:59 +08:00
});
2016-10-28 14:02:55 +08:00
}
2016-11-03 16:45:13 +08:00
getChildContext() {
return {
anchorHelper: this.anchorHelper,
};
}
2016-10-28 14:02:55 +08:00
componentDidMount() {
this.handleScroll();
2016-11-03 16:45:13 +08:00
this.updateInk();
2016-10-28 14:02:55 +08:00
this.scrollEvent = addEventListener((this.props.target || getDefaultTarget)(), 'scroll', this.handleScroll);
}
componentWillUnmount() {
if (this.scrollEvent) {
this.scrollEvent.remove();
}
}
2016-11-03 16:45:13 +08:00
componentDidUpdate() {
this.updateInk();
}
updateInk = () => {
const activeAnchor = this.anchorHelper.getCurrentActiveAnchor();
if (activeAnchor) {
this.refs.ink.style.top = `${activeAnchor.offsetTop + activeAnchor.clientHeight / 2 - 4.5}px`;
}
2016-10-28 14:02:55 +08:00
}
renderAnchorLink = (child) => {
const { href } = child.props;
if (href) {
2016-11-03 16:45:13 +08:00
this.anchorHelper.addLink(href);
2016-10-28 16:53:59 +08:00
return React.cloneElement(child, {
2016-11-03 16:45:13 +08:00
onClick: this.anchorHelper.scrollTo,
2016-10-28 16:53:59 +08:00
prefixCls: this.props.prefixCls,
2016-11-03 16:45:13 +08:00
bounds: this.props.bounds,
2016-10-28 16:53:59 +08:00
});
2016-10-28 14:02:55 +08:00
}
return child;
}
render() {
2016-11-03 16:45:13 +08:00
const { prefixCls, offsetTop } = this.props;
const { activeAnchor } = this.state;
const inkClass = className({
[`${prefixCls}-ink-ball`]: true,
visible: !!activeAnchor,
});
return <Affix offsetTop={offsetTop}>
<div className={`${prefixCls}-wrapper`}>
<div className={prefixCls}>
<div className={`${prefixCls}-ink`} >
<span className={inkClass} ref="ink"/>
</div>
{React.Children.map(this.props.children, this.renderAnchorLink)}
</div>
</div>
2016-10-28 14:02:55 +08:00
</Affix>;
}
}