ant-design/components/anchor/index.tsx

126 lines
2.9 KiB
TypeScript
Raw Normal View History

2016-10-28 14:02:55 +08:00
import React from 'react';
import classNames 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-11-09 14:40:31 +08:00
className?: string;
style?: React.CSSProperties;
affix?: boolean;
2016-11-03 16:45:13 +08:00
}
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',
affix: true,
2016-11-03 16:45:13 +08:00
};
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
}
2016-11-09 14:22:38 +08:00
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-11-09 14:22:38 +08:00
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() {
const { prefixCls, offsetTop, style, className = '', affix} = this.props;
2016-11-03 16:45:13 +08:00
const { activeAnchor } = this.state;
const inkClass = classNames({
2016-11-03 16:45:13 +08:00
[`${prefixCls}-ink-ball`]: true,
visible: !!activeAnchor,
});
const wrapperClass = classNames({
[`${prefixCls}-wrapper`]: true,
[className]: !!className,
});
const anchorClass = classNames({
[`${prefixCls}`]: true,
affix,
});
const anchorContent = (<div className={wrapperClass} style={style}>
<div className={anchorClass}>
<div className={`${prefixCls}-ink`} >
<span className={inkClass} ref="ink" />
2016-11-03 16:45:13 +08:00
</div>
{React.Children.map(this.props.children, this.renderAnchorLink)}
</div>
</div>);
return affix === false ? anchorContent : <Affix offsetTop={offsetTop}>
{anchorContent}
</Affix>;
2016-10-28 14:02:55 +08:00
}
}