2018-06-02 10:11:50 +00:00
|
|
|
import PropTypes from '../_util/vue-types'
|
|
|
|
import { initDefaultProps, getComponentFromProp } from '../_util/props-util'
|
|
|
|
import classNames from 'classnames'
|
|
|
|
|
|
|
|
export const AnchorLinkProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
href: PropTypes.string,
|
|
|
|
title: PropTypes.any,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'AAnchorLink',
|
|
|
|
props: initDefaultProps(AnchorLinkProps, {
|
|
|
|
prefixCls: 'ant-anchor',
|
|
|
|
href: '#',
|
|
|
|
}),
|
|
|
|
inject: {
|
|
|
|
antAnchor: { default: {}},
|
2018-11-28 02:35:37 +00:00
|
|
|
antAnchorContext: { default: {}},
|
2018-06-02 10:11:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
mounted () {
|
|
|
|
this.antAnchor.registerLink(this.href)
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeDestroy () {
|
|
|
|
this.antAnchor.unregisterLink(this.href)
|
|
|
|
},
|
2018-07-14 09:11:53 +00:00
|
|
|
watch: {
|
2018-09-05 13:28:54 +00:00
|
|
|
href (val, oldVal) {
|
|
|
|
this.antAnchor.unregisterLink(oldVal)
|
2018-07-14 09:11:53 +00:00
|
|
|
this.antAnchor.registerLink(val)
|
|
|
|
},
|
|
|
|
},
|
2018-06-02 10:11:50 +00:00
|
|
|
methods: {
|
2018-11-28 02:35:37 +00:00
|
|
|
handleClick (e) {
|
2018-06-02 10:11:50 +00:00
|
|
|
this.antAnchor.scrollTo(this.href)
|
2018-11-28 02:35:37 +00:00
|
|
|
const { scrollTo } = this.antAnchor
|
|
|
|
const { href, title } = this.$props
|
|
|
|
if (this.antAnchorContext.$emit) {
|
|
|
|
this.antAnchorContext.$emit('click', e, { title, href })
|
|
|
|
}
|
|
|
|
scrollTo(href)
|
2018-06-02 10:11:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
href,
|
|
|
|
$slots,
|
|
|
|
} = this
|
|
|
|
const title = getComponentFromProp(this, 'title')
|
|
|
|
const active = this.antAnchor.$data.activeLink === href
|
|
|
|
const wrapperClassName = classNames(`${prefixCls}-link`, {
|
|
|
|
[`${prefixCls}-link-active`]: active,
|
|
|
|
})
|
|
|
|
const titleClassName = classNames(`${prefixCls}-link-title`, {
|
|
|
|
[`${prefixCls}-link-title-active`]: active,
|
|
|
|
})
|
|
|
|
return (
|
|
|
|
<div class={wrapperClassName}>
|
|
|
|
<a
|
|
|
|
class={titleClassName}
|
|
|
|
href={href}
|
|
|
|
title={typeof title === 'string' ? title : ''}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
>
|
|
|
|
{title}
|
|
|
|
</a>
|
|
|
|
{$slots.default}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|