1+ import React , { Component , PropTypes } from 'react' ;
2+ import AlertMessage from '../common/AlertMessage' ;
3+ import TabsForm from './../form/TabsForm' ;
4+
5+ class Group extends Component {
6+ static propTypes = {
7+ component : PropTypes . string ,
8+ fields : PropTypes . array . isRequired ,
9+ layout : PropTypes . object . isRequired ,
10+ componentFactory : PropTypes . object . isRequired
11+ } ;
12+
13+ getComponents = ( ) => {
14+ let { layout, componentFactory, fields } = this . props ;
15+ let components ;
16+
17+ if ( layout . fields ) {
18+
19+ components = layout . fields . map ( field => {
20+ let fieldMetadata = fields . find ( cp => cp . name === field . name ) ;
21+
22+ if ( ! fieldMetadata ) {
23+ throw Error ( `Could not find field. Field: ${ field . name } ` ) ;
24+ }
25+
26+ // in case the field is going to render layouts internally, it's going to need information about the
27+ // layout and fields. I'm not sure if this is the best way to do it, probably not. TODO: Review it.
28+ fieldMetadata . _extra = { layout, fields} ;
29+
30+ return {
31+ data : fieldMetadata ,
32+ length : layout . fields . length ,
33+ component : componentFactory . buildFieldComponent ( fieldMetadata )
34+ }
35+ } ) ;
36+
37+ } else if ( layout . groups ) {
38+
39+ components = layout . groups . map ( group => {
40+ return {
41+ data : group ,
42+ length : layout . groups . length ,
43+ component : componentFactory . buildGroupComponent ( {
44+ component : group . component ,
45+ layout : group ,
46+ fields : fields ,
47+ componentFactory : componentFactory
48+ } )
49+ }
50+ } ) ;
51+ }
52+
53+ return components ;
54+ } ;
55+
56+ getDefaultSize = ( component , gridLength = 12 ) => {
57+ let { layout } = this . props ;
58+
59+ return layout . orientation == 'horizontal' ? Math . floor ( gridLength / component . length ) : gridLength ;
60+ } ;
61+
62+ getContent = ( ) => {
63+ let components = this . getComponents ( ) ;
64+
65+ return components . map ( ( component , i ) => {
66+ let componentContent , size = 12 ;
67+
68+ // invisible components should be hidden
69+ if ( component . data . visible === false ) {
70+ componentContent = null ;
71+ } else {
72+ size = component . data . size || this . getDefaultSize ( component ) ;
73+ componentContent = component . component ;
74+ }
75+
76+ return (
77+ < div key = { `component-${ i } -wrapper` } className = { `col-md-${ size } ` } >
78+ { componentContent }
79+ </ div >
80+ ) ;
81+ } ) ;
82+ } ;
83+
84+ render ( ) {
85+ let { layout } = this . props ;
86+
87+ // the passed in layout can contain either fields or groups.
88+ // in case it contains 'fields', we're gonna render each of the fields.
89+ // in case it contains 'groups', we're gonna render each group, passing the fields as a parameter
90+ try {
91+ return < TabsForm layout = { layout } content = { this . getContent ( ) } /> ;
92+ } catch ( ex ) {
93+ return < AlertMessage error = { ex } />
94+ }
95+ }
96+ }
97+
98+ export default Group ;
0 commit comments