Item.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* eslint-disable max-len */
  2. /* eslint-disable no-nested-ternary */
  3. import BaseComponent, { BaseProps } from '../_base/baseComponent';
  4. import React from 'react';
  5. import PropTypes from 'prop-types';
  6. import cls from 'classnames';
  7. import { times, noop } from 'lodash-es';
  8. import isNullOrUndefined from '@douyinfe/semi-foundation/utils/isNullOrUndefined';
  9. import { cloneDeep, isSemiIcon } from '../_utils';
  10. import ItemFoundation, { ItemProps, SelectedItemProps, ItemAdapter } from '@douyinfe/semi-foundation/navigation/itemFoundation';
  11. import { strings, cssClasses } from '@douyinfe/semi-foundation/navigation/constants';
  12. import Tooltip from '../tooltip';
  13. import NavContext from './nav-context';
  14. import Dropdown from '../dropdown';
  15. const clsPrefix = `${cssClasses.PREFIX}-item`;
  16. export interface NavItemProps extends ItemProps, BaseProps {
  17. children?: React.ReactNode;
  18. disabled?: boolean;
  19. forwardRef?: (ele: HTMLLIElement) => void;
  20. icon?: React.ReactNode;
  21. itemKey?: React.ReactText;
  22. level?: number;
  23. link?: string;
  24. linkOptions?: React.HTMLAttributes<HTMLLinkElement>;
  25. text?: React.ReactNode;
  26. tooltipHideDelay?: number;
  27. tooltipShowDelay?: number;
  28. onClick?(clickItems: SelectedData): void;
  29. onMouseEnter?: React.MouseEventHandler<HTMLLIElement>;
  30. onMouseLeave?: React.MouseEventHandler<HTMLLIElement>;
  31. }
  32. export interface SelectedData extends SelectedItemProps<NavItemProps> {
  33. text?: React.ReactNode;
  34. }
  35. export interface NavItemState {
  36. tooltipShow: boolean;
  37. }
  38. export default class NavItem extends BaseComponent<NavItemProps, NavItemState> {
  39. static contextType = NavContext;
  40. static propTypes = {
  41. text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  42. itemKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  43. onClick: PropTypes.func,
  44. onMouseEnter: PropTypes.func,
  45. onMouseLeave: PropTypes.func,
  46. children: PropTypes.node,
  47. icon: PropTypes.oneOfType([PropTypes.node]),
  48. className: PropTypes.string,
  49. toggleIcon: PropTypes.string,
  50. style: PropTypes.object,
  51. forwardRef: PropTypes.func,
  52. indent: PropTypes.oneOfType([PropTypes.bool, PropTypes.number]),
  53. isCollapsed: PropTypes.bool, // Is it in a state of folding to the side
  54. isSubNav: PropTypes.bool, // Whether to navigate for children
  55. link: PropTypes.string,
  56. linkOptions: PropTypes.object,
  57. disabled: PropTypes.bool,
  58. };
  59. static defaultProps = {
  60. isSubNav: false,
  61. indent: false,
  62. forwardRef: noop,
  63. isCollapsed: false,
  64. onClick: noop,
  65. onMouseEnter: noop,
  66. onMouseLeave: noop,
  67. disabled: false,
  68. };
  69. constructor(props: NavItemProps) {
  70. super(props);
  71. this.state = {
  72. tooltipShow: false,
  73. };
  74. this.foundation = new ItemFoundation(this.adapter);
  75. }
  76. _invokeContextFunc(funcName: string, ...args: any[]) {
  77. if (funcName && this.context && typeof this.context[funcName] === 'function') {
  78. return this.context[funcName](...args);
  79. }
  80. return null;
  81. }
  82. get adapter(): ItemAdapter<NavItemProps, NavItemState> {
  83. return {
  84. ...super.adapter,
  85. cloneDeep,
  86. updateTooltipShow: tooltipShow => this.setState({ tooltipShow }),
  87. updateSelected: _selected => this._invokeContextFunc('updateSelectedKeys', [this.props.itemKey]),
  88. updateGlobalSelectedKeys: keys => this._invokeContextFunc('updateSelectedKeys', [...keys]),
  89. getSelectedKeys: () => this.context && this.context.selectedKeys,
  90. getSelectedKeysIsControlled: () => this.context && this.context.selectedKeysIsControlled,
  91. notifyGlobalOnSelect: (...args) => this._invokeContextFunc('onSelect', ...args),
  92. notifyGlobalOnClick: (...args) => this._invokeContextFunc('onClick', ...args),
  93. notifyClick: (...args) => this.props.onClick(...args),
  94. notifyMouseEnter: (...args) => this.props.onMouseEnter(...args),
  95. notifyMouseLeave: (...args) => this.props.onMouseLeave(...args),
  96. getIsCollapsed: () => this.props.isCollapsed || Boolean(this.context && this.context.isCollapsed) || false,
  97. getSelected: () =>
  98. Boolean(this.context && this.context.selectedKeys && this.context.selectedKeys.includes(this.props.itemKey)),
  99. };
  100. }
  101. renderIcon(icon: React.ReactNode, pos: string, isToggleIcon = false) {
  102. if (this.props.isSubNav) {
  103. return null;
  104. }
  105. if (!icon && this.context.mode === strings.MODE_HORIZONTAL) {
  106. return null;
  107. }
  108. let iconSize = 'large';
  109. if (pos === strings.ICON_POS_RIGHT) {
  110. iconSize = 'default';
  111. }
  112. const className = cls(`${clsPrefix}-icon`, {
  113. [`${clsPrefix}-icon-toggle-${this.context.toggleIconPosition}`]: isToggleIcon,
  114. [`${clsPrefix}-icon-info`]: !isToggleIcon
  115. });
  116. return (
  117. <i className={className}>
  118. {isSemiIcon(icon) ? React.cloneElement((icon as React.ReactElement), {size: (icon as React.ReactElement).props.size || iconSize}) : icon}
  119. </i>
  120. );
  121. }
  122. setItemRef = (ref: HTMLLIElement) => {
  123. // console.log('Item - setItemRef()', ref);
  124. this.props.forwardRef && this.props.forwardRef(ref);
  125. };
  126. wrapTooltip = (node: React.ReactNode) => {
  127. const { text, tooltipHideDelay, tooltipShowDelay } = this.props;
  128. return (
  129. <Tooltip
  130. content={text}
  131. position="right"
  132. trigger={'hover'}
  133. mouseEnterDelay={tooltipShowDelay}
  134. mouseLeaveDelay={tooltipHideDelay}
  135. >
  136. {node}
  137. </Tooltip>
  138. );
  139. };
  140. handleClick = (e: React.MouseEvent) => this.foundation.handleClick(e);
  141. render() {
  142. const {
  143. text,
  144. children,
  145. icon,
  146. toggleIcon,
  147. className,
  148. isSubNav,
  149. style,
  150. indent,
  151. onMouseEnter,
  152. onMouseLeave,
  153. link,
  154. linkOptions,
  155. disabled,
  156. level = 0,
  157. } = this.props;
  158. const { mode, isInSubNav, prefixCls, limitIndent } = this.context;
  159. const isCollapsed = this.adapter.getIsCollapsed();
  160. const selected = this.adapter.getSelected();
  161. let itemChildren = null;
  162. if (!isNullOrUndefined(children)) {
  163. itemChildren = children;
  164. } else {
  165. let placeholderIcons = null;
  166. if (mode === strings.MODE_VERTICAL && !limitIndent) {
  167. const iconAmount = (icon && !indent) ? level : level - 1;
  168. placeholderIcons = times(iconAmount, () => this.renderIcon(null, strings.ICON_POS_RIGHT, false));
  169. }
  170. itemChildren = (
  171. <>
  172. {placeholderIcons}
  173. {this.context.toggleIconPosition === strings.TOGGLE_ICON_LEFT && this.renderIcon(toggleIcon, strings.ICON_POS_RIGHT, true)}
  174. {icon || indent || isInSubNav ? this.renderIcon(icon, strings.ICON_POS_LEFT) : null}
  175. {!isNullOrUndefined(text) ? <span className={`${cssClasses.PREFIX}-item-text`}>{text}</span> : ''}
  176. {this.context.toggleIconPosition === strings.TOGGLE_ICON_RIGHT && this.renderIcon(toggleIcon, strings.ICON_POS_RIGHT, true)}
  177. </>
  178. );
  179. }
  180. if (typeof link === 'string') {
  181. itemChildren = (
  182. <a className={`${prefixCls}-item-link`} href={link} {...(linkOptions as any)}>
  183. {itemChildren}
  184. </a>
  185. );
  186. }
  187. let itemDom: React.ReactNode = '';
  188. if (isInSubNav && (isCollapsed || mode === strings.MODE_HORIZONTAL)) {
  189. const popoverItemCls = cls({
  190. [clsPrefix]: true,
  191. [`${clsPrefix}-sub`]: isSubNav,
  192. [`${clsPrefix}-selected`]: selected,
  193. [`${clsPrefix}-collapsed`]: isCollapsed,
  194. [`${clsPrefix}-disabled`]: disabled,
  195. });
  196. itemDom = (
  197. <Dropdown.Item
  198. selected={selected}
  199. active={selected}
  200. forwardRef={this.setItemRef}
  201. className={popoverItemCls}
  202. onClick={this.handleClick}
  203. onMouseEnter={onMouseEnter}
  204. onMouseLeave={onMouseLeave}
  205. disabled={disabled}
  206. >
  207. {itemChildren}
  208. </Dropdown.Item>
  209. );
  210. } else {
  211. // Items are divided into normal and sub-wrap
  212. const popoverItemCls = cls(`${className || `${clsPrefix }-normal`}`, {
  213. [clsPrefix]: true,
  214. [`${clsPrefix}-sub`]: isSubNav,
  215. [`${clsPrefix}-selected`]: selected && !isSubNav,
  216. [`${clsPrefix}-collapsed`]: isCollapsed,
  217. [`${clsPrefix}-disabled`]: disabled,
  218. });
  219. itemDom = (
  220. <li
  221. style={style}
  222. ref={this.setItemRef}
  223. className={popoverItemCls}
  224. onClick={this.handleClick}
  225. onMouseEnter={onMouseEnter}
  226. onMouseLeave={onMouseLeave}
  227. >
  228. {itemChildren}
  229. </li>
  230. );
  231. }
  232. // Display Tooltip when disabled and SubNav
  233. if (isCollapsed && !isInSubNav && !isSubNav || isCollapsed && isSubNav && disabled) {
  234. itemDom = this.wrapTooltip(itemDom);
  235. }
  236. return itemDom;
  237. }
  238. }