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 = {
|
2017-07-27 16:07:09 +02:00
|
|
|
checkForNestedForm: React.PropTypes.bool,
|
2017-07-25 12:42:03 +02:00
|
|
|
handleChange: React.PropTypes.func.isRequired,
|
2017-07-26 15:22:54 +02:00
|
|
|
section: React.PropTypes.object,
|
2017-07-25 12:42:03 +02:00
|
|
|
values: React.PropTypes.object,
|
|
|
|
}
|
|
|
|
|
2017-07-26 15:22:54 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
hasNestedInput: false,
|
|
|
|
showNestedForm: false,
|
|
|
|
inputWithNestedForm: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = ({ target }) => {
|
|
|
|
|
|
|
|
// display nestedForm if the selected input has a nested form
|
|
|
|
if (target.id === this.state.inputWithNestedForm) {
|
|
|
|
this.setState({ showNestedForm: true });
|
|
|
|
} else {
|
|
|
|
this.setState({ showNestedForm: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.handleChange({ target });
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
// check if there is inside a section an input that requires nested input to display it on the entire line
|
2017-07-27 16:07:09 +02:00
|
|
|
if (this.props.checkForNestedForm) {
|
|
|
|
forEach(this.props.section.items, (items) => {
|
|
|
|
forEach(items, (item) => {
|
|
|
|
forEach(item, (input) => {
|
|
|
|
|
|
|
|
if (has(input, 'items')) {
|
|
|
|
// store the name of the input that has a nested form
|
|
|
|
this.setState({ hasNestedInput: true, inputWithNestedForm: input.name });
|
|
|
|
|
|
|
|
// showNestedForm if the selected input has a nested form
|
|
|
|
if (items.value === input.value) {
|
|
|
|
this.setState({ showNestedForm: true });
|
|
|
|
}
|
2017-07-26 15:22:54 +02:00
|
|
|
}
|
2017-07-27 16:07:09 +02:00
|
|
|
});
|
2017-07-26 15:22:54 +02:00
|
|
|
});
|
|
|
|
});
|
2017-07-27 16:07:09 +02:00
|
|
|
}
|
2017-07-26 15:22:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
renderInput = (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];
|
|
|
|
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 15:22:54 +02:00
|
|
|
// check if the input has a nested form so it is display on the entire line
|
|
|
|
const customBootstrapClass = this.state.hasNestedInput ?
|
|
|
|
// bootstrap class to make the input display on the entire line
|
|
|
|
'col-md-6 offset-md-6 pull-md-6' :
|
|
|
|
// if the input hasn't a nested form but the config requires him to be displayed differently
|
|
|
|
config[props.target] || '';
|
|
|
|
|
|
|
|
// custom handle change props for nested input form
|
|
|
|
const handleChange = this.state.hasNestedInput ? this.handleChange : this.props.handleChange;
|
2017-07-25 12:42:03 +02:00
|
|
|
return (
|
|
|
|
<Input
|
|
|
|
customBootstrapClass={customBootstrapClass}
|
|
|
|
key={key}
|
2017-07-26 15:22:54 +02:00
|
|
|
handleChange={handleChange}
|
2017-07-25 12:42:03 +02:00
|
|
|
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;
|
2017-07-26 15:22:54 +02:00
|
|
|
|
|
|
|
// Object {name: "form.security.item.xframe.allow-from", value: "ALLOW-FROM", items: Array(1)}
|