/** * * Input * */ import React from 'react'; 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, showPassword: false, }; } componentDidMount() { 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) { // 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: 'components.Input.error.validation.required' }; mapKeys(this.props.validations, (validationValue, validationKey) => { switch (validationKey) { case 'max': if (parseInt(value, 10) > validationValue) { errors.push({ id: 'components.Input.error.validation.max' }); } break; case 'maxLength': if (value.length > validationValue) { errors.push({ id: 'components.Input.error.validation.maxLength' }); } break; case 'min': if (parseInt(value, 10) < validationValue) { errors.push({ id: 'components.Input.error.validation.min' }); } break; case 'minLength': if (value.length < validationValue) { errors.push({ id: 'components.Input.error.validation.minLength' }); } break; case 'required': if (value.length === 0) { errors.push({ id: 'components.Input.error.validation.required' }); } break; case 'regex': if (!new RegExp(validationValue).test(value)) { errors.push({ id: 'components.Input.error.validation.regex' }); } break; default: errors = []; } }); if (includes(errors, requiredError)) { errors = reject(errors, (error) => error !== requiredError); } return errors; } handleChangeCheckbox = (e) => { const target = { type: 'checkbox', value: !this.props.value, name: this.props.name, }; this.props.onChange({ 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 (
{title} {(message) => ( )}
{inputDescription}
{spacer}
) } renderInputSelect = (requiredClass, inputDescription, handleBlur) => { let spacer = !isEmpty(this.props.inputDescription) ?
:
; if (!this.props.noErrorsDescription && !isEmpty(this.state.errors)) { spacer =
; } return (
{inputDescription}
{this.renderErrors()} {spacer}
); } renderInputTextArea = (requiredClass, inputDescription, handleBlur) => { let spacer = !isEmpty(this.props.inputDescription) ?
:
; if (!this.props.noErrorsDescription && !isEmpty(this.state.errors)) { spacer =
; } return (
{(placeholder) => (