@@ -9,16 +9,30 @@ import Template from '../components/Template.js';
99
1010function headerFooter ( ComposedComponent ) {
1111 class HeaderFooter extends React . Component {
12- componentWillMount ( ) {
13- // Only add header/footer if a template is defined
14- this . _header = this . getTemplate ( 'header' ) ;
15- this . _footer = this . getTemplate ( 'footer' ) ;
16- this . _classNames = {
17- root : cx ( this . props . cssClasses . root ) ,
18- body : cx ( this . props . cssClasses . body )
12+ constructor ( props ) {
13+ super ( props ) ;
14+ this . handleHeaderClick = this . handleHeaderClick . bind ( this ) ;
15+ this . state = {
16+ collapsed : props . collapsible && props . collapsible . collapsed
1917 } ;
18+
19+ this . _headerElement = this . _getElement ( {
20+ type : 'header' ,
21+ handleClick : props . collapsible ? this . handleHeaderClick : null
22+ } ) ;
23+
24+ this . _cssClasses = {
25+ root : cx ( 'ais-root' , this . props . cssClasses . root ) ,
26+ body : cx ( 'ais-body' , this . props . cssClasses . body )
27+ } ;
28+
29+ this . _footerElement = this . _getElement ( { type : 'footer' } ) ;
2030 }
21- getTemplate ( type ) {
31+ shouldComponentUpdate ( nextProps , nextState ) {
32+ return nextState . collapsed === false ||
33+ nextState !== this . state ;
34+ }
35+ _getElement ( { type, handleClick = null } ) {
2236 let templates = this . props . templateProps . templates ;
2337 if ( ! templates || ! templates [ type ] ) {
2438 return null ;
@@ -27,25 +41,54 @@ function headerFooter(ComposedComponent) {
2741 return (
2842 < Template { ...this . props . templateProps }
2943 cssClass = { className }
44+ onClick = { handleClick }
3045 templateKey = { type }
3146 transformData = { null }
3247 />
3348 ) ;
3449 }
50+ handleHeaderClick ( ) {
51+ this . setState ( {
52+ collapsed : ! this . state . collapsed
53+ } ) ;
54+ }
3555 render ( ) {
56+ let rootCssClasses = [ this . _cssClasses . root ] ;
57+
58+ if ( this . props . collapsible ) {
59+ rootCssClasses . push ( 'ais-root__collapsible' ) ;
60+ }
61+
62+ if ( this . state . collapsed ) {
63+ rootCssClasses . push ( 'ais-root__collapsed' ) ;
64+ }
65+
66+ const cssClasses = {
67+ ...this . _cssClasses ,
68+ root : cx ( rootCssClasses )
69+ } ;
70+
3671 return (
37- < div className = { this . _classNames . root } >
38- { this . _header }
39- < div className = { this . _classNames . body } >
72+ < div className = { cssClasses . root } >
73+ { this . _headerElement }
74+ < div
75+ className = { cssClasses . body }
76+ >
4077 < ComposedComponent { ...this . props } />
4178 </ div >
42- { this . _footer }
79+ { this . _footerElement }
4380 </ div >
4481 ) ;
4582 }
4683 }
4784
4885 HeaderFooter . propTypes = {
86+ collapsible : React . PropTypes . oneOfType ( [
87+ React . PropTypes . bool ,
88+ React . PropTypes . shape ( {
89+ collapsed : React . PropTypes . bool
90+ } )
91+ ] ) ,
4992 cssClasses : React . PropTypes . shape ( {
5093 root : React . PropTypes . string ,
5194 header : React . PropTypes . string ,
@@ -56,7 +99,8 @@ function headerFooter(ComposedComponent) {
5699 } ;
57100
58101 HeaderFooter . defaultProps = {
59- cssClasses : { }
102+ cssClasses : { } ,
103+ collapsible : false
60104 } ;
61105
62106 // precise displayName for ease of debugging (react dev tool, react warnings)
0 commit comments