item.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 type { 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. //@ts-ignore
  79. return React.cloneElement(iconType, { className, size: iconSize });
  80. }
  81. return iconType;
  82. };
  83. getTooltipOpt = () => {
  84. const { showTooltip } = this.context;
  85. if (!showTooltip) {
  86. return {
  87. width: 150,
  88. ellipsisPos: 'end',
  89. };
  90. }
  91. const defaultOpts = {
  92. width: 150,
  93. ellipsisPos: 'end',
  94. opts: {
  95. autoAdjustOverflow: true,
  96. position: 'top',
  97. },
  98. };
  99. if (typeof showTooltip === 'object') {
  100. return merge(defaultOpts, showTooltip);
  101. }
  102. return defaultOpts;
  103. };
  104. getItemInfo = (): BreadcrumbItemInfo => {
  105. let itemInfo: BreadcrumbItemInfo = {};
  106. const { route, children, href } = this.props;
  107. const hasHref = !isUndefined(href) && !isNull(href);
  108. if (route) {
  109. itemInfo = route;
  110. } else {
  111. itemInfo.name = children;
  112. if (hasHref) {
  113. itemInfo.href = href;
  114. }
  115. }
  116. return itemInfo;
  117. };
  118. renderBreadItem = () => {
  119. const { children } = this.props;
  120. const { compact } = this.context;
  121. const showTooltip = this.getTooltipOpt();
  122. const icon = this.renderIcon();
  123. if (Boolean(children) && typeof children === 'string') {
  124. const { opts, ellipsisPos, width } = showTooltip as GetTooltipOptType;
  125. return (
  126. <Fragment>
  127. {icon}
  128. <span className={`${clsPrefix}-item-title`}>
  129. <Typography.Text
  130. ellipsis={{
  131. showTooltip: opts ? { opts } : false,
  132. pos: ellipsisPos,
  133. }}
  134. // icon={this.renderIcon(icon)}
  135. style={{ width }}
  136. size={compact ? 'small' : 'normal'}
  137. >
  138. {children}
  139. </Typography.Text>
  140. </span>
  141. </Fragment>
  142. );
  143. }
  144. return (
  145. <Fragment>
  146. {icon}
  147. {children ? (
  148. <span className={`${clsPrefix}-item-title ${clsPrefix}-item-title-inline`}>{children}</span>
  149. ) : null}
  150. </Fragment>
  151. );
  152. };
  153. renderItem = () => {
  154. const { href, active, noLink } = this.props;
  155. const hasHref = href !== null && typeof href !== 'undefined';
  156. const itemCls = cls({
  157. [`${clsPrefix}-item`]: true,
  158. [`${clsPrefix}-item-active`]: active,
  159. [`${clsPrefix}-item-link`]: !noLink,
  160. });
  161. const itemInner = this.renderBreadItem();
  162. const tag = active || !hasHref ? 'span' : 'a';
  163. const itemInfo = this.getItemInfo();
  164. return React.createElement(
  165. tag,
  166. {
  167. className: itemCls,
  168. onClick: e => this.foundation.handleClick(itemInfo, e),
  169. href,
  170. },
  171. itemInner
  172. );
  173. };
  174. render() {
  175. const {
  176. active,
  177. shouldRenderSeparator
  178. // children,
  179. } = this.props;
  180. const pageLabel = active ? { 'aria-current': 'page' as const } : {} ;
  181. const item = this.renderItem();
  182. const separator = !active ?
  183. this.props.separator || <span className={`${clsPrefix}-separator`}>{this.context.separator}</span> :
  184. null;
  185. const wrapperCLs = cls({
  186. [`${clsPrefix}-item-wrap`]: true,
  187. // [`${clsPrefix}-item-wrap-iconOnly`]: !!children && this.props.icon,
  188. });
  189. return (
  190. <span className={wrapperCLs} {...pageLabel}>
  191. {item}
  192. {shouldRenderSeparator && separator}
  193. </span>
  194. );
  195. }
  196. }