ant-design-vue/components/anchor/AnchorLink.jsx

71 lines
1.8 KiB
Vue
Raw Normal View History

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