item.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import React, { Fragment } from 'react';
  2. import cls from 'classnames';
  3. import propTypes from 'prop-types';
  4. import { cssClasses } from '@douyinfe/semi-foundation/breadcrumb/constants';
  5. import BreadcrumbItemFoundation, { BreadcrumbItemAdapter, BreadcrumbItemInfo, Route } from '@douyinfe/semi-foundation/breadcrumb/itemFoundation';
  6. import BaseComponent, { BaseProps } from '../_base/baseComponent';
  7. import { noop } from '@douyinfe/semi-foundation/utils/function';
  8. import BreadContext, { BreadContextType } from './bread-context';
  9. import Typography, { EllipsisPos, ShowTooltip as ShowTooltipType } from '../typography';
  10. import { merge, isUndefined, isNull } from 'lodash';
  11. const clsPrefix = cssClasses.PREFIX;
  12. export { BreadcrumbItemInfo };
  13. export interface RouteProps extends Route {
  14. icon?: React.ReactNode;
  15. }
  16. export interface BreadcrumbItemProps extends BaseProps {
  17. onClick?: (item: RouteProps, e: React.MouseEvent) => void;
  18. icon?: React.ReactNode;
  19. href?: string;
  20. separator?: React.ReactNode;
  21. noLink?: boolean;
  22. active?: boolean;
  23. shouldRenderSeparator?: boolean;
  24. route?: RouteProps;
  25. }
  26. type BreadcrumbItemState = Record<string, never>;
  27. interface GetTooltipOptType {
  28. width: number;
  29. ellipsisPos: EllipsisPos;
  30. opts?: ShowTooltipType['opts'];
  31. }
  32. export default class BreadcrumbItem extends BaseComponent<BreadcrumbItemProps, BreadcrumbItemState> {
  33. static isBreadcrumbItem = true;
  34. static contextType = BreadContext;
  35. static propTypes = {
  36. onClick: propTypes.func,
  37. route: propTypes.oneOfType([propTypes.object, propTypes.string]),
  38. name: propTypes.string,
  39. children: propTypes.node,
  40. active: propTypes.bool,
  41. shouldRenderSeparator: propTypes.bool,
  42. icon: propTypes.oneOfType([propTypes.string, propTypes.node]),
  43. separator: propTypes.node,
  44. noLink: propTypes.bool,
  45. };
  46. static defaultProps = {
  47. onClick: noop,
  48. shouldRenderSeparator: true
  49. };
  50. context: BreadContextType;
  51. get adapter(): BreadcrumbItemAdapter<BreadcrumbItemProps, BreadcrumbItemState> {
  52. return {
  53. ...super.adapter,
  54. notifyClick: (...args) => {
  55. this.props.onClick(...args);
  56. },
  57. notifyParent: (...args) => {
  58. this.context.onClick(...args);
  59. },
  60. };
  61. }
  62. constructor(props: BreadcrumbItemProps) {
  63. super(props);
  64. this.foundation = new BreadcrumbItemFoundation(this.adapter);
  65. }
  66. componentDidMount() {
  67. this.foundation.init();
  68. }
  69. componentWillUnmount() {
  70. this.foundation.destroy();
  71. }
  72. renderIcon = () => {
  73. const iconType = this.props.icon;
  74. const { compact } = this.context;
  75. const iconSize = compact ? 'small' : 'default';
  76. const className = `${clsPrefix}-item-icon`;
  77. if (React.isValidElement(iconType)) {
  78. return React.cloneElement(iconType, { className, size: iconSize });
  79. }
  80. return iconType;
  81. };
  82. getTooltipOpt = () => {
  83. const { showTooltip } = this.context;
  84. if (!showTooltip) {
  85. return {
  86. width: 150,
  87. ellipsisPos: 'end',
  88. };
  89. }
  90. const defaultOpts = {
  91. width: 150,
  92. ellipsisPos: 'end',
  93. opts: {
  94. autoAdjustOverflow: true,
  95. position: 'top',
  96. },
  97. };
  98. if (typeof showTooltip === 'object') {
  99. return merge(defaultOpts, showTooltip);
  100. }
  101. return defaultOpts;
  102. };
  103. getItemInfo = (): BreadcrumbItemInfo => {
  104. let itemInfo: BreadcrumbItemInfo = {};
  105. const { route, children, href } = this.props;
  106. const hasHref = !isUndefined(href) && !isNull(href);
  107. if (route) {
  108. itemInfo = route;
  109. } else {
  110. itemInfo.name = children;
  111. if (hasHref) {
  112. itemInfo.href = href;
  113. }
  114. }
  115. return itemInfo;
  116. };
  117. renderBreadItem = () => {
  118. const { children } = this.props;
  119. const { compact } = this.context;
  120. const showTooltip = this.getTooltipOpt();
  121. const icon = this.renderIcon();
  122. if (Boolean(children) && typeof children === 'string') {
  123. const { opts, ellipsisPos, width } = showTooltip as GetTooltipOptType;
  124. return (
  125. <Fragment>
  126. {icon}
  127. <span className={`${clsPrefix}-item-title`}>
  128. <Typography.Text
  129. ellipsis={{
  130. showTooltip: opts ? { opts } : false,
  131. pos: ellipsisPos,
  132. }}
  133. // icon={this.renderIcon(icon)}
  134. style={{ width }}
  135. size={compact ? 'small' : 'normal'}
  136. >
  137. {children}
  138. </Typography.Text>
  139. </span>
  140. </Fragment>
  141. );
  142. }
  143. return (
  144. <Fragment>
  145. {icon}
  146. {children ? (
  147. <span className={`${clsPrefix}-item-title ${clsPrefix}-item-title-inline`}>{children}</span>
  148. ) : null}
  149. </Fragment>
  150. );
  151. };
  152. renderItem = () => {
  153. const { href, active, noLink } = this.props;
  154. const hasHref = href !== null && typeof href !== 'undefined';
  155. const itemCls = cls({
  156. [`${clsPrefix}-item`]: true,
  157. [`${clsPrefix}-item-active`]: active,
  158. [`${clsPrefix}-item-link`]: !noLink,
  159. });
  160. const itemInner = this.renderBreadItem();
  161. const tag = active || !hasHref ? 'span' : 'a';
  162. const itemInfo = this.getItemInfo();
  163. return React.createElement(
  164. tag,
  165. {
  166. className: itemCls,
  167. onClick: e => this.foundation.handleClick(itemInfo, e),
  168. href,
  169. },
  170. itemInner
  171. );
  172. };
  173. render() {
  174. const {
  175. active,
  176. shouldRenderSeparator
  177. // children,
  178. } = this.props;
  179. const pageLabel = active ? { 'aria-current': 'page' as const } : {} ;
  180. const item = this.renderItem();
  181. const separator = !active ?
  182. this.props.separator || <span className={`${clsPrefix}-separator`}>{this.context.separator}</span> :
  183. null;
  184. const wrapperCLs = cls({
  185. [`${clsPrefix}-item-wrap`]: true,
  186. // [`${clsPrefix}-item-wrap-iconOnly`]: !!children && this.props.icon,
  187. });
  188. return (
  189. <span className={wrapperCLs} {...pageLabel}>
  190. {item}
  191. {shouldRenderSeparator && separator}
  192. </span>
  193. );
  194. }
  195. }