/**
*
* InputNumber
* Customization
* - deactivateErrorHighlight: bool
* allow the user to remove bootstrap class 'has-danger' on the inputText
* - customBootstrapClass : string
* overrides the default 'col-md-6' on the inputText
* - handleBlur: function
* overrides the default input validations
* - errors : array
* prevent from displaying errors messages
*
* Required
* - name : string
* - handleChange : function
* - target : string
* - value : string
* - validations : object
*
* Optionnal
* - description : input description
* - handleFocus : function
* - placeholder : string if set to "" nothing will display
*
*
* - styles are retrieved from the HOC
*/
import React from 'react';
import { isEmpty, includes, map, mapKeys, isObject, reject } from 'lodash';
import { FormattedMessage } from 'react-intl';
import WithInput from 'components/WithInput';
class InputNumber extends React.Component { // eslint-disable-line react/prefer-stateless-function
constructor(props) {
super(props);
this.state = {
errors: [],
hasInitialValue: false,
};
}
componentDidMount() {
if (this.props.value && this.props.value !== '') {
this.setState({ hasInitialValue: true });
}
}
componentWillReceiveProps(nextProps) {
if (this.props.errors !== nextProps.errors) {
this.setState({ errors: nextProps.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 = [];
const requiredError = { id: 'settings-manager.request.error.validation.required' };
mapKeys(this.props.validations, (validationValue, validationKey) => {
switch (validationKey) {
case 'required':
if (value.length === 0) {
errors.push({ id: 'settings-manager.request.error.validation.required' });
}
break;
default:
errors = [];
}
});
if (includes(errors, requiredError)) {
errors = reject(errors, (error) => error !== requiredError);
}
return errors;
}
renderErrors = () => { // eslint-disable-line consistent-return
if (!this.props.noErrorsDescription) {
return (
map(this.state.errors, (error, key) => {
const displayError = isObject(error) && error.id ?