From de4693c321ba35e6e63af9375a7ebd242f00efa9 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Mon, 24 May 2021 23:07:42 +0800 Subject: [PATCH] refactor: anchor --- components/anchor/Anchor.tsx | 5 ++++- components/anchor/AnchorLink.tsx | 11 +++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/components/anchor/Anchor.tsx b/components/anchor/Anchor.tsx index 965764764..7797539fd 100644 --- a/components/anchor/Anchor.tsx +++ b/components/anchor/Anchor.tsx @@ -78,7 +78,7 @@ export default defineComponent({ inheritAttrs: false, props: anchorProps, emits: ['change', 'click'], - setup(props, { emit, attrs, slots }) { + setup(props, { emit, attrs, slots, expose }) { const { prefixCls, getTargetContainer, direction } = useConfigInject('anchor', props); const inkNodeRef = ref(); const anchorRef = ref(); @@ -159,6 +159,9 @@ export default defineComponent({ getContainer, }); }; + expose({ + scrollTo: handleScrollTo, + }); const handleScroll = () => { if (state.animating) { return; diff --git a/components/anchor/AnchorLink.tsx b/components/anchor/AnchorLink.tsx index fb08b0c07..c43d519a7 100644 --- a/components/anchor/AnchorLink.tsx +++ b/components/anchor/AnchorLink.tsx @@ -12,9 +12,6 @@ import classNames from '../_util/classNames'; import useConfigInject from '../_util/hooks/useConfigInject'; import { useInjectAnchor } from './context'; -// eslint-disable-next-line @typescript-eslint/no-unused-vars -function noop(..._any: any[]): any {} - const anchorLinkProps = { prefixCls: PropTypes.string, href: PropTypes.string.def('#'), @@ -27,7 +24,9 @@ export type AnchorLinkProps = Partial>; export default defineComponent({ name: 'AAnchorLink', props: anchorLinkProps, + slots: ['title'], setup(props, { slots }) { + let mergedTitle = null; const { handleClick: contextHandleClick, scrollTo, @@ -38,9 +37,8 @@ export default defineComponent({ const { prefixCls } = useConfigInject('anchor', props); const handleClick = (e: Event) => { - // antAnchor.scrollTo(props.href); - const { href, title } = props; - contextHandleClick(e, { title, href }); + const { href } = props; + contextHandleClick(e, { title: mergedTitle, href }); scrollTo(href); }; @@ -66,6 +64,7 @@ export default defineComponent({ 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,