2021-06-07 09:35:03 +00:00
|
|
|
import {
|
|
|
|
defineComponent,
|
|
|
|
ExtractPropTypes,
|
|
|
|
nextTick,
|
|
|
|
onBeforeUnmount,
|
|
|
|
onMounted,
|
|
|
|
watch,
|
|
|
|
} from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2021-06-07 09:35:03 +00:00
|
|
|
import { getPropsSlot } from '../_util/props-util';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2021-06-07 09:35:03 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import { useInjectAnchor } from './context';
|
2020-10-19 08:43:10 +00:00
|
|
|
|
2021-06-07 09:35:03 +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
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
export type AnchorLinkProps = Partial<ExtractPropTypes<typeof anchorLinkProps>>;
|
|
|
|
|
2020-10-12 09:54:43 +00:00
|
|
|
export default defineComponent({
|
2018-06-02 10:11:50 +00:00
|
|
|
name: 'AAnchorLink',
|
2021-06-07 09:35:03 +00:00
|
|
|
props: anchorLinkProps,
|
|
|
|
slots: ['title'],
|
|
|
|
setup(props, { slots }) {
|
|
|
|
let mergedTitle = null;
|
|
|
|
const {
|
|
|
|
handleClick: contextHandleClick,
|
|
|
|
scrollTo,
|
|
|
|
unregisterLink,
|
|
|
|
registerLink,
|
|
|
|
activeLink,
|
|
|
|
} = useInjectAnchor();
|
|
|
|
const { prefixCls } = useConfigInject('anchor', props);
|
2018-06-02 10:11:50 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const handleClick = (e: Event) => {
|
|
|
|
const { href } = props;
|
|
|
|
contextHandleClick(e, { title: mergedTitle, href });
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollTo(href);
|
2021-06-07 09:35:03 +00:00
|
|
|
};
|
2019-03-11 06:43:23 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
watch(
|
|
|
|
() => props.href,
|
|
|
|
(val, oldVal) => {
|
|
|
|
nextTick(() => {
|
|
|
|
unregisterLink(oldVal);
|
|
|
|
registerLink(val);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2019-03-11 06:43:23 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
onMounted(() => {
|
|
|
|
registerLink(props.href);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2021-06-07 09:35:03 +00:00
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
unregisterLink(props.href);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2021-06-07 09:35:03 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
const { href, target } = props;
|
|
|
|
const pre = prefixCls.value;
|
|
|
|
const title = getPropsSlot(slots, props, 'title');
|
|
|
|
mergedTitle = title;
|
|
|
|
const active = activeLink.value === href;
|
|
|
|
const wrapperClassName = classNames(`${pre}-link`, {
|
|
|
|
[`${pre}-link-active`]: active,
|
|
|
|
});
|
|
|
|
const titleClassName = classNames(`${pre}-link-title`, {
|
|
|
|
[`${pre}-link-title-active`]: active,
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<div class={wrapperClassName}>
|
|
|
|
<a
|
|
|
|
class={titleClassName}
|
|
|
|
href={href}
|
|
|
|
title={typeof title === 'string' ? title : ''}
|
|
|
|
target={target}
|
|
|
|
onClick={handleClick}
|
|
|
|
>
|
|
|
|
{title}
|
|
|
|
</a>
|
|
|
|
{slots.default?.()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2018-06-02 10:11:50 +00:00
|
|
|
},
|
2020-10-12 09:54:43 +00:00
|
|
|
});
|