2017-07-25 12:42:03 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* WithFormSection
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2017-07-26 11:48:05 +02:00
|
|
|
import { forEach, has } from 'lodash';
|
|
|
|
|
2017-07-25 12:42:03 +02:00
|
|
|
import InputNumber from 'components/InputNumber';
|
|
|
|
import InputText from 'components/InputText';
|
|
|
|
import InputToggle from 'components/InputToggle';
|
|
|
|
import InputSelect from 'components/InputSelect';
|
|
|
|
import InputEnum from 'components/InputEnum';
|
|
|
|
import config from './config.json';
|
2017-07-25 15:34:43 +02:00
|
|
|
import styles from './styles.scss';
|
2017-07-25 12:42:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
const WithFormSection = (InnerComponent) => class extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
handleChange: React.PropTypes.func.isRequired,
|
|
|
|
values: React.PropTypes.object,
|
|
|
|
}
|
|
|
|
|
2017-07-26 11:48:05 +02:00
|
|
|
renderInput = (section, props, key) => {
|
2017-07-25 12:42:03 +02:00
|
|
|
const inputs = {
|
|
|
|
string: InputText,
|
|
|
|
number: InputNumber,
|
|
|
|
boolean: InputToggle,
|
|
|
|
enum: InputEnum,
|
|
|
|
select: InputSelect,
|
|
|
|
};
|
|
|
|
const Input = inputs[props.type];
|
2017-07-26 11:48:05 +02:00
|
|
|
let customBootstrapClass = config[props.target] || '';
|
2017-07-25 12:42:03 +02:00
|
|
|
const inputValue = this.props.values[props.target];
|
|
|
|
// retrieve options for the select input
|
2017-07-25 15:34:43 +02:00
|
|
|
const selectOptions = props.type === 'enum' || props.type === 'select' ? props.items : [];
|
|
|
|
|
2017-07-26 11:48:05 +02:00
|
|
|
// check if there is inside a section an input that requires nested input to display it on the entire line
|
|
|
|
forEach(section, (items) => {
|
|
|
|
forEach(items.items, (item) => {
|
|
|
|
customBootstrapClass = has(item, 'items') && items.type === 'enum' ? 'col-md-6 offset-md-6 pull-md-6' : '';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-25 12:42:03 +02:00
|
|
|
return (
|
|
|
|
<Input
|
|
|
|
customBootstrapClass={customBootstrapClass}
|
|
|
|
key={key}
|
|
|
|
handleChange={this.props.handleChange}
|
|
|
|
name={props.name}
|
|
|
|
target={props.target}
|
|
|
|
isChecked={inputValue}
|
|
|
|
selectOptions={selectOptions}
|
|
|
|
validations={props.validations}
|
|
|
|
value={inputValue}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<InnerComponent
|
|
|
|
{...this.props}
|
|
|
|
renderInput={this.renderInput}
|
2017-07-25 15:34:43 +02:00
|
|
|
styles={styles}
|
2017-07-25 12:42:03 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WithFormSection;
|