treeNode.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import React, { PureComponent } from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { cssClasses } from '@douyinfe/semi-foundation/tree/constants';
  5. import { debounce, isFunction, isString } from 'lodash-es';
  6. import { IconTreeTriangleDown, IconFile, IconFolder, IconFolderOpen } from '@douyinfe/semi-icons';
  7. import { Checkbox } from '../checkbox';
  8. import TreeContext from './treeContext';
  9. import Spin from '../spin';
  10. import { TreeNodeProps, TreeNodeState } from './interface';
  11. const prefixcls = cssClasses.PREFIX_OPTION;
  12. export default class TreeNode extends PureComponent<TreeNodeProps, TreeNodeState> {
  13. static contextType = TreeContext;
  14. static propTypes = {
  15. expanded: PropTypes.bool,
  16. selected: PropTypes.bool,
  17. checked: PropTypes.bool,
  18. halfChecked: PropTypes.bool,
  19. active: PropTypes.bool,
  20. disabled: PropTypes.bool,
  21. loaded: PropTypes.bool,
  22. loading: PropTypes.bool,
  23. isLeaf: PropTypes.bool,
  24. pos: PropTypes.string,
  25. children: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
  26. icon: PropTypes.node,
  27. directory: PropTypes.bool,
  28. keyword: PropTypes.string,
  29. treeNodeFilterProp: PropTypes.string,
  30. selectedKey: PropTypes.string,
  31. motionKey: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)])
  32. };
  33. static defaultProps = {
  34. selectedKey: '',
  35. motionKey: '',
  36. };
  37. debounceSelect: any;
  38. refNode: HTMLElement;
  39. constructor(props: TreeNodeProps) {
  40. super(props);
  41. this.state = {};
  42. this.debounceSelect = debounce(this.onSelect, 500, {
  43. leading: true,
  44. trailing: false
  45. });
  46. }
  47. onSelect = (e: React.MouseEvent) => {
  48. const { onNodeSelect } = this.context;
  49. onNodeSelect(e, this.props);
  50. };
  51. onExpand = (e: React.MouseEvent) => {
  52. const { onNodeExpand } = this.context;
  53. e && e.stopPropagation();
  54. e.nativeEvent.stopImmediatePropagation();
  55. onNodeExpand(e, this.props);
  56. };
  57. onCheck = (e: React.MouseEvent) => {
  58. if (this.isDisabled()) {
  59. return;
  60. }
  61. const { onNodeCheck } = this.context;
  62. e.stopPropagation();
  63. e.nativeEvent.stopImmediatePropagation();
  64. onNodeCheck(e, this.props);
  65. };
  66. onContextMenu = (e: React.MouseEvent) => {
  67. const { onNodeRightClick } = this.context;
  68. e.stopPropagation();
  69. e.nativeEvent.stopImmediatePropagation();
  70. onNodeRightClick(e, this.props);
  71. };
  72. onClick = (e: React.MouseEvent) => {
  73. const { expandAction } = this.context;
  74. if (expandAction === 'doubleClick') {
  75. this.debounceSelect(e);
  76. return;
  77. }
  78. this.onSelect(e);
  79. if (expandAction === 'click') {
  80. this.onExpand(e);
  81. }
  82. };
  83. onDoubleClick = (e: React.MouseEvent) => {
  84. const { expandAction, onNodeDoubleClick } = this.context;
  85. e.stopPropagation();
  86. e.nativeEvent.stopImmediatePropagation();
  87. if (isFunction(onNodeDoubleClick)) {
  88. onNodeDoubleClick(e, this.props);
  89. }
  90. if (expandAction === 'doubleClick') {
  91. this.onExpand(e);
  92. }
  93. };
  94. onDragStart = (e: React.DragEvent) => {
  95. const { onNodeDragStart } = this.context;
  96. e.stopPropagation();
  97. onNodeDragStart(e, { ...this.props, nodeInstance: this.refNode });
  98. try {
  99. // ie throw error
  100. // firefox-need-it
  101. e.dataTransfer.setData('text/plain', '');
  102. } catch (error) {
  103. // empty
  104. }
  105. };
  106. onDragEnter = (e: React.DragEvent) => {
  107. const { onNodeDragEnter } = this.context;
  108. e.preventDefault();
  109. e.stopPropagation();
  110. onNodeDragEnter(e, { ...this.props, nodeInstance: this.refNode });
  111. };
  112. onDragOver = (e: React.DragEvent) => {
  113. const { onNodeDragOver } = this.context;
  114. e.preventDefault();
  115. e.stopPropagation();
  116. onNodeDragOver(e, { ...this.props, nodeInstance: this.refNode });
  117. };
  118. onDragLeave = (e: React.DragEvent) => {
  119. const { onNodeDragLeave } = this.context;
  120. e.stopPropagation();
  121. onNodeDragLeave(e, { ...this.props, nodeInstance: this.refNode });
  122. };
  123. onDragEnd = (e: React.DragEvent) => {
  124. const { onNodeDragEnd } = this.context;
  125. e.stopPropagation();
  126. onNodeDragEnd(e, { ...this.props, nodeInstance: this.refNode });
  127. };
  128. onDrop = (e: React.DragEvent) => {
  129. const { onNodeDrop } = this.context;
  130. e.preventDefault();
  131. e.stopPropagation();
  132. onNodeDrop(e, { ...this.props, nodeInstance: this.refNode });
  133. };
  134. getNodeChildren = () => {
  135. const { children } = this.props;
  136. return children || [];
  137. };
  138. isLeaf = () => {
  139. const { isLeaf, loaded } = this.props;
  140. const { loadData } = this.context;
  141. const hasChildren = this.getNodeChildren().length !== 0;
  142. if (isLeaf === false) {
  143. return false;
  144. }
  145. return isLeaf || (!loadData && !hasChildren) || (loadData && loaded && !hasChildren);
  146. };
  147. isDisabled = () => {
  148. const { disabled } = this.props;
  149. const { treeDisabled } = this.context;
  150. if (disabled === false) {
  151. return false;
  152. }
  153. return Boolean(treeDisabled || disabled);
  154. };
  155. renderArrow() {
  156. const showIcon = !this.isLeaf();
  157. const { loading } = this.props;
  158. if (loading) {
  159. return <Spin wrapperClassName={`${prefixcls}-spin-icon`} />;
  160. }
  161. if (showIcon) {
  162. return (
  163. <IconTreeTriangleDown
  164. className={`${prefixcls}-expand-icon`}
  165. size="small"
  166. onClick={this.onExpand}
  167. />
  168. );
  169. }
  170. return (
  171. <span className={`${prefixcls}-empty-icon`} />
  172. );
  173. }
  174. renderCheckbox() {
  175. const { checked, halfChecked } = this.props;
  176. const disabled = this.isDisabled();
  177. return (
  178. <div onClick={this.onCheck}>
  179. <Checkbox
  180. indeterminate={halfChecked}
  181. checked={checked}
  182. disabled={Boolean(disabled)}
  183. />
  184. </div>
  185. );
  186. }
  187. renderIcon() {
  188. const {
  189. directory,
  190. treeIcon
  191. } = this.context;
  192. const { expanded, icon } = this.props;
  193. const hasChild = !this.isLeaf();
  194. const hasIcon = icon || treeIcon;
  195. let itemIcon;
  196. if (hasIcon || directory) {
  197. if (hasIcon) {
  198. itemIcon = icon || treeIcon;
  199. } else {
  200. if (!hasChild) {
  201. itemIcon = <IconFile className={`${prefixcls}-item-icon`} />;
  202. } else {
  203. // eslint-disable-next-line max-len
  204. itemIcon = expanded ? <IconFolderOpen className={`${prefixcls}-item-icon`} /> : <IconFolder className={`${prefixcls}-item-icon`} />;
  205. }
  206. }
  207. }
  208. return itemIcon;
  209. }
  210. renderEmptyNode() {
  211. const { emptyContent } = this.props;
  212. const wrapperCls = cls(prefixcls, {
  213. [`${prefixcls}-empty`]: true,
  214. });
  215. return (
  216. <ul className={wrapperCls}>
  217. <span className={`${prefixcls}-label ${prefixcls}-label-empty`}>
  218. {emptyContent}
  219. </span>
  220. </ul>
  221. );
  222. }
  223. renderRealLabel = () => {
  224. const { renderLabel } = this.context;
  225. const { label, keyword, data, filtered, treeNodeFilterProp } = this.props;
  226. if (isFunction(renderLabel)) {
  227. return renderLabel(label, data);
  228. } else if (isString(label) && filtered && keyword && treeNodeFilterProp === 'label') {
  229. const content: React.ReactNode[] = [];
  230. label.split(keyword).forEach((node, index) => {
  231. if (index > 0) {
  232. content.push(
  233. <span className={`${prefixcls}-highlight`} key={index}>
  234. {keyword}
  235. </span>
  236. );
  237. }
  238. content.push(node);
  239. });
  240. return content;
  241. } else {
  242. return label;
  243. }
  244. };
  245. setRef = (node: HTMLElement) => {
  246. this.refNode = node;
  247. };
  248. // eslint-disable-next-line max-lines-per-function
  249. render() {
  250. const {
  251. eventKey,
  252. expanded,
  253. selected,
  254. checked,
  255. halfChecked,
  256. loading,
  257. active,
  258. level,
  259. empty,
  260. filtered,
  261. treeNodeFilterProp,
  262. // eslint-disable-next-line no-unused-vars
  263. display,
  264. style,
  265. ...rest
  266. } = this.props;
  267. if (empty) {
  268. return this.renderEmptyNode();
  269. }
  270. const {
  271. multiple,
  272. draggable,
  273. renderFullLabel,
  274. dragOverNodeKey,
  275. dropPosition,
  276. labelEllipsis
  277. } = this.context;
  278. const disabled = this.isDisabled();
  279. const dragOver = dragOverNodeKey === eventKey && dropPosition === 0;
  280. const dragOverGapTop = dragOverNodeKey === eventKey && dropPosition === -1;
  281. const dragOverGapBottom = dragOverNodeKey === eventKey && dropPosition === 1;
  282. const nodeCls = cls(prefixcls, {
  283. [`${prefixcls}-level-${level + 1}`]: true,
  284. [`${prefixcls}-collapsed`]: !expanded,
  285. [`${prefixcls}-disabled`]: Boolean(disabled),
  286. [`${prefixcls}-selected`]: selected,
  287. [`${prefixcls}-active`]: !multiple && active,
  288. [`${prefixcls}-ellipsis`]: labelEllipsis,
  289. [`${prefixcls}-filtered`]: filtered && treeNodeFilterProp !== 'label',
  290. [`${prefixcls}-drag-over`]: !disabled && dragOver,
  291. [`${prefixcls}-draggable`]: !disabled && draggable && !renderFullLabel,
  292. // When draggable + renderFullLabel is enabled, the default style
  293. [`${prefixcls}-fullLabel-draggable`]: !disabled && draggable && renderFullLabel,
  294. // When draggable + renderFullLabel is turned on, the style of dragover
  295. [`${prefixcls}-fullLabel-drag-over-gap-top`]: !disabled && dragOverGapTop && renderFullLabel,
  296. [`${prefixcls}-fullLabel-drag-over-gap-bottom`]: !disabled && dragOverGapBottom && renderFullLabel,
  297. });
  298. const labelProps = {
  299. onClick: this.onClick,
  300. onContextMenu: this.onContextMenu,
  301. onDoubleClick: this.onDoubleClick,
  302. className: nodeCls,
  303. onExpand: this.onExpand,
  304. data: rest.data,
  305. level,
  306. onCheck: this.onCheck,
  307. style,
  308. expandIcon: this.renderArrow(),
  309. checkStatus: {
  310. checked,
  311. halfChecked,
  312. },
  313. expandStatus: {
  314. expanded,
  315. loading,
  316. },
  317. };
  318. const dragProps = {
  319. onDoubleClick: this.onDoubleClick,
  320. onDragStart: draggable ? this.onDragStart : undefined,
  321. onDragEnter: draggable ? this.onDragEnter : undefined,
  322. onDragOver: draggable ? this.onDragOver : undefined,
  323. onDragLeave: draggable ? this.onDragLeave : undefined,
  324. onDrop: draggable ? this.onDrop : undefined,
  325. onDragEnd: draggable ? this.onDragEnd : undefined,
  326. draggable: (!disabled && draggable) || undefined,
  327. };
  328. if (renderFullLabel) {
  329. const customLabel = renderFullLabel({ ...labelProps });
  330. if (draggable) {
  331. return React.cloneElement(customLabel, {
  332. ref: this.setRef,
  333. ...dragProps
  334. });
  335. } else {
  336. return customLabel;
  337. }
  338. }
  339. const labelCls = cls(`${prefixcls}-label`, {
  340. [`${prefixcls}-drag-over-gap-top`]: !disabled && dragOverGapTop,
  341. [`${prefixcls}-drag-over-gap-bottom`]: !disabled && dragOverGapBottom,
  342. });
  343. return (
  344. <li
  345. className={nodeCls}
  346. role="treenode"
  347. data-key={eventKey}
  348. onClick={this.onClick}
  349. onContextMenu={this.onContextMenu}
  350. onDoubleClick={this.onDoubleClick}
  351. ref={this.setRef}
  352. style={style}
  353. {...dragProps}
  354. >
  355. {this.renderArrow()}
  356. <span
  357. className={labelCls}
  358. >
  359. {multiple ? this.renderCheckbox() : null}
  360. {this.renderIcon()}
  361. <span className={`${prefixcls}-label-text`}>{this.renderRealLabel()}</span>
  362. </span>
  363. </li>
  364. );
  365. }
  366. }