-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#24 Just making it a little bit better
- Loading branch information
1 parent
de658e7
commit e843390
Showing
3 changed files
with
91 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import AlertMessage from '../common/AlertMessage'; | ||
import NormalForm from './../form/NormalForm'; | ||
|
||
class BaseGroup extends Component { | ||
static propTypes = { | ||
component: PropTypes.string, | ||
fields: PropTypes.array.isRequired, | ||
layout: PropTypes.object.isRequired, | ||
componentFactory: PropTypes.object.isRequired | ||
}; | ||
|
||
getComponents = () => { | ||
let { layout, componentFactory, fields } = this.props; | ||
let components; | ||
|
||
if (layout.fields) { | ||
|
||
components = layout.fields.map(field => { | ||
let fieldMetadata = fields.find(cp => cp.name === field.name); | ||
|
||
if (!fieldMetadata) { | ||
throw Error(`Could not find field. Field: ${field.name}`); | ||
} | ||
|
||
// in case the field is going to render layouts internally, it's going to need information about the | ||
// layout and fields. I'm not sure if this is the best way to do it, probably not. TODO: Review it. | ||
fieldMetadata._extra = {layout, fields}; | ||
|
||
return { | ||
data: fieldMetadata, | ||
length: layout.fields.length, | ||
component: componentFactory.buildFieldComponent(fieldMetadata) | ||
} | ||
}); | ||
|
||
} else if (layout.groups) { | ||
|
||
components = layout.groups.map(group => { | ||
return { | ||
data: group, | ||
length: layout.groups.length, | ||
component: componentFactory.buildGroupComponent({ | ||
component: group.component, | ||
layout: group, | ||
fields: fields, | ||
componentFactory: componentFactory | ||
}) | ||
} | ||
}); | ||
} | ||
|
||
return components; | ||
}; | ||
|
||
getDefaultSize = (component, gridLength = 12) => { | ||
let { layout } = this.props; | ||
|
||
return layout.orientation == 'horizontal' ? Math.floor(gridLength/component.length) : gridLength; | ||
}; | ||
|
||
getContent = () => { | ||
let components = this.getComponents(); | ||
|
||
return components.map((component, i) => { | ||
let componentContent, size = 12; | ||
|
||
// invisible components should be hidden | ||
if (component.data.visible === false) { | ||
componentContent = null; | ||
} else { | ||
size = component.data.size || this.getDefaultSize(component); | ||
componentContent = component.component; | ||
} | ||
|
||
return ( | ||
<div key={`component-${i}-wrapper`} className={`col-md-${size}`}> | ||
{ componentContent } | ||
</div> | ||
); | ||
}); | ||
}; | ||
} | ||
|
||
export default BaseGroup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters