|
@ -4,7 +4,7 @@ import AnchorHelper, { scrollTo } from './anchorHelper'; |
|
|
|
|
|
|
|
|
export interface AnchorLinkProps { |
|
|
export interface AnchorLinkProps { |
|
|
href: string; |
|
|
href: string; |
|
|
onClick: (href: string) => void; |
|
|
onClick: (href: string, component: Element) => void; |
|
|
active?: boolean; |
|
|
active?: boolean; |
|
|
prefixCls?: string; |
|
|
prefixCls?: string; |
|
|
children?: any; |
|
|
children?: any; |
|
@ -32,6 +32,8 @@ export default class AnchorLink extends React.Component<AnchorLinkProps, any> { |
|
|
anchorHelper: AnchorHelper; |
|
|
anchorHelper: AnchorHelper; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
private _component: Element; |
|
|
|
|
|
|
|
|
constructor(props, context) { |
|
|
constructor(props, context) { |
|
|
super(props, context); |
|
|
super(props, context); |
|
|
} |
|
|
} |
|
@ -42,24 +44,46 @@ export default class AnchorLink extends React.Component<AnchorLinkProps, any> { |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
renderAnchorLink = (child) => { |
|
|
renderAnchorLink = (child) => { |
|
|
const { href } = child.props; |
|
|
const { href } = child.props; |
|
|
if (href) { |
|
|
if (href) { |
|
|
this.context.anchorHelper.addLink(href); |
|
|
this.context.anchorHelper.addLink(href); |
|
|
return React.cloneElement(child, { |
|
|
return React.cloneElement(child, { |
|
|
onClick: this.context.anchorHelper.scrollTo, |
|
|
onClick: this.props.onClick, |
|
|
prefixCls: this.props.prefixCls, |
|
|
prefixCls: this.props.prefixCls, |
|
|
|
|
|
affix: this.props.affix, |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
return child; |
|
|
return child; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
refsTo = (component) => { |
|
|
|
|
|
this._component = component; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
scrollTo = (e) => { |
|
|
scrollTo = (e) => { |
|
|
const { onClick, href } = this.props; |
|
|
const { onClick, href } = this.props; |
|
|
const { anchorHelper } = this.context; |
|
|
const { anchorHelper } = this.context; |
|
|
e.preventDefault(); |
|
|
e.preventDefault(); |
|
|
if (onClick) { |
|
|
if (onClick) { |
|
|
onClick(href); |
|
|
onClick(href, this._component); |
|
|
} else { |
|
|
} else { |
|
|
e.stopPreventDefault(); |
|
|
e.stopPreventDefault(); |
|
|
const scrollToFn = anchorHelper ? anchorHelper.scrollTo : scrollTo; |
|
|
const scrollToFn = anchorHelper ? anchorHelper.scrollTo : scrollTo; |
|
@ -78,7 +102,7 @@ export default class AnchorLink extends React.Component<AnchorLinkProps, any> { |
|
|
return ( |
|
|
return ( |
|
|
<div className={cls}> |
|
|
<div className={cls}> |
|
|
<a |
|
|
<a |
|
|
ref={(component) => component && active && anchorHelper ? anchorHelper.setActiveAnchor(component) : null} |
|
|
ref={this.refsTo} |
|
|
className={`${prefixCls}-link-title`} |
|
|
className={`${prefixCls}-link-title`} |
|
|
onClick={this.scrollTo} |
|
|
onClick={this.scrollTo} |
|
|
href={href} |
|
|
href={href} |
|
|