168 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-07-10 16:34:12 +02:00
/**
*
* InputText
2017-07-11 12:29:43 +02:00
* 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-13 16:13:31 +02:00
* - noErrorsDescription : bool
2017-07-19 09:51:23 +02:00
* prevent from displaying errors messages
2017-07-10 16:34:12 +02:00
*
2017-07-11 12:29:43 +02:00
* Required
* - name : string
* - handleChange : function
* - value : string
2017-07-18 17:52:39 +02:00
* - target : string
2017-07-19 09:51:23 +02:00
* - validations : object
2017-07-11 12:29:43 +02:00
*
* Optionnal
* - description : input description
* - handleFocus : function
* - placeholder : string if set to "" nothing will display
2017-08-01 16:47:05 +02:00
*
* - styles are retrieved from the HOC
2017-07-10 16:34:12 +02:00
*/
import React from 'react';
2017-07-13 16:55:59 +02:00
import { isEmpty, includes, mapKeys, reject, map, isObject } from 'lodash';
import { FormattedMessage } from 'react-intl';
import WithInput from 'components/WithInput';
2017-07-10 16:34:12 +02:00
class InputText extends React.Component { // eslint-disable-line react/prefer-stateless-function
constructor(props) {
super(props);
this.state = {
2017-07-13 16:13:31 +02:00
errors: [],
2017-07-11 12:29:43 +02:00
hasInitialValue: false,
2017-07-10 16:34:12 +02:00
};
}
2017-07-11 12:29:43 +02:00
componentDidMount() {
2017-07-13 15:24:21 +02:00
if (this.props.value && !isEmpty(this.props.value)) {
2017-07-11 12:29:43 +02:00
this.setState({ hasInitialValue: true });
}
}
2017-07-10 16:34:12 +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 16:34:12 +02:00
}
}
handleBlur = ({ target }) => {
2017-07-11 12:29:43 +02:00
// prevent error display if input is initially empty
2017-07-13 17:18:06 +02:00
if (!isEmpty(target.value) || this.state.hasInitialValue) {
2017-07-11 12:29:43 +02:00
// validates basic string validations
// add custom logic here such as alerts...
const errors = this.validate(target.value);
this.setState({ errors, hasInitialValue: true });
2017-07-11 12:29:43 +02:00
}
2017-07-10 16:34:12 +02:00
}
// Basic string validations
validate = (value) => {
let errors = [];
2017-07-13 16:55:59 +02:00
// handle i18n
const requiredError = { id: 'request.error.validation.required' };
2017-07-12 18:12:07 +02:00
mapKeys(this.props.validations, (validationValue, validationKey) => {
switch (validationKey) {
case 'maxLength':
if (value.length > validationValue) {
2017-07-13 16:55:59 +02:00
errors.push({ id: 'request.error.validation.maxLength' });
}
break;
case 'minLength':
if (value.length < validationValue) {
2017-07-13 16:55:59 +02:00
errors.push({ id: 'request.error.validation.minLength' });
}
break;
case 'required':
if (value.length === 0) {
2017-07-13 16:55:59 +02:00
errors.push({ id: 'request.error.validation.required' });
}
break;
case 'regex':
if (!new RegExp(validationValue).test(value)) {
2017-07-13 16:55:59 +02:00
errors.push({ id: 'request.error.validation.regex' });
}
break;
default:
2017-07-13 16:13:31 +02:00
errors = [];
}
2017-07-10 16:34:12 +02:00
});
2017-07-13 16:13:31 +02:00
if (includes(errors, requiredError)) {
2017-07-12 18:12:07 +02:00
errors = reject(errors, (error) => error !== requiredError);
2017-07-10 16:34:12 +02:00
}
return errors;
}
2017-07-13 16:55:59 +02:00
renderErrors = () => { // eslint-disable-line consistent-return
2017-07-13 16:13:31 +02:00
if (!this.props.noErrorsDescription) {
return (
2017-07-13 16:55:59 +02:00
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-13 16:13:31 +02:00
);
}
}
2017-07-10 16:34:12 +02:00
render() {
const inputValue = this.props.value || '';
// override default onBlur
const handleBlur = this.props.handleBlur || this.handleBlur;
// override bootStrapClass
2017-07-11 12:29:43 +02:00
const bootStrapClass = this.props.customBootstrapClass ? this.props.customBootstrapClass : 'col-md-6';
2017-07-10 16:34:12 +02:00
// set error class with override possibility
2017-07-13 16:13:31 +02:00
const bootStrapClassDanger = !this.props.deactivateErrorHighlight && !isEmpty(this.state.errors) ? 'has-danger' : '';
const placeholder = this.props.placeholder || this.props.name;
2017-08-04 17:47:38 +02:00
const label = this.props.name ? <label htmlFor={this.props.name}><FormattedMessage {...{id: this.props.name}} /></label> : '';
2017-07-10 16:34:12 +02:00
return (
2017-08-01 16:47:05 +02:00
<div className={`${this.props.styles.inputText} ${bootStrapClass} ${bootStrapClassDanger}`}>
2017-08-07 14:03:22 +02:00
{label}
2017-07-10 16:34:12 +02:00
<input
2017-07-18 17:52:39 +02:00
name={this.props.target}
id={this.props.name}
2017-07-10 16:34:12 +02:00
onBlur={handleBlur}
2017-07-11 12:29:43 +02:00
onFocus={this.props.handleFocus}
2017-07-10 16:34:12 +02:00
onChange={this.props.handleChange}
value={inputValue}
type="text"
className={`form-control ${this.state.errors? 'form-control-danger' : ''}`}
2017-07-10 17:55:46 +02:00
placeholder={placeholder}
2017-07-10 16:34:12 +02:00
/>
<small>{this.props.inputDescription}</small>
2017-07-13 16:13:31 +02:00
{this.renderErrors()}
2017-07-10 16:34:12 +02:00
</div>
);
}
}
InputText.propTypes = {
customBootstrapClass: React.PropTypes.string,
deactivateErrorHighlight: React.PropTypes.bool,
2017-07-13 16:13:31 +02:00
errors: React.PropTypes.array,
2017-07-10 17:55:46 +02:00
handleBlur: React.PropTypes.func,
2017-07-10 16:34:12 +02:00
handleChange: React.PropTypes.func.isRequired,
2017-07-11 12:29:43 +02:00
handleFocus: React.PropTypes.func,
2017-07-10 16:34:12 +02:00
inputDescription: React.PropTypes.string,
name: React.PropTypes.string.isRequired,
2017-07-13 16:13:31 +02:00
noErrorsDescription: React.PropTypes.bool,
2017-07-10 16:34:12 +02:00
placeholder: React.PropTypes.string,
styles: React.PropTypes.object,
2017-08-04 17:47:38 +02:00
target: React.PropTypes.string,
2017-07-10 16:34:12 +02:00
validations: React.PropTypes.object.isRequired,
2017-08-01 11:26:38 +02:00
value: React.PropTypes.string,
2017-07-10 16:34:12 +02:00
}
export default WithInput(InputText); // eslint-disable-line new-cap