205 lines
4.9 KiB
JavaScript
Raw Normal View History

/**
*
* InputCheckboxWithErrors
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
2018-02-15 15:46:25 +01:00
import { isEmpty, isObject, isFunction } from 'lodash';
import cn from 'classnames';
// Design
2019-02-22 10:22:21 +01:00
import InputDescription from '../InputDescription';
import InputErrors from '../InputErrors';
import InputCheckbox from '../InputCheckbox';
import InputSpacer from '../InputSpacer';
2019-10-09 15:44:56 +02:00
import Container from './Container';
class InputCheckboxWithErrors extends React.Component {
state = { errors: [] };
componentDidMount() {
// Display input error if it already has some
const { errors } = this.props;
if (!isEmpty(errors)) {
this.setState({ errors });
}
}
2019-08-13 11:31:10 +02:00
UNSAFE_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 });
}
}
2019-08-13 11:31:10 +02:00
render() {
const {
autoFocus,
className,
customBootstrapClass,
disabled,
errorsClassName,
errorsStyle,
inputClassName,
inputDescription,
inputDescriptionClassName,
inputDescriptionStyle,
inputStyle,
label,
name,
noErrorsDescription,
onBlur,
onChange,
onFocus,
placeholder,
style,
tabIndex,
title,
value,
} = this.props;
const handleBlur = onBlur || (() => {});
let inputTitle = '';
2018-02-15 15:46:25 +01:00
let spacer = !isEmpty(inputDescription) ? <InputSpacer /> : <div />;
if (!noErrorsDescription && !isEmpty(this.state.errors)) {
spacer = <div />;
}
if (isObject(title) && title.id) {
inputTitle = (
2019-10-09 15:44:56 +02:00
<div className="inputTitle">
2019-08-13 11:31:10 +02:00
<FormattedMessage
id={title.id}
defaultMessage={title.id}
values={title.params}
/>
</div>
);
}
if (isFunction(title)) {
inputTitle = title();
}
return (
2019-10-09 15:44:56 +02:00
<Container
className={cn(customBootstrapClass, !isEmpty(className) && className)}
style={style}
>
{inputTitle}
<InputCheckbox
autoFocus={autoFocus}
className={inputClassName}
disabled={disabled}
label={label}
name={name}
onBlur={handleBlur}
onChange={onChange}
onFocus={onFocus}
placeholder={placeholder}
style={inputStyle}
tabIndex={tabIndex}
value={value}
/>
<InputDescription
2019-08-13 11:31:10 +02:00
className={cn(
2019-10-09 15:44:56 +02:00
'inputCheckboxDescriptionContainer',
2019-08-13 11:31:10 +02:00
inputDescriptionClassName
)}
message={this.props.inputDescription}
style={inputDescriptionStyle}
/>
<InputErrors
className={errorsClassName}
errors={this.state.errors}
2018-11-21 12:57:20 +01:00
name={name}
style={errorsStyle}
/>
2018-03-14 09:59:42 +01:00
{spacer}
2019-10-09 15:44:56 +02:00
</Container>
);
}
}
InputCheckboxWithErrors.defaultProps = {
autoFocus: false,
className: '',
customBootstrapClass: 'col-md-3',
didCheckErrors: false,
disabled: false,
onBlur: () => {},
onFocus: () => {},
errors: [],
errorsClassName: '',
errorsStyle: {},
inputClassName: '',
inputDescription: '',
inputDescriptionClassName: '',
inputDescriptionStyle: {},
inputStyle: {},
label: '',
noErrorsDescription: false,
placeholder: 'app.utils.placeholder.defaultMessage',
style: {},
tabIndex: '0',
title: '',
value: false,
};
InputCheckboxWithErrors.propTypes = {
autoFocus: PropTypes.bool,
className: PropTypes.string,
customBootstrapClass: PropTypes.string,
didCheckErrors: PropTypes.bool,
disabled: PropTypes.bool,
errors: PropTypes.array,
errorsClassName: PropTypes.string,
errorsStyle: PropTypes.object,
inputClassName: PropTypes.string,
inputDescription: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.shape({
id: PropTypes.string,
params: PropTypes.object,
}),
]),
inputDescriptionClassName: PropTypes.string,
inputDescriptionStyle: PropTypes.object,
inputStyle: PropTypes.object,
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.shape({
id: PropTypes.string,
params: PropTypes.object,
}),
]),
name: PropTypes.string.isRequired,
noErrorsDescription: PropTypes.bool,
2019-08-13 11:31:10 +02:00
onBlur: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
onChange: PropTypes.func.isRequired,
onFocus: PropTypes.func,
placeholder: PropTypes.string,
style: PropTypes.object,
tabIndex: PropTypes.string,
title: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.shape({
id: PropTypes.string,
params: PropTypes.object,
}),
]),
2019-08-13 11:31:10 +02:00
value: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
};
export default InputCheckboxWithErrors;