2018-02-01 17:04:08 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { includes, isEmpty, isFunction, mapKeys, reject } from 'lodash';
|
|
|
|
import cn from 'classnames';
|
|
|
|
|
|
|
|
// Design
|
|
|
|
import Label from 'components/Label';
|
|
|
|
import InputDescription from 'components/InputDescription';
|
|
|
|
import InputErrors from 'components/InputErrors';
|
|
|
|
import InputNumber from 'components/InputNumber';
|
|
|
|
|
|
|
|
import styles from './styles.scss';
|
|
|
|
|
|
|
|
class InputNumberWithErrors extends React.Component { // eslint-disable-line react/prefer-stateless-function
|
|
|
|
state = { errors: [], hasInitialValue: false };
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { value, errors } = this.props;
|
|
|
|
|
|
|
|
// Prevent the input from displaying an error when the user enters and leaves without filling it
|
|
|
|
if (value && !isEmpty(value)) {
|
|
|
|
this.setState({ hasInitialValue: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display input error if it already has some
|
|
|
|
if (!isEmpty(errors)) {
|
|
|
|
this.setState({ errors });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
// Check if errors have been updated during validations
|
|
|
|
if (nextProps.didCheckErrors !== this.props.didCheckErrors) {
|
|
|
|
// Remove from the state the errors that have already been set
|
|
|
|
const errors = isEmpty(nextProps.errors) ? [] : nextProps.errors;
|
|
|
|
this.setState({ errors });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the errors depending on the validations given to the input
|
|
|
|
* @param {Object} target
|
|
|
|
*/
|
|
|
|
handleBlur = ({ target }) => {
|
|
|
|
// Prevent from displaying error if the input is initially isEmpty
|
|
|
|
if (!isEmpty(target.value) || this.state.hasInitialValue) {
|
|
|
|
const errors = this.validate(target.value);
|
|
|
|
this.setState({ errors, hasInitialValue: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-02-01 18:47:51 +01:00
|
|
|
const {
|
|
|
|
autoFocus,
|
|
|
|
deactivateErrorHighlight,
|
|
|
|
disabled,
|
|
|
|
errorsClassName,
|
|
|
|
errorsStyle,
|
|
|
|
inputClassName,
|
|
|
|
inputDescriptionClassName,
|
|
|
|
inputDescriptionStyle,
|
|
|
|
inputStyle,
|
|
|
|
labelClassName,
|
|
|
|
labelStyle,
|
|
|
|
name,
|
|
|
|
onChange,
|
|
|
|
onFocus,
|
|
|
|
placeholder,
|
|
|
|
style,
|
|
|
|
tabIndex,
|
|
|
|
value,
|
|
|
|
} = this.props;
|
2018-02-01 17:04:08 +01:00
|
|
|
const handleBlur = isFunction(this.props.onBlur) ? this.props.onBlur : this.handleBlur;
|
2018-02-01 18:47:51 +01:00
|
|
|
|
2018-02-01 17:04:08 +01:00
|
|
|
return (
|
|
|
|
<div className={cn(
|
|
|
|
styles.container,
|
|
|
|
this.props.customBootstrapClass || 'col-md-6',
|
2018-02-01 18:47:51 +01:00
|
|
|
!isEmpty(this.props.className) && props.className,
|
2018-02-01 17:04:08 +01:00
|
|
|
)}
|
2018-02-01 18:47:51 +01:00
|
|
|
style={style}
|
2018-02-01 17:04:08 +01:00
|
|
|
>
|
2018-02-01 18:47:51 +01:00
|
|
|
<Label
|
|
|
|
className={labelClassName}
|
|
|
|
htmlFor={name}
|
|
|
|
message={this.props.label && this.props.label.message || this.props.label}
|
|
|
|
style={labelStyle}
|
|
|
|
/>
|
2018-02-02 12:12:25 +01:00
|
|
|
<InputNumber
|
2018-02-01 17:04:08 +01:00
|
|
|
autoFocus={autoFocus}
|
|
|
|
className={inputClassName}
|
2018-02-01 18:47:51 +01:00
|
|
|
disabled={disabled}
|
|
|
|
deactivateErrorHighlight={deactivateErrorHighlight}
|
|
|
|
error={!isEmpty(this.state.errors)}
|
2018-02-01 17:04:08 +01:00
|
|
|
name={name}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
onChange={onChange}
|
|
|
|
onFocus={onFocus}
|
|
|
|
placeholder={placeholder}
|
|
|
|
style={inputStyle}
|
2018-02-01 18:47:51 +01:00
|
|
|
tabIndex={tabIndex}
|
2018-02-01 17:04:08 +01:00
|
|
|
value={value}
|
|
|
|
/>
|
2018-02-01 18:47:51 +01:00
|
|
|
<InputDescription
|
|
|
|
className={inputDescriptionClassName}
|
|
|
|
message={this.props.inputDescription && this.props.inputDescription.message || this.props.inputDescription}
|
|
|
|
style={inputDescriptionStyle}
|
|
|
|
/>
|
|
|
|
<InputErrors
|
|
|
|
className={errorsClassName}
|
|
|
|
errors={this.state.errors}
|
|
|
|
style={errorsStyle}
|
|
|
|
/>
|
2018-02-01 17:04:08 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
validate = (value) => {
|
|
|
|
const requiredError = { id: 'components.Input.error.validation.required' };
|
|
|
|
let errors = [];
|
|
|
|
|
|
|
|
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 'min': {
|
|
|
|
if (parseInt(value, 10) < validationValue) {
|
|
|
|
errors.push({ id: 'components.Input.error.validation.min' });
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'required': {
|
|
|
|
if (value.length === 0) {
|
|
|
|
console.log('ok');
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InputNumberWithErrors.defaultProps = {
|
2018-02-01 18:47:51 +01:00
|
|
|
autoFocus: false,
|
|
|
|
className: '',
|
2018-02-01 17:04:08 +01:00
|
|
|
customBootstrapClass: false,
|
2018-02-01 18:47:51 +01:00
|
|
|
deactivateErrorHighlight: false,
|
2018-02-01 17:04:08 +01:00
|
|
|
didCheckErrors: false,
|
2018-02-01 18:47:51 +01:00
|
|
|
disabled: false,
|
2018-02-01 17:04:08 +01:00
|
|
|
onBlur: false,
|
|
|
|
onFocus: () => {},
|
|
|
|
errors: [],
|
|
|
|
errorsClassName: '',
|
|
|
|
errorsStyle: {},
|
|
|
|
inputClassName: '',
|
2018-02-01 18:47:51 +01:00
|
|
|
inputDescription: '',
|
|
|
|
inputDescriptionClassName: '',
|
|
|
|
inputDescriptionStyle: {},
|
2018-02-01 17:04:08 +01:00
|
|
|
inputStyle: {},
|
2018-02-01 18:47:51 +01:00
|
|
|
labelClassName: '',
|
|
|
|
labelStyle: {},
|
2018-02-01 17:04:08 +01:00
|
|
|
placeholder: 'app.utils.placeholder.defaultMessage',
|
2018-02-01 18:47:51 +01:00
|
|
|
style: {},
|
|
|
|
tabIndex: '0',
|
2018-02-01 17:04:08 +01:00
|
|
|
validations: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
InputNumberWithErrors.propTypes = {
|
2018-02-01 18:47:51 +01:00
|
|
|
autoFocus: PropTypes.bool,
|
|
|
|
className: PropTypes.string,
|
2018-02-01 17:04:08 +01:00
|
|
|
customBootstrapClass: PropTypes.oneOfType([
|
|
|
|
PropTypes.bool,
|
|
|
|
PropTypes.string,
|
|
|
|
]),
|
2018-02-01 18:47:51 +01:00
|
|
|
deactivateErrorHighlight: PropTypes.bool,
|
2018-02-01 17:04:08 +01:00
|
|
|
didCheckErrors: PropTypes.bool,
|
2018-02-01 18:47:51 +01:00
|
|
|
disabled: PropTypes.bool,
|
2018-02-01 17:04:08 +01:00
|
|
|
errors: PropTypes.array,
|
|
|
|
errorsClassName: PropTypes.string,
|
|
|
|
errorsStyle: PropTypes.object,
|
|
|
|
inputClassName: PropTypes.string,
|
2018-02-01 18:47:51 +01:00
|
|
|
inputDescription: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.func,
|
|
|
|
PropTypes.object,
|
|
|
|
]),
|
|
|
|
inputDescriptionClassName: PropTypes.string,
|
|
|
|
inputDescriptionStyle: PropTypes.object,
|
2018-02-01 17:04:08 +01:00
|
|
|
inputStyle: PropTypes.object,
|
2018-02-01 18:47:51 +01:00
|
|
|
labelClassName: PropTypes.string,
|
|
|
|
labelStyle: PropTypes.object,
|
2018-02-01 17:04:08 +01:00
|
|
|
onBlur: PropTypes.oneOfType([
|
|
|
|
PropTypes.bool,
|
|
|
|
PropTypes.func,
|
|
|
|
]),
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
onFocus: PropTypes.func,
|
2018-02-01 18:47:51 +01:00
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
style: PropTypes.object,
|
|
|
|
tabIndex: PropTypes.string,
|
2018-02-01 17:04:08 +01:00
|
|
|
validations: PropTypes.object,
|
2018-02-01 18:47:51 +01:00
|
|
|
value: PropTypes.string.isRequired,
|
2018-02-01 17:04:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default InputNumberWithErrors;
|