Solve conflict

This commit is contained in:
cyril lopez 2018-02-06 18:56:31 +01:00
commit fee239fa7d
6 changed files with 519 additions and 1 deletions

View File

@ -403,6 +403,26 @@ export default Foo;
***
## InputAddon
| Property | Type | Required | Description |
| -------- | ---- | -------- | ----------- |
| addon | string | no | Sets the input's addon. Works with i18n |
| autoFocus | bool | no | Sets the input's autoFocus |
| className | string | no | custom className for the input |
| deactivateErrorHighlight | bool | no | Allow to deactivate the red border on the input when there is an error |
| disabled | bool | no | Disables the input |
| errors | array | no | Sets the red border on the input |
| onBlur | func | no | Function executed when the user leaves the input |
| onFocus | func | no | Function executed when the user enters the input |
| name | string | yes | The name of the input |
| placeholder | string | no | Input's placeholder, works with i18n |
| style | object | no | Input's style property |
| tabIndex | string | no | Input's tabIndex |
| value | string or number | yes | Input's value |
***
## InputDate
Please refer to the [InputText documentation](#InputText);
@ -543,6 +563,43 @@ Input type: 'toggle' component
***
## InputAddonWithErrors
| Property | Type | Required | Description |
| -------- | ---- | -------- | ----------- |
| addon | string | no | Sets the input's addon. Works with i18n |
| autoFocus | bool | no | Sets the input's autoFocus |
| className | string | no | Overrides the container className |
| customBootstrapClass | string | no | Allows to override the input bootstrap col system |
| deactivateErrorHighlight | bool | no | Allow to deactivate the red border on the input when there is an error |
| didCheckErrors | bool | no | Use this props to display errors after submitting a form. |
| disabled | bool | no | Disables the input |
| errors | array | no | Array of errors |
| errorsClassName | string | no | Overrides the InputErrors' className |
| errorsStyle | object | no | Overrides the InputErrors' style |
| inputClassName | string | no | Overrides the InputText's className |
| inputDescriptionClassName | string | no | Overrides the InputDescription's className |
| inputDescriptionStyle | object | no | Overrides the InputDescription's style|
| inputStyle | object | no | Overrides the InputText's style |
| labelClassName | string | no | Overrides the Label's className |
| labelStyle | object | no | Overrides the Label's style |
| onBlur | func | no | Function executed when the user leaves the input |
| onChange | func | yes | Handler to modify the input's value |
| onFocus | func | no | Function executed when the user enters the input |
| name | string | yes | The name of the input |
| noErrorsDescription | bool | no | Remove the input's errors description |
| placeholder | string | no | Input's placeholder, works with i18n |
| style | object | no | Overrides the container style |
| tabIndex | string | no | Input's tabIndex |
| validations | object | no | Input's validations |
| value | string | yes | Input's value |
### Usage
Please refer to the [InputTextWithErrors](#InputTextWithErrors) documentation.
***
## InputDateWithErrors
Please refer to the [InputTextWithErrors](#InputTextWithErrors) documentation.

View File

@ -0,0 +1,122 @@
/**
*
* InputAddon
*/
import React from 'react';
import PropTypes from 'prop-types';
import { isEmpty, isFunction, upperFirst } from 'lodash';
import { FormattedMessage } from 'react-intl';
import cn from 'classnames';
import styles from './styles.scss';
class InputAddon extends React.Component {
state = { isFocused: false };
handleBlur = (e) => {
this.setState({ isFocused: !this.state.isFocused });
if (isFunction(this.props.onBlur)) {
this.props.onBlur(e);
}
}
handleFocus = (e) => {
this.setState({ isFocused: !this.state.isFocused });
this.props.onFocus(e);
}
render() {
const {
addon,
autoFocus,
className,
deactivateErrorHighlight,
disabled,
error,
name,
onChange,
placeholder,
style,
tabIndex,
value,
} = this.props;
return (
<div className={cn(styles.inputAddon, 'input-group', !isEmpty(className) && className)} style={style}>
<FormattedMessage id={addon} defaultMessage={upperFirst(addon)}>
{(message) => (
<span className={cn(
'input-group-addon',
styles.addon,
this.state.isFocused && styles.addonFocus,
!deactivateErrorHighlight && error && styles.errorAddon,
)}
>
{message}
</span>
)}
</FormattedMessage>
<FormattedMessage id={placeholder} defaultMessage={placeholder}>
{(message) => (
<input
autoFocus={autoFocus}
className={cn(
'form-control',
!deactivateErrorHighlight && error && 'is-invalid',
!deactivateErrorHighlight && error && this.state.isFocused && styles.invalidAddon,
)}
disabled={disabled}
id={name}
name={name}
onBlur={this.handleBlur}
onChange={onChange}
onFocus={this.handleFocus}
placeholder={message}
tabIndex={tabIndex}
type="text"
value={value}
/>
)}
</FormattedMessage>
</div>
);
}
}
InputAddon.defaultProps = {
addon: 'app.utils.placeholder.defaultMessage', // Prevent error from FormattedMessage
autoFocus: false,
className: '',
deactivateErrorHighlight: false,
disabled: false,
error: false,
onBlur: () => {},
onFocus: () => {},
placeholder: 'app.utils.placeholder.defaultMessage',
style: {},
tabIndex: '0',
};
InputAddon.propTypes = {
addon: PropTypes.string,
autoFocus: PropTypes.bool,
className: PropTypes.string,
deactivateErrorHighlight: PropTypes.bool,
disabled: PropTypes.bool,
error: PropTypes.bool,
onBlur: PropTypes.oneOfType([
PropTypes.func,
PropTypes.bool,
]),
onChange: PropTypes.func.isRequired,
onFocus: PropTypes.func,
name: PropTypes.string.isRequired,
placeholder: PropTypes.string,
style: PropTypes.object,
tabIndex: PropTypes.string,
value: PropTypes.string.isRequired,
};
export default InputAddon;

View File

@ -0,0 +1,58 @@
.addon {
width: 5.9rem;
height: 3.4rem;
margin-top: .9rem;
background-color: rgba(16, 22, 34, 0.02);
border: 1px solid #E3E9F3;
border-right: 0;
border-radius: 0.25rem;
color: rgba(16, 22, 34, 0.5);
line-height: 3.2rem;
font-size: 1.3rem;
font-family: 'Lato';
font-weight: 600!important;
-moz-appearance: none;
-webkit-appearance: none;
}
.errorAddon {
border: 1px solid #ff203c!important;
border-right: none!important;
}
.addonFocus {
border-color: #78caff;
border-right: 0;
}
.invalidAddon {
border-color: #ff203c !important;
border-left: 0;
}
.inputAddon {
min-width: 200px;
margin-bottom: 1rem;
font-size: 1.3rem;
> input {
height: 3.4rem;
margin-top: .9rem;
padding-left: 1rem;
background-size: 0 !important;
border: 1px solid #E3E9F3;
border-left: 0;
border-radius: 0.25rem;
line-height: 3.4rem;
font-size: 1.3rem;
font-family: 'Lato' !important;
box-shadow: 0px 1px 1px rgba(104, 118, 142, 0.05);
&:focus {
border-color: #78caff;
}
}
& + span {
border-color: #E3E9F3;
}
}

View File

@ -0,0 +1,258 @@
/**
*
* InputAddonWithErrors
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import { includes, isEmpty, isFunction, mapKeys, reject } from 'lodash';
import cn from 'classnames';
// Design
import Label from 'components/Label';
import InputDescription from 'components/InputDescription';
import InputErrors from 'components/InputErrors';
import InputAddon from 'components/InputAddon';
import styles from './styles.scss';
class InputAddonWithErrors extends React.Component { // eslint-disable-line react/prefer-stateless-function
state = { errors: [], hasInitialValue: false };
componentDidMount() {
const { value, errors } = this.props;
// Prevent the input from displaying an error when the user enters and leaves without filling it
if (value && !isEmpty(value)) {
this.setState({ hasInitialValue: true });
}
// Display input error if it already has some
if (!isEmpty(errors)) {
this.setState({ errors });
}
}
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 });
}
}
/**
* Set the errors depending on the validations given to the input
* @param {Object} target
*/
handleBlur = ({ target }) => {
// Prevent from displaying error if the input is initially isEmpty
if (!isEmpty(target.value) || this.state.hasInitialValue) {
const errors = this.validate(target.value);
this.setState({ errors, hasInitialValue: true });
}
}
render() {
const {
addon,
autoFocus,
className,
customBootstrapClass,
deactivateErrorHighlight,
disabled,
errorsClassName,
errorsStyle,
inputClassName,
inputDescription,
inputDescriptionClassName,
inputDescriptionStyle,
inputStyle,
label,
labelClassName,
labelStyle,
name,
noErrorsDescription,
onBlur,
onChange,
onFocus,
placeholder,
style,
tabIndex,
value,
} = this.props;
const handleBlur = isFunction(onBlur) ? onBlur : this.handleBlur;
let spacer = !isEmpty(inputDescription) ? <div className={styles.spacer} /> : <div />;
if (!noErrorsDescription && !isEmpty(this.state.errors)) {
spacer = <div />;
}
return (
<div className={cn(
styles.containerAddon,
customBootstrapClass,
!isEmpty(className) && className,
)}
style={style}
>
<Label
className={labelClassName}
htmlFor={name}
message={label}
style={labelStyle}
/>
<InputAddon
addon={addon}
autoFocus={autoFocus}
className={inputClassName}
disabled={disabled}
deactivateErrorHighlight={deactivateErrorHighlight}
error={!isEmpty(this.state.errors)}
name={name}
onBlur={handleBlur}
onChange={onChange}
onFocus={onFocus}
placeholder={placeholder}
style={inputStyle}
tabIndex={tabIndex}
value={value}
/>
<InputDescription
className={inputDescriptionClassName}
message={inputDescription}
style={inputDescriptionStyle}
/>
<InputErrors
className={errorsClassName}
errors={!noErrorsDescription && this.state.errors || []}
style={errorsStyle}
/>
{spacer}
</div>
);
}
validate = (value) => {
const requiredError = { id: 'components.Input.error.validation.required' };
let errors = [];
mapKeys(this.props.validations, (validationValue, validationKey) => {
switch (validationKey) {
case 'maxLength': {
if (value.length > validationValue) {
errors.push({ id: 'components.Input.error.validation.maxLength' });
}
break;
}
case 'minLength': {
if (value.length < validationValue) {
errors.push({ id: 'components.Input.error.validation.minLength' });
}
break;
}
case 'required': {
if (value.length === 0) {
errors.push({ id: 'components.Input.error.validation.required' });
}
break;
}
case 'regex': {
if (!new RegExp(validationValue).test(value)) {
errors.push({ id: 'components.Input.error.validation.regex' });
}
break;
}
default:
errors = [];
}
});
if (includes(errors, requiredError)) {
errors = reject(errors, (error) => error !== requiredError);
}
return errors;
}
}
InputAddonWithErrors.defaultProps = {
addon: 'app.utils.placeholder.defaultMessage',
autoFocus: false,
className: '',
customBootstrapClass: 'col-md-6',
deactivateErrorHighlight: false,
didCheckErrors: false,
disabled: false,
onBlur: false,
onFocus: () => {},
errors: [],
errorsClassName: '',
errorsStyle: {},
inputClassName: '',
inputDescription: '',
inputDescriptionClassName: '',
inputDescriptionStyle: {},
inputStyle: {},
label: '',
labelClassName: '',
labelStyle: {},
noErrorsDescription: false,
placeholder: 'app.utils.placeholder.defaultMessage',
style: {},
tabIndex: '0',
validations: {},
};
InputAddonWithErrors.propTypes = {
addon: PropTypes.string,
autoFocus: PropTypes.bool,
className: PropTypes.string,
customBootstrapClass: PropTypes.string,
deactivateErrorHighlight: PropTypes.bool,
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,
}),
]),
labelClassName: PropTypes.string,
labelStyle: PropTypes.object,
name: PropTypes.string.isRequired,
noErrorsDescription: PropTypes.bool,
onBlur: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.func,
]),
onChange: PropTypes.func.isRequired,
onFocus: PropTypes.func,
placeholder: PropTypes.string,
style: PropTypes.object,
tabIndex: PropTypes.string,
validations: PropTypes.object,
value: PropTypes.string.isRequired,
};
export default InputAddonWithErrors;

