mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-11 23:00:20 +08:00
b0fe69c18f
* fix: fix * fix: fix * fix: del forwardRef * fix: add depList * test(anchor): rewrite test to match the latest fc implementation * fix: eslint error fix * style: type optimization * style: type optimization * Update components/anchor/Anchor.tsx Co-authored-by: afc163 <afc163@gmail.com> * Update Anchor.tsx * Update components/anchor/Anchor.tsx Co-authored-by: afc163 <afc163@gmail.com> * fix: rename * fix: fix * fix: fix * Update components/anchor/style/index.less Co-authored-by: afc163 <afc163@gmail.com> Co-authored-by: lijianan <574980606@qq.com> Co-authored-by: afc163 <afc163@gmail.com>
67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
import classNames from 'classnames';
|
|
import * as React from 'react';
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
import { ConfigConsumer } from '../config-provider';
|
|
import type { AntAnchor } from './Anchor';
|
|
import AnchorContext from './context';
|
|
|
|
export interface AnchorLinkProps {
|
|
prefixCls?: string;
|
|
href: string;
|
|
target?: string;
|
|
title: React.ReactNode;
|
|
children?: React.ReactNode;
|
|
className?: string;
|
|
}
|
|
|
|
const AnchorLink: React.FC<AnchorLinkProps> = props => {
|
|
const { href = '#', title, prefixCls: customizePrefixCls, children, className, target } = props;
|
|
|
|
const context = React.useContext<AntAnchor | undefined>(AnchorContext);
|
|
|
|
const { registerLink, unregisterLink, scrollTo, onClick, activeLink } = context || {};
|
|
|
|
React.useEffect(() => {
|
|
registerLink?.(href);
|
|
return () => {
|
|
unregisterLink?.(href);
|
|
};
|
|
}, [href, registerLink, unregisterLink]);
|
|
|
|
const handleClick = (e: React.MouseEvent<HTMLElement>) => {
|
|
onClick?.(e, { title, href });
|
|
scrollTo?.(href);
|
|
};
|
|
|
|
return (
|
|
<ConfigConsumer>
|
|
{({ getPrefixCls }: ConfigConsumerProps) => {
|
|
const prefixCls = getPrefixCls('anchor', customizePrefixCls);
|
|
const active = activeLink === href;
|
|
const wrapperClassName = classNames(`${prefixCls}-link`, className, {
|
|
[`${prefixCls}-link-active`]: active,
|
|
});
|
|
const titleClassName = classNames(`${prefixCls}-link-title`, {
|
|
[`${prefixCls}-link-title-active`]: active,
|
|
});
|
|
return (
|
|
<div className={wrapperClassName}>
|
|
<a
|
|
className={titleClassName}
|
|
href={href}
|
|
title={typeof title === 'string' ? title : ''}
|
|
target={target}
|
|
onClick={handleClick}
|
|
>
|
|
{title}
|
|
</a>
|
|
{children}
|
|
</div>
|
|
);
|
|
}}
|
|
</ConfigConsumer>
|
|
);
|
|
};
|
|
|
|
export default AnchorLink;
|