2020-10-12 09:54:43 +00:00
|
|
|
import { ComponentPublicInstance, defineComponent, inject } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-10-12 09:54:43 +00:00
|
|
|
import { getComponent } from '../_util/props-util';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-10-12 09:54:43 +00:00
|
|
|
import { AntAnchor } from './Anchor';
|
|
|
|
function noop(..._any: any[]): any {}
|
2018-06-02 10:11:50 +00:00
|
|
|
|
2020-10-12 09:54:43 +00:00
|
|
|
const AnchorLinkProps = {
|
2018-06-02 10:11:50 +00:00
|
|
|
prefixCls: PropTypes.string,
|
2020-10-12 09:54:43 +00:00
|
|
|
href: PropTypes.string.def('#'),
|
2020-10-13 02:43:39 +00:00
|
|
|
title: PropTypes.VNodeChild,
|
2020-03-07 11:45:13 +00:00
|
|
|
target: PropTypes.string,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-02 10:11:50 +00:00
|
|
|
|
2020-10-12 09:54:43 +00:00
|
|
|
export default defineComponent({
|
2018-06-02 10:11:50 +00:00
|
|
|
name: 'AAnchorLink',
|
2020-10-12 09:54:43 +00:00
|
|
|
props: AnchorLinkProps,
|
2020-06-08 07:45:14 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
2020-08-19 07:24:18 +00:00
|
|
|
antAnchor: inject('antAnchor', {
|
|
|
|
registerLink: noop,
|
|
|
|
unregisterLink: noop,
|
|
|
|
scrollTo: noop,
|
2020-08-20 09:56:49 +00:00
|
|
|
$data: {},
|
2020-10-12 09:54:43 +00:00
|
|
|
} as AntAnchor),
|
|
|
|
antAnchorContext: inject('antAnchorContext', {}) as ComponentPublicInstance,
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-06-08 07:45:14 +00:00
|
|
|
};
|
2018-06-02 10:11:50 +00:00
|
|
|
},
|
2019-02-01 09:23:00 +00:00
|
|
|
watch: {
|
|
|
|
href(val, oldVal) {
|
2019-09-06 11:01:49 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.antAnchor.unregisterLink(oldVal);
|
|
|
|
this.antAnchor.registerLink(val);
|
|
|
|
});
|
2019-02-01 09:23:00 +00:00
|
|
|
},
|
|
|
|
},
|
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
|
|
|
},
|
|
|
|
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.antAnchor.unregisterLink(this.href);
|
2018-06-02 10:11:50 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2020-10-12 09:54:43 +00:00
|
|
|
handleClick(e: Event) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.antAnchor.scrollTo(this.href);
|
|
|
|
const { scrollTo } = this.antAnchor;
|
|
|
|
const { href, title } = this.$props;
|
2018-11-28 02:35:37 +00:00
|
|
|
if (this.antAnchorContext.$emit) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.antAnchorContext.$emit('click', e, { title, href });
|
2018-11-28 02:35:37 +00:00
|
|
|
}
|
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() {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { prefixCls: customizePrefixCls, href, $slots, target } = this;
|
2019-03-11 06:43:23 +00:00
|
|
|
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-11 06:43:23 +00:00
|
|
|
const prefixCls = getPrefixCls('anchor', customizePrefixCls);
|
|
|
|
|
2020-06-08 07:45:14 +00:00
|
|
|
const title = getComponent(this, 'title');
|
2020-08-20 09:56:49 +00:00
|
|
|
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 : ''}
|
2020-03-07 11:45:13 +00:00
|
|
|
target={target}
|
2018-06-02 10:11:50 +00:00
|
|
|
onClick={this.handleClick}
|
|
|
|
>
|
|
|
|
{title}
|
|
|
|
</a>
|
2020-06-08 07:45:14 +00:00
|
|
|
{$slots.default && $slots.default()}
|
2018-06-02 10:11:50 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-06-02 10:11:50 +00:00
|
|
|
},
|
2020-10-12 09:54:43 +00:00
|
|
|
});
|