Update lib input

This commit is contained in:
cyril lopez 2017-09-19 13:51:40 +02:00
parent 9255f8098e
commit 72bcfd23f7

View File

@ -6,6 +6,7 @@
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';
@ -67,37 +68,37 @@ class Input extends React.Component { // eslint-disable-line react/prefer-statel
validate = (value) => {
let errors = [];
// handle i18n
const requiredError = { id: 'error.validation.required' };
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: 'error.validation.max' });
errors.push({ id: `${this.props.pluginID}.error.validation.max` });
}
break;
case 'maxLength':
if (value.length > validationValue) {
errors.push({ id: 'error.validation.maxLength' });
errors.push({ id: `${this.props.pluginID}.error.validation.maxLength` });
}
break;
case 'min':
if (parseInt(value, 10) < validationValue) {
errors.push({ id: 'error.validation.min' });
errors.push({ id: `${this.props.pluginID}.error.validation.min` });
}
break;
case 'minLength':
if (value.length < validationValue) {
errors.push({ id: 'error.validation.minLength' });
errors.push({ id: `${this.props.pluginID}.error.validation.minLength` });
}
break;
case 'required':
if (value.length === 0) {
errors.push({ id: 'error.validation.required' });
errors.push({ id: `${this.props.pluginID}.error.validation.required` });
}
break;
case 'regex':
if (!new RegExp(validationValue).test(value)) {
errors.push({ id: 'error.validation.regex' });
errors.push({ id: `${this.props.pluginID}.error.validation.regex` });
}
break;
default:
@ -367,36 +368,37 @@ class Input extends React.Component { // eslint-disable-line react/prefer-statel
}
Input.propTypes = {
addon: React.PropTypes.oneOfType([
React.PropTypes.bool,
React.PropTypes.string,
addon: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
]),
addRequiredInputDesign: React.PropTypes.bool,
customBootstrapClass: React.PropTypes.string,
deactivateErrorHighlight: React.PropTypes.bool,
didCheckErrors: React.PropTypes.bool,
disabled: React.PropTypes.bool,
errors: React.PropTypes.array,
handleBlur: React.PropTypes.oneOfType([
React.PropTypes.func,
React.PropTypes.bool,
addRequiredInputDesign: PropTypes.bool,
customBootstrapClass: PropTypes.string,
deactivateErrorHighlight: PropTypes.bool,
didCheckErrors: PropTypes.bool,
disabled: PropTypes.bool,
errors: PropTypes.array,
handleBlur: PropTypes.oneOfType([
PropTypes.func,
PropTypes.bool,
]),
handleChange: React.PropTypes.func.isRequired,
handleFocus: React.PropTypes.func,
inputDescription: React.PropTypes.string,
label: React.PropTypes.string.isRequired,
name: React.PropTypes.string.isRequired,
noErrorsDescription: React.PropTypes.bool,
placeholder: React.PropTypes.string,
selectOptions: React.PropTypes.array,
selectOptionsFetchSucceeded: React.PropTypes.bool,
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,
handleChange: PropTypes.func.isRequired,
handleFocus: PropTypes.func,
inputDescription: PropTypes.string,
label: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
noErrorsDescription: PropTypes.bool,
placeholder: PropTypes.string,
pluginID: PropTypes.string,
selectOptions: PropTypes.array,
selectOptionsFetchSucceeded: PropTypes.bool,
title: PropTypes.string,
type: PropTypes.string.isRequired,
validations: PropTypes.object.isRequired,
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
PropTypes.number,
]),
};