link.tsx 5.5 KB

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