1
0

link.tsx 5.2 KB

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