1
+ import SubPanel from './SubPanel' ;
1
2
import React , { Component , cloneElement } from 'react' ;
2
3
import PropTypes from 'prop-types' ;
3
- import { bem } from '../../lib' ;
4
+ import { icon } from '../../lib' ;
4
5
import unpackPlotProps from '../../lib/unpackPlotProps' ;
5
6
6
7
function childIsVisible ( child ) {
@@ -11,42 +12,63 @@ class Section extends Component {
11
12
constructor ( props , context ) {
12
13
super ( props , context ) ;
13
14
14
- this . children = this . processChildren ( context ) ;
15
+ this . children = null ;
16
+ this . subPanel = null ;
17
+
18
+ this . processAndSetChildren ( context ) ;
15
19
}
16
20
17
21
componentWillReceiveProps ( nextProps , nextContext ) {
18
- this . children = this . processChildren ( nextContext ) ;
22
+ this . processAndSetChildren ( nextContext ) ;
19
23
}
20
24
21
- processChildren ( context ) {
25
+ processAndSetChildren ( context ) {
22
26
let children = this . props . children ;
23
27
if ( ! Array . isArray ( children ) ) {
24
28
children = [ children ] ;
25
29
}
26
- children = children . filter ( c => Boolean ( c ) ) ;
30
+
31
+ const attrChildren = [ ] ;
32
+ let subPanel = null ;
27
33
28
34
for ( let i = 0 ; i < children . length ; i ++ ) {
29
35
let child = children [ i ] ;
36
+ if ( ! child ) {
37
+ continue ;
38
+ }
39
+ if ( child . type === SubPanel ) {
40
+ // Process the first subPanel. Ignore the rest.
41
+ if ( subPanel ) {
42
+ continue ;
43
+ }
44
+ subPanel = child ;
45
+ continue ;
46
+ }
30
47
31
48
let isAttr = ! ! child . props . attr ;
32
49
let plotProps = isAttr
33
50
? unpackPlotProps ( child . props , context , child . constructor )
34
- : { } ;
51
+ : { isVisible : true } ;
35
52
let childProps = Object . assign ( { plotProps} , child . props ) ;
36
53
childProps . key = i ;
37
-
38
- children [ i ] = cloneElement ( child , childProps , child . children ) ;
54
+ attrChildren . push ( cloneElement ( child , childProps ) ) ;
39
55
}
40
56
41
- return children ;
57
+ this . children = attrChildren . length ? attrChildren : null ;
58
+ this . subPanel = subPanel ;
42
59
}
43
60
44
61
render ( ) {
45
- const hasVisibleChildren = this . children . some ( childIsVisible ) ;
62
+ const hasVisibleChildren =
63
+ ( this . children && this . children . some ( childIsVisible ) ) ||
64
+ Boolean ( this . subPanel ) ;
46
65
47
66
return hasVisibleChildren ? (
48
- < div className = { bem ( 'section' ) } >
49
- < div className = { bem ( 'section' , 'heading' ) } > { this . props . name } </ div >
67
+ < div className = "section" >
68
+ < div className = "section__heading" >
69
+ { this . props . name }
70
+ { this . subPanel }
71
+ </ div >
50
72
{ this . children }
51
73
</ div >
52
74
) : null ;
0 commit comments