link.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import BaseComponent from '../_base/baseComponent';
  5. import { cssClasses } from '@douyinfe/semi-foundation/anchor/constants';
  6. import LinkFoundation, { LinkAdapter } from '@douyinfe/semi-foundation/anchor/linkFoundation';
  7. import AnchorContext from './anchor-context';
  8. import Typography from '../typography/index';
  9. const prefixCls = cssClasses.PREFIX;
  10. export interface LinkProps {
  11. href?: string;
  12. title?: string | React.ReactNode;
  13. className?: string;
  14. style?: React.CSSProperties;
  15. disabled?: boolean;
  16. }
  17. // eslint-disable-next-line @typescript-eslint/ban-types
  18. export default class Link extends BaseComponent<LinkProps, {}> {
  19. static propTypes = {
  20. href: PropTypes.string,
  21. title: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  22. className: PropTypes.string,
  23. style: PropTypes.object,
  24. };
  25. static defaultProps = {
  26. href: '#',
  27. title: '',
  28. className: '',
  29. };
  30. static contextType = AnchorContext;
  31. foundation: LinkFoundation;
  32. constructor(props: LinkProps) {
  33. super(props);
  34. this.foundation = new LinkFoundation(this.adapter);
  35. }
  36. get adapter(): LinkAdapter {
  37. return {
  38. ...super.adapter,
  39. addLink: href => {
  40. this.context.addLink(href);
  41. },
  42. removeLink: href => {
  43. this.context.removeLink(href);
  44. },
  45. };
  46. }
  47. handleAddLink() {
  48. this.foundation.handleAddLink();
  49. }
  50. handleRemoveLink() {
  51. this.foundation.handleRemoveLink();
  52. }
  53. handleUpdateLink(href: string, prevHref: string) {
  54. this.foundation.handleUpdateLink(href, prevHref);
  55. }
  56. componentDidMount() {
  57. this.handleAddLink();
  58. }
  59. componentDidUpdate(prevProps: LinkProps) {
  60. const prevHref = prevProps.href;
  61. const { href } = this.props;
  62. this.handleUpdateLink(href, prevHref);
  63. }
  64. componentWillUnmount() {
  65. this.handleRemoveLink();
  66. }
  67. renderTitle = () => {
  68. const { href, title, disabled = false } = this.props;
  69. const { activeLink, showTooltip, position, size } = this.context;
  70. const active = activeLink === href;
  71. const linkTitleCls = cls(`${prefixCls}-link-tooltip`, {
  72. [`${prefixCls}-link-tooltip-small`]: size === 'small',
  73. [`${prefixCls}-link-tooltip-active`]: active,
  74. [`${prefixCls}-link-tooltip-disabled`]: disabled,
  75. });
  76. const toolTipOpt = position ? { position } : {};
  77. if (showTooltip) {
  78. return (
  79. <Typography.Text
  80. size={size === 'default' ? 'normal' : 'small'}
  81. ellipsis={{ showTooltip: { opts: { ...toolTipOpt } } }}
  82. type={'tertiary'}
  83. className={linkTitleCls}
  84. >
  85. {title}
  86. </Typography.Text>
  87. );
  88. } else {
  89. return title;
  90. }
  91. };
  92. renderChildren = () => {
  93. const { activeLink, childMap } = this.context;
  94. const { href, children } = this.props;
  95. if (!this.context.autoCollapse) {
  96. return this.props.children;
  97. }
  98. return activeLink === href || (childMap[href] && childMap[href].has(activeLink)) ? children : null;
  99. };
  100. render() {
  101. const { href, className, style, disabled = false } = this.props;
  102. const { activeLink, onClick } = this.context;
  103. const active = activeLink === href;
  104. const linkCls = cls(`${prefixCls}-link`, className);
  105. const linkTitleCls = cls(`${prefixCls}-link-title`, {
  106. [`${prefixCls}-link-title-active`]: active,
  107. [`${prefixCls}-link-title-disabled`]: disabled,
  108. });
  109. return (
  110. <div className={linkCls} style={style}>
  111. <div className={linkTitleCls} onClick={e => !disabled && onClick(e, href)}>
  112. {this.renderTitle()}
  113. </div>
  114. {this.renderChildren()}
  115. </div>
  116. );
  117. }
  118. }