refactor: anchor

pull/4134/head
tangjinzhou 2021-05-24 23:07:42 +08:00
parent a6e30c2ded
commit de4693c321
2 changed files with 9 additions and 7 deletions

View File

@ -78,7 +78,7 @@ export default defineComponent({
inheritAttrs: false, inheritAttrs: false,
props: anchorProps, props: anchorProps,
emits: ['change', 'click'], emits: ['change', 'click'],
setup(props, { emit, attrs, slots }) { setup(props, { emit, attrs, slots, expose }) {
const { prefixCls, getTargetContainer, direction } = useConfigInject('anchor', props); const { prefixCls, getTargetContainer, direction } = useConfigInject('anchor', props);
const inkNodeRef = ref(); const inkNodeRef = ref();
const anchorRef = ref(); const anchorRef = ref();
@ -159,6 +159,9 @@ export default defineComponent({
getContainer, getContainer,
}); });
}; };
expose({
scrollTo: handleScrollTo,
});
const handleScroll = () => { const handleScroll = () => {
if (state.animating) { if (state.animating) {
return; return;

View File

@ -12,9 +12,6 @@ import classNames from '../_util/classNames';
import useConfigInject from '../_util/hooks/useConfigInject'; import useConfigInject from '../_util/hooks/useConfigInject';
import { useInjectAnchor } from './context'; import { useInjectAnchor } from './context';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function noop(..._any: any[]): any {}
const anchorLinkProps = { const anchorLinkProps = {
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
href: PropTypes.string.def('#'), href: PropTypes.string.def('#'),
@ -27,7 +24,9 @@ export type AnchorLinkProps = Partial<ExtractPropTypes<typeof anchorLinkProps>>;
export default defineComponent({ export default defineComponent({
name: 'AAnchorLink', name: 'AAnchorLink',
props: anchorLinkProps, props: anchorLinkProps,
slots: ['title'],
setup(props, { slots }) { setup(props, { slots }) {
let mergedTitle = null;
const { const {
handleClick: contextHandleClick, handleClick: contextHandleClick,
scrollTo, scrollTo,
@ -38,9 +37,8 @@ export default defineComponent({
const { prefixCls } = useConfigInject('anchor', props); const { prefixCls } = useConfigInject('anchor', props);
const handleClick = (e: Event) => { const handleClick = (e: Event) => {
// antAnchor.scrollTo(props.href); const { href } = props;
const { href, title } = props; contextHandleClick(e, { title: mergedTitle, href });
contextHandleClick(e, { title, href });
scrollTo(href); scrollTo(href);
}; };
@ -66,6 +64,7 @@ export default defineComponent({
const { href, target } = props; const { href, target } = props;
const pre = prefixCls.value; const pre = prefixCls.value;
const title = getPropsSlot(slots, props, 'title'); const title = getPropsSlot(slots, props, 'title');
mergedTitle = title;
const active = activeLink.value === href; const active = activeLink.value === href;
const wrapperClassName = classNames(`${pre}-link`, { const wrapperClassName = classNames(`${pre}-link`, {
[`${pre}-link-active`]: active, [`${pre}-link-active`]: active,