View File

@ -0,0 +1,9 @@
.containerAddon {
min-width: 200px;
margin-bottom: 1.5rem;
font-size: 1.3rem;
}
.spacer {
height: .5rem;
}

View File

@ -5,8 +5,10 @@
import React from 'react';
import PropTypes from 'prop-types';
import { isEmpty } from 'lodash';
// Design
import InputAddonWithErrors from 'components/InputAddonWithErrors';
import InputCheckboxWithErrors from 'components/InputCheckboxWithErrors';
import InputDateWithErrors from 'components/InputDateWithErrors';
import InputEmailWithErrors from 'components/InputEmailWithErrors';
@ -21,6 +23,7 @@ import InputToggleWithErrors from 'components/InputToggleWithErrors';
const DefaultInputError = ({ type }) => <div>Your input type: <b>{type}</b> does not exist</div>
const inputs = {
addon: InputAddonWithErrors,
checkbox: InputCheckboxWithErrors,
date: InputDateWithErrors,
email: InputEmailWithErrors,
@ -35,18 +38,29 @@ const inputs = {
};
function InputsIndex(props) {
const Input = inputs[props.type] ? inputs[props.type] : DefaultInputError;
const inputValue = props.type === 'checkbox' || props.type === 'toggle' ? props.value || false : props.value || '';
const type = props.type && !isEmpty(props.addon) ? 'addon' : props.type;
const Input = inputs[type] ? inputs[type] : DefaultInputError;
return <Input {...props} value={inputValue} />;
}
InputsIndex.defaultProps = {
addon: false,
}
InputsIndex.propTypes = {
addon: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
]),
type: PropTypes.string.isRequired,
};
export default InputsIndex;
export {
InputAddonWithErrors,
InputCheckboxWithErrors,
InputDateWithErrors,
InputEmailWithErrors,