link.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import React, { ReactNode } 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, { AnchorContextType } from './anchor-context';
  8. import Typography from '../typography/index';
  9. const prefixCls = cssClasses.PREFIX;
  10. export interface LinkProps {
  11. href?: string;
  12. title?: ReactNode;
  13. className?: string;
  14. children?: ReactNode;
  15. style?: React.CSSProperties;
  16. disabled?: boolean;
  17. }
  18. // eslint-disable-next-line @typescript-eslint/ban-types
  19. export default class Link extends BaseComponent<LinkProps, {}> {
  20. static propTypes = {
  21. href: PropTypes.string,
  22. title: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  23. className: PropTypes.string,
  24. style: PropTypes.object,
  25. };
  26. static defaultProps = {
  27. href: '#',
  28. title: '',
  29. className: '',
  30. };
  31. static contextType = AnchorContext;
  32. foundation: LinkFoundation;
  33. context!: AnchorContextType;
  34. constructor(props: LinkProps) {
  35. super(props);
  36. this.foundation = new LinkFoundation(this.adapter);
  37. this.handleClick = this.handleClick.bind(this);
  38. }
  39. get adapter(): LinkAdapter {
  40. return {
  41. ...super.adapter,
  42. addLink: href => {
  43. this.context.addLink(href);
  44. },
  45. removeLink: href => {
  46. this.context.removeLink(href);
  47. },
  48. };
  49. }
  50. handleAddLink() {
  51. this.foundation.handleAddLink();
  52. }
  53. handleRemoveLink() {
  54. this.foundation.handleRemoveLink();
  55. }
  56. handleUpdateLink(href: string, prevHref: string) {
  57. this.foundation.handleUpdateLink(href, prevHref);
  58. }
  59. handleClick(e: React.KeyboardEvent | React.MouseEvent) {
  60. const { disabled, href } = this.props;
  61. const { onClick } = this.context;
  62. !disabled && onClick(e as any, href);
  63. }
  64. componentDidMount() {
  65. this.handleAddLink();
  66. }
  67. componentDidUpdate(prevProps: LinkProps) {
  68. const prevHref = prevProps.href;
  69. const { href } = this.props;
  70. this.handleUpdateLink(href, prevHref);
  71. }
  72. componentWillUnmount() {
  73. this.handleRemoveLink();
  74. }
  75. renderTitle = () => {
  76. const { href, title, disabled = false } = this.props;
  77. const { activeLink, showTooltip, position, size } = this.context;
  78. const active = activeLink === href;
  79. const linkTitleCls = cls(`${prefixCls}-link-tooltip`, {
  80. [`${prefixCls}-link-tooltip-small`]: size === 'small',
  81. [`${prefixCls}-link-tooltip-active`]: active,
  82. [`${prefixCls}-link-tooltip-disabled`]: disabled,
  83. });
  84. const toolTipOpt = position ? { position } : {};
  85. if (showTooltip) {
  86. return (
  87. <Typography.Text
  88. size={size === 'default' ? 'normal' : 'small'}
  89. ellipsis={{ showTooltip: { opts: { ...toolTipOpt } } }}
  90. type={'tertiary'}
  91. className={linkTitleCls}
  92. >
  93. {title}
  94. </Typography.Text>
  95. );
  96. } else {
  97. return title;
  98. }
  99. };
  100. renderChildren = () => {
  101. const { activeLink, childMap } = this.context;
  102. const { href, children } = this.props;
  103. if (!this.context.autoCollapse) {
  104. return this.props.children;
  105. }
  106. return activeLink === href || (childMap[href] && childMap[href].has(activeLink)) ? children : null;
  107. };
  108. render() {
  109. const { href, className, style, disabled = false, title } = this.props;
  110. const { activeLink, showTooltip } = this.context;
  111. const active = activeLink === href;
  112. const linkCls = cls(`${prefixCls}-link`, className);
  113. const linkTitleCls = cls(`${prefixCls}-link-title`, {
  114. [`${prefixCls}-link-title-active`]: active,
  115. [`${prefixCls}-link-title-disabled`]: disabled,
  116. });
  117. const ariaProps = {
  118. 'aria-disabled': disabled,
  119. 'aria-label': href,
  120. };
  121. if (active) {
  122. ariaProps['aria-details'] = 'active';
  123. }
  124. if (!showTooltip && typeof title === 'string') {
  125. ariaProps['title'] = title;
  126. }
  127. return (
  128. <div className={linkCls} style={style}>
  129. <div
  130. role="link"
  131. tabIndex={0}
  132. {...ariaProps}
  133. className={linkTitleCls}
  134. onClick={e => this.handleClick(e)}
  135. onKeyPress={e => this.handleClick(e)}
  136. >
  137. {this.renderTitle()}
  138. </div>
  139. {this.renderChildren()}
  140. </div>
  141. );
  142. }
  143. }