2020-06-08 15:45:14 +08:00
|
|
|
import { inject } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-06-08 15:45:14 +08:00
|
|
|
import { initDefaultProps, getComponent } from '../_util/props-util';
|
2019-01-12 11:33:27 +08:00
|
|
|
import classNames from 'classnames';
|
2019-03-11 14:43:23 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2018-06-02 18:11:50 +08:00
|
|
|
|
|
|
|
export const AnchorLinkProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
href: PropTypes.string,
|
|
|
|
title: PropTypes.any,
|
2020-03-07 19:45:13 +08:00
|
|
|
target: PropTypes.string,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-06-02 18:11:50 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'AAnchorLink',
|
|
|
|
props: initDefaultProps(AnchorLinkProps, {
|
|
|
|
href: '#',
|
|
|
|
}),
|
2020-06-08 15:45:14 +08:00
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
antAnchor: inject('antAnchor', {}),
|
|
|
|
antAnchorContext: inject('antAnchorContext', {}),
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
};
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
2019-02-01 17:23:00 +08:00
|
|
|
watch: {
|
|
|
|
href(val, oldVal) {
|
2019-09-06 19:01:49 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.antAnchor.unregisterLink(oldVal);
|
|
|
|
this.antAnchor.registerLink(val);
|
|
|
|
});
|
2019-02-01 17:23:00 +08:00
|
|
|
},
|
|
|
|
},
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
mounted() {
|
|
|
|
this.antAnchor.registerLink(this.href);
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
|
|
|
|
2020-06-11 16:13:09 +08:00
|
|
|
beforeUnmount() {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.antAnchor.unregisterLink(this.href);
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
handleClick(e) {
|
|
|
|
this.antAnchor.scrollTo(this.href);
|
|
|
|
const { scrollTo } = this.antAnchor;
|
|
|
|
const { href, title } = this.$props;
|
2018-11-28 10:35:37 +08:00
|
|
|
if (this.antAnchorContext.$emit) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.antAnchorContext.$emit('click', e, { title, href });
|
2018-11-28 10:35:37 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
scrollTo(href);
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { prefixCls: customizePrefixCls, href, $slots, target } = this;
|
2019-03-11 14:43:23 +08:00
|
|
|
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-11 14:43:23 +08:00
|
|
|
const prefixCls = getPrefixCls('anchor', customizePrefixCls);
|
|
|
|
|
2020-06-08 15:45:14 +08:00
|
|
|
const title = getComponent(this, 'title');
|
2019-01-12 11:33:27 +08:00
|
|
|
const active = this.antAnchor.$data.activeLink === href;
|
2018-06-02 18:11:50 +08:00
|
|
|
const wrapperClassName = classNames(`${prefixCls}-link`, {
|
|
|
|
[`${prefixCls}-link-active`]: active,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
const titleClassName = classNames(`${prefixCls}-link-title`, {
|
|
|
|
[`${prefixCls}-link-title-active`]: active,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
return (
|
|
|
|
<div class={wrapperClassName}>
|
|
|
|
<a
|
|
|
|
class={titleClassName}
|
|
|
|
href={href}
|
|
|
|
title={typeof title === 'string' ? title : ''}
|
2020-03-07 19:45:13 +08:00
|
|
|
target={target}
|
2018-06-02 18:11:50 +08:00
|
|
|
onClick={this.handleClick}
|
|
|
|
>
|
|
|
|
{title}
|
|
|
|
</a>
|
2020-06-08 15:45:14 +08:00
|
|
|
{$slots.default && $slots.default()}
|
2018-06-02 18:11:50 +08:00
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|