/** * * 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, isFocus: 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 }); } } handleChangeCheckbox = (e) => { const target = { type: 'checkbox', value: !this.props.value, name: this.props.name, }; this.props.onChange({ target }); } handleBlurEmail = (e) => { this.setState({ isFocus: !this.state.isFocus }); if (this.props.handleBlur) { this.props.handleBlur(e); } else { this.handleBlur(e); } } handleFocusEmail = (e) => { this.setState({ isFocus: !this.state.isFocus }); if (this.props.onFocus) { this.props.onFocus(e); } } handleToggle = (e) => { const target = { type: 'toggle', name: this.props.name, value: e.target.id === 'on', } this.props.onChange({ target }); } handleShowPassword = () => this.setState({ showPassword: !this.state.showPassword }); 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}
) } renderInputDate = (requiredClass, inputDescription) => { let spacer = !isEmpty(this.props.inputDescription) ?
:
; if (!this.props.noErrorsDescription && !isEmpty(this.state.errors)) { spacer =
; } const value = isObject(this.props.value) && this.props.value._isAMomentObject === true ? this.props.value : moment(this.props.value); return (
this.props.onChange({ target: { name: this.props.name, value: moment }})} />
{inputDescription}
{this.renderErrors(styles.errorContainerTextArea)} {spacer}
) } renderInputEmail = (requiredClass, inputDescription, handleBlur) => { let spacer = !isEmpty(this.props.inputDescription) ?
:
; if (!this.props.noErrorsDescription && !isEmpty(this.state.errors)) { spacer =
; } return (
{(placeholder) => ( )}
{inputDescription}
{this.renderErrors()} {spacer}
) } renderFormattedInput = (handleBlur, inputValue, placeholder) => ( {(message) => ( )} ) renderInputPassword = (requiredClass, inputDescription, handleBlur) => { let spacer = !isEmpty(this.props.inputDescription) ?
:
; if (!this.props.noErrorsDescription && !isEmpty(this.state.errors)) { spacer =
; } const color = this.state.showPassword ? { color: 'black' } : { color: '#9EA7B8' }; const type = this.state.showPassword ? 'text' : 'password'; return (
{(placeholder) => ( )}
{inputDescription}
{this.renderErrors()} {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}
); } renderInputSearch = (requiredClass, inputDescription, handleBlur) => { let spacer = !isEmpty(this.props.inputDescription) ?
:
; if (!this.props.noErrorsDescription && !isEmpty(this.state.errors)) { spacer =
; } return (
{(placeholder) => ( )}
{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) => (