/**
*
* Input
*
*/
import React from 'react';
import { get, isEmpty, map, isObject } from 'lodash';
import { FormattedMessage } from 'react-intl';
import styles from './styles.scss';
class Input extends React.Component { // eslint-disable-line react/prefer-stateless-function
constructor(props) {
super(props);
this.state = {
errors: [],
hasInitialValue: false,
};
}
componentDidMount() {
// Init the select value if type === "select"
if (this.props.type === 'select' && !isEmpty(this.props.selectOptions) && this.props.selectOptions[0].value !== '') {
const target = { name: this.props.target, value: this.props.selectOptions[0].value };
this.props.handleChange({ target });
}
}
componentWillReceiveProps(nextProps) {
if (this.props.type === 'select' && this.props.selectOptionsFetchSucceeded !== nextProps.selectOptionsFetchSucceeded && nextProps.selectOptions[0].value !== '') {
const target = { name: nextProps.target, value: nextProps.selectOptions[0].value };
this.props.handleChange({ target });
}
}
handleChangeCheckbox = (e) => {
const target = {
type: e.target.type,
value: !this.props.value,
name: e.target.name,
};
this.props.handleChange({ target });
}
renderErrors = (errorStyles) => { // eslint-disable-line consistent-return
if (!this.props.noErrorsDescription) {
const divStyle = errorStyles || styles.errorContainer;
return (
map(this.state.errors, (error, key) => {
const displayError = isObject(error) && error.id
?
: error;
return (
{displayError}
);
})
);
}
}
renderInputCheckbox = (requiredClass, inputDescription) => {
const title = !isEmpty(this.props.title) ?
: '';
const spacer = !inputDescription ? : ;
return (
)
}
renderInputSelect = (bootStrapClass, requiredClass, inputDescription) => {
const spacer = !isEmpty(this.props.inputDescription) ? : ;
return (
{inputDescription}
{spacer}
);
}
renderInputTextArea = (bootStrapClass, requiredClass, bootStrapClassDanger, inputDescription) => {
let spacer = !isEmpty(this.props.inputDescription) ? : ;
if (!this.props.noErrorsDescription && !isEmpty(this.state.errors)) {
spacer = ;
}
return (
{(placeholder) => (
)}
{inputDescription}
{this.renderErrors(styles.errorContainerTextArea)}
{spacer}
)
}
renderFormattedInput = (handleBlur, inputValue, placeholder) => (
{(message) => (
)}
)
render() {
const inputValue = this.props.value || '';
// override default onBlur
const handleBlur = this.props.handleBlur || this.handleBlur;
// override bootStrapClass
const bootStrapClass = this.props.customBootstrapClass ? this.props.customBootstrapClass : 'col-md-6';
// set error class with override possibility
const bootStrapClassDanger = !this.props.deactivateErrorHighlight && !isEmpty(this.state.errors) ? 'has-danger' : '';
const placeholder = this.props.placeholder || this.props.name;
const label = this.props.name ?
: ;
const requiredClass = get(this.props.validations, 'required') && this.props.addRequiredInputDesign ?
styles.requiredClass : '';
const input = placeholder ? this.renderFormattedInput(handleBlur, inputValue, placeholder)
: ;
const inputDescription = !isEmpty(this.props.inputDescription) ? : '';
let spacer = !isEmpty(this.props.inputDescription) ? : ;
if (!this.props.noErrorsDescription && !isEmpty(this.state.errors)) {
spacer = ;
}
switch (this.props.type) {
case 'select':
return this.renderInputSelect(bootStrapClass, requiredClass, inputDescription);
case 'textarea':
return this.renderInputTextArea(bootStrapClass, requiredClass, bootStrapClassDanger, inputDescription);
case 'checkbox':
return this.renderInputCheckbox(requiredClass, inputDescription);
default:
}
return (
{label}
{input}
{inputDescription}
{this.renderErrors()}
{spacer}
);
}
}
Input.propTypes = {
addRequiredInputDesign: React.PropTypes.bool,
customBootstrapClass: React.PropTypes.string,
deactivateErrorHighlight: React.PropTypes.bool,
// errors: React.PropTypes.array,
handleBlur: React.PropTypes.func,
handleChange: React.PropTypes.func.isRequired,
handleFocus: React.PropTypes.func,
inputDescription: React.PropTypes.string,
name: React.PropTypes.string.isRequired,
noErrorsDescription: React.PropTypes.bool,
placeholder: React.PropTypes.string,
// styles: React.PropTypes.object,
selectOptions: React.PropTypes.array,
selectOptionsFetchSucceeded: React.PropTypes.bool,
target: React.PropTypes.string,
title: React.PropTypes.string,
type: React.PropTypes.string.isRequired,
validations: React.PropTypes.object.isRequired,
value: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.bool,
React.PropTypes.number,
]),
};
export default Input;