22import React from 'react' ;
33import PropTypes from 'prop-types' ;
44
5- export default class TagCount extends React . Component {
6- static propTypes = {
7- count : PropTypes . number ,
8- depth : PropTypes . number ,
9- node : PropTypes . oneOfType ( [
10- PropTypes . element ,
11- PropTypes . string
12- ] ) ,
13- leaf : PropTypes . node ,
14- useCache : PropTypes . bool
15- } ;
16-
17- static defaultProps = {
18- count : 3000 ,
19- depth : 7 ,
20- node : 'div' ,
21- leaf : '.' ,
22- useCache : false
23- } ;
24-
25- static cache = { } ;
26-
27- createTag ( children ) {
28- const { node, leaf} = this . props ;
29- if ( children . length > 0 ) {
30- return React . createElement ( node , null , ...children ) ;
31- } else {
32- return React . createElement ( node , null , leaf ) ;
33- }
34- }
35-
36- generateTagsHelper ( curdepth , children , genState ) {
37- const { count, depth} = this . props ;
38-
39- if ( curdepth >= depth ) {
40- return this . createTag ( [ ] ) ;
41- }
42-
43- const newChildren = Math . max ( 0 , Math . min ( children , count - genState . count ) ) ;
44- genState . count += newChildren ;
5+ const numberOfChildren = ( depth , count ) => {
456
46- const elems = new Array ( newChildren ) ;
47- for ( let ii = 0 ; ii < newChildren ; ++ ii ) {
48- elems [ ii ] = this . generateTagsHelper ( curdepth + 1 , children , genState ) ;
7+ if ( depth == 1 || depth > count ) {
8+ return count ;
499 }
5010
51- return this . createTag ( elems ) ;
52- }
53-
54- generateTags ( ) {
55- const { depth, node, count} = this . props ;
56-
5711 let children = 2 ;
5812
5913 for ( ; ; ++ children ) {
@@ -63,30 +17,36 @@ export default class TagCount extends React.Component {
6317 }
6418 }
6519
66- const genState = { count : 1 } ;
67-
68- const tags = React . createElement ( node , null ,
69- this . generateTagsHelper ( 0 , children , genState ) ) ;
20+ return children ;
21+ } ;
7022
71- return tags ;
72- }
23+ export default class TagCount extends React . Component {
7324
7425 render ( ) {
75- const { useCache, count, depth, node, leaf} = this . props ;
76- let key ;
77- if ( useCache ) {
78- key = [ count , depth , node , leaf ] . join ( '<=>' ) ;
79- if ( TagCount . cache [ key ] ) {
80- return TagCount . cache [ key ] ;
81- }
26+ let { depth, count : tagCount } = this . props ;
27+
28+ if ( depth <= 0 ) {
29+ return < div > { 'This is a leaf' } </ div > ;
8230 }
8331
84- const tags = this . generateTags ( ) ;
32+ tagCount -= 1 ;
33+
34+ const childCount = numberOfChildren ( depth , tagCount ) ;
35+ const childTagCount = Math . ceil ( tagCount / childCount ) ;
36+ let tagsRemaining = tagCount ;
37+
38+ const children = [ ] ;
39+
40+ for ( let ii = 0 ; ii < childCount ; ++ ii ) {
41+ children . push ( < TagCount
42+ key = { ii }
43+ depth = { depth - 1 }
44+ count = { Math . min ( tagsRemaining , childTagCount ) }
45+ /> ) ;
8546
86- if ( useCache ) {
87- TagCount . cache [ key ] = tags ;
47+ tagsRemaining -= childTagCount ;
8848 }
8949
90- return tags ;
50+ return < div > { children } </ div > ;
9151 }
92- } ;
52+ }
0 commit comments