/**
*
* Input
*
*/
import moment from 'moment';
import PropTypes from 'prop-types';
import { get, isEmpty, map, mapKeys, isObject, reject, includes } from 'lodash';
import { FormattedMessage } from 'react-intl';
import DateTime from 'react-datetime';
import DateTimeStyle from 'react-datetime/css/react-datetime.css';
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.name, value: this.props.selectOptions[0].value };
this.props.handleChange({ target });
}
if (this.props.value && !isEmpty(this.props.value)) {
this.setState({ hasInitialValue: true });
}
if (!isEmpty(this.props.errors)) {
this.setState({ errors: this.props.errors });
}
}
componentWillReceiveProps(nextProps) {
if (this.props.type === 'select' && this.props.selectOptionsFetchSucceeded !== nextProps.selectOptionsFetchSucceeded && nextProps.selectOptions[0].value !== '') {
const target = { name: nextProps.name, value: nextProps.selectOptions[0].value };
this.props.handleChange({ target });
}
// Check if errors have been updated during validations
if (this.props.didCheckErrors !== nextProps.didCheckErrors) {
// Remove from the state errors that are already set
const errors = isEmpty(nextProps.errors) ? [] : nextProps.errors;
this.setState({ errors });
}
}
handleBlur = ({ target }) => {
// prevent error display if input is initially empty
if (!isEmpty(target.value) || this.state.hasInitialValue) {
// validates basic string validations
// add custom logic here such as alerts...
const errors = this.validate(target.value);
this.setState({ errors, hasInitialValue: true });
}
}
validate = (value) => {
let errors = [];
// handle i18n
const requiredError = { id: `${this.props.pluginID}.error.validation.required` };
mapKeys(this.props.validations, (validationValue, validationKey) => {
switch (validationKey) {
case 'max':
if (parseInt(value, 10) > validationValue) {
errors.push({ id: `${this.props.pluginID}.error.validation.max` });
}
break;
case 'maxLength':
if (value.length > validationValue) {
errors.push({ id: `${this.props.pluginID}.error.validation.maxLength` });
}
break;
case 'min':
if (parseInt(value, 10) < validationValue) {
errors.push({ id: `${this.props.pluginID}.error.validation.min` });
}
break;
case 'minLength':
if (value.length < validationValue) {
errors.push({ id: `${this.props.pluginID}.error.validation.minLength` });
}
break;
case 'required':
if (value.length === 0) {
errors.push({ id: `${this.props.pluginID}.error.validation.required` });
}
break;
case 'regex':
if (!new RegExp(validationValue).test(value)) {
errors.push({ id: `${this.props.pluginID}.error.validation.regex` });
}
break;
default:
errors = [];
}
});
if (includes(errors, requiredError)) {
errors = reject(errors, (error) => error !== requiredError);
}
return errors;
}
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
?