2021-06-26 01:35:40 +00:00
|
|
|
import type { ExtractPropTypes } from 'vue';
|
|
|
|
import { defineComponent, nextTick, onBeforeUnmount, onMounted, watch } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2022-03-26 14:52:54 +00:00
|
|
|
import { getPropsSlot, initDefaultProps } 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
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export const anchorLinkProps = () => ({
|
|
|
|
prefixCls: String,
|
|
|
|
href: String,
|
2021-12-16 09:20:18 +00:00
|
|
|
title: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
target: String,
|
|
|
|
});
|
2018-06-02 10:11:50 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export type AnchorLinkProps = Partial<ExtractPropTypes<ReturnType<typeof anchorLinkProps>>>;
|
2021-06-07 09:35:03 +00:00
|
|
|
|
2020-10-12 09:54:43 +00:00
|
|
|
export default defineComponent({
|
2018-06-02 10:11:50 +00:00
|
|
|
name: 'AAnchorLink',
|
2022-03-26 14:52:54 +00:00
|
|
|
props: initDefaultProps(anchorLinkProps(), { href: '#' }),
|
2021-06-07 09:35:03 +00:00
|
|
|
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
|
|
|
});
|