1+ import React from 'react' ;
2+
3+ // 判断是否叶子节点
4+ const isLeaf = ( data , prop ) => {
5+ const node = prop . node ;
6+ return ! ( Array . isArray ( data [ node . children ] ) && data [ node . children ] . length > 0 ) ;
7+ } ;
8+
9+ // 创建 node 节点
10+ export const renderNode = ( data , prop ) => {
11+ const node = prop . node ;
12+ const cls = [ 'org-tree-node' ] ;
13+ const childNodes = [ ] ;
14+
15+ if ( isLeaf ( data , prop ) ) {
16+ cls . push ( 'is-leaf' ) ;
17+ }
18+
19+ childNodes . push ( renderLabel ( data , prop ) ) ;
20+
21+ if ( data [ node . expand ] ) {
22+ childNodes . push ( renderChildren ( data . children , prop ) ) ;
23+ }
24+
25+ return React . createElement ( 'div' , {
26+ key : data . id ,
27+ className : cls . join ( ' ' )
28+ } , childNodes ) ;
29+ } ;
30+
31+ // 创建 label 节点
32+ export const renderLabel = ( data , prop ) => {
33+ const node = prop . node ;
34+ const label = data [ node . label ] ;
35+ const renderContent = prop . renderContent ;
36+
37+ const childNodes = [ ] ;
38+ if ( typeof renderContent === 'function' ) {
39+ let vnode = renderContent ( data ) ;
40+
41+ vnode && childNodes . push ( vnode ) ;
42+ } else {
43+ childNodes . push ( label ) ;
44+ }
45+
46+ const cls = [ 'org-tree-node-label-inner' ] ;
47+
48+ return React . createElement ( 'div' , {
49+ className : 'org-tree-node-label' ,
50+ } , [ React . createElement ( 'div' , {
51+ className : cls . join ( ' ' )
52+ } , childNodes ) ] ) ;
53+ } ;
54+
55+ // 创建 node 子节点
56+ export const renderChildren = ( list , prop ) => {
57+ if ( Array . isArray ( list ) && list . length ) {
58+ const children = list . map ( item => {
59+ return renderNode ( item , prop ) ;
60+ } ) ;
61+
62+ return React . createElement ( 'div' , {
63+ className : 'org-tree-node-children'
64+ } , children ) ;
65+ }
66+ return '' ;
67+ } ;
68+
69+ export const render = ( props ) => {
70+ return renderNode ( props . data , props ) ;
71+ } ;
72+
73+ export default render ;
74+
0 commit comments