157 lines
4.8 KiB
JavaScript
Raw Normal View History

2017-07-10 17:55:46 +02:00
/**
*
* 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
2017-07-19 09:51:23 +02:00
* prevent from displaying errors messages
*
* Required
* - name : string
* - handleChange : function
2017-07-19 09:51:23 +02:00
* - target : string
* - value : string
* - validations : object
*
* Optionnal
* - description : input description
* - handleFocus : function
* - placeholder : string if set to "" nothing will display
2017-07-10 17:55:46 +02:00
*
*/
import React from 'react';
import { isEmpty, includes, map, mapKeys, isObject, reject } from 'lodash';
2017-07-13 16:55:59 +02:00
import { FormattedMessage } from 'react-intl';
import WithInput from 'components/WithInput';
2017-07-10 17:55:46 +02:00
class InputNumber extends React.Component { // eslint-disable-line react/prefer-stateless-function
constructor(props) {
super(props);
this.state = {
2017-07-13 16:55:59 +02:00
errors: [],
hasInitialValue: false,
2017-07-10 17:55:46 +02:00
};
}
componentDidMount() {
2017-07-13 15:24:21 +02:00
if (this.props.value && this.props.value !== '') {
this.setState({ hasInitialValue: true });
}
}
2017-07-10 17:55:46 +02:00
componentWillReceiveProps(nextProps) {
if (this.props.errors !== nextProps.errors) {
2017-07-13 16:55:59 +02:00
this.setState({ errors: nextProps.errors });
2017-07-10 17:55:46 +02:00
}
}
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);
2017-07-11 15:42:54 +02:00
this.setState({ errors, hasInitialValue: true });
}
2017-07-10 17:55:46 +02:00
}
validate = (value) => {
let errors = [];
const requiredError = { id: 'request.error.validation.required' };
mapKeys(this.props.validations, (validationValue, validationKey) => {
switch (validationKey) {
case 'required':
if (value.length === 0) {
errors.push({ id: 'request.error.validation.required' });
}
break;
default:
errors = [];
}
});
if (includes(errors, requiredError)) {
errors = reject(errors, (error) => error !== requiredError);
}
2017-07-10 17:55:46 +02:00
return errors;
}
2017-07-13 16:55:59 +02:00
renderErrors = () => { // eslint-disable-line consistent-return
if (!this.props.noErrorsDescription) {
return (
map(this.state.errors, (error, key) => {
const displayError = isObject(error) && error.id ?
<FormattedMessage {...error} /> : error;
return (
<div key={key} className="form-control-feedback">{displayError}</div>
);
})
);
}
}
2017-07-10 17:55:46 +02:00
render() {
const inputValue = this.props.value || '';
// override default onBlur
const handleBlur = this.props.handleBlur || this.handleBlur;
// override bootStrapClass
const bootStrapClass = this.props.customBootstrapClass ? this.props.customBootstrapClass : 'col-md-4';
2017-07-10 17:55:46 +02:00
// set error class with override possibility
2017-07-13 16:55:59 +02:00
const bootStrapClassDanger = !this.props.deactivateErrorHighlight && !isEmpty(this.state.errors) ? 'has-danger' : '';
const placeholder = this.props.placeholder || this.props.name;
// retrieve styles from WithInput HOC
const styles = this.props.styles;
2017-07-10 17:55:46 +02:00
return (
<div className={`${styles.inputNumber} ${bootStrapClass} ${bootStrapClassDanger}`}>
<label htmlFor={this.props.name}><FormattedMessage {...{id: this.props.name}} /></label>
2017-07-10 17:55:46 +02:00
<input
type="number"
2017-07-19 09:51:23 +02:00
name={this.props.target}
2017-07-11 15:42:54 +02:00
id={this.props.name}
value={inputValue}
2017-07-10 17:55:46 +02:00
onBlur={handleBlur}
onChange={this.props.handleChange}
onFocus={this.props.handleFocus}
className={`form-control ${this.state.errors? 'form-control-danger' : ''}`}
2017-07-10 17:55:46 +02:00
placeholder={placeholder}
/>
<small>{this.props.inputDescription}</small>
2017-07-13 16:55:59 +02:00
{this.renderErrors()}
2017-07-10 17:55:46 +02:00
</div>
);
}
}
InputNumber.propTypes = {
2017-07-11 15:42:54 +02:00
customBootstrapClass: React.PropTypes.string,
deactivateErrorHighlight: React.PropTypes.bool,
2017-07-10 17:55:46 +02:00
errors: React.PropTypes.oneOfType([
React.PropTypes.bool,
React.PropTypes.array,
]),
2017-07-11 15:42:54 +02:00
handleBlur: React.PropTypes.func,
2017-07-10 17:55:46 +02:00
handleChange: React.PropTypes.func.isRequired,
handleFocus: React.PropTypes.func,
2017-07-10 17:55:46 +02:00
inputDescription: React.PropTypes.string,
name: React.PropTypes.string.isRequired,
2017-07-13 16:55:59 +02:00
noErrorsDescription: React.PropTypes.bool,
2017-07-10 17:55:46 +02:00
placeholder: React.PropTypes.string,
styles: React.PropTypes.object,
2017-07-19 09:51:23 +02:00
target: React.PropTypes.string.isRequired,
validations: React.PropTypes.object.isRequired,
2017-07-10 17:55:46 +02:00
value: React.PropTypes.oneOfType([
React.PropTypes.number.isRequired,
React.PropTypes.string.isRequired,
]),
}
export default WithInput(InputNumber); // eslint-disable-line new-cap