1
0

treeNode.tsx 13 KB

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