116 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-01-03 10:53:58 +01:00
/**
*
* Inputs
*/
import React from 'react';
import PropTypes from 'prop-types';
2020-01-10 15:17:36 +01:00
import { FormattedMessage } from 'react-intl';
2020-01-03 10:53:58 +01:00
import { ErrorMessage, Label } from '@buffetjs/styles';
2020-01-15 12:17:44 +01:00
import { Error } from '@buffetjs/core';
2020-01-03 10:53:58 +01:00
import HeadersInput from '../HeadersInput';
2020-01-06 01:52:26 +01:00
import EventInput from '../EventInput';
2020-01-10 15:17:36 +01:00
import Wrapper from './Wrapper';
2020-01-03 10:53:58 +01:00
function Inputs({
error: inputError,
label,
name,
onChange,
2020-01-10 15:17:36 +01:00
onBlur,
2020-01-03 10:53:58 +01:00
onClick,
2020-01-06 16:46:30 +01:00
onRemove,
2020-01-03 10:53:58 +01:00
type,
validations,
value,
}) {
return (
2020-01-10 15:17:36 +01:00
<Wrapper>
<Label htmlFor={name}>{label}</Label>
{type === 'headers' ? (
<>
<HeadersInput
errors={inputError}
name={name}
onBlur={onBlur}
onClick={onClick}
onChange={onChange}
onRemove={onRemove}
value={value}
/>
{inputError && (
<ErrorMessage>
<FormattedMessage id="components.Input.error.validation.required" />
</ErrorMessage>
)}
</>
) : (
<Error
inputError={inputError}
name={name}
type="text"
validations={validations}
>
{({ canCheck, error, dispatch }) => {
const hasError = error && error !== null;
2020-01-03 10:53:58 +01:00
2020-01-10 15:17:36 +01:00
const handleChange = e => {
if (!canCheck) {
dispatch({
type: 'SET_CHECK',
});
}
dispatch({
type: 'SET_ERROR',
error: null,
});
2020-01-06 16:46:30 +01:00
2020-01-10 15:17:36 +01:00
onChange(e);
};
2020-01-09 20:25:16 +01:00
2020-01-10 15:17:36 +01:00
return (
<>
2020-01-15 12:17:44 +01:00
<EventInput
name={name}
onChange={e => {
handleChange(e);
onBlur(e);
}}
value={value}
/>
{hasError && <ErrorMessage>{error}</ErrorMessage>}
2020-01-10 15:17:36 +01:00
</>
);
}}
</Error>
)}
</Wrapper>
2020-01-03 10:53:58 +01:00
);
}
Inputs.defaultProps = {
error: null,
label: '',
onClick: () => {},
2020-01-06 16:46:30 +01:00
onRemove: () => {},
2020-01-03 10:53:58 +01:00
type: 'text',
validations: {},
2020-01-03 15:54:53 +01:00
value: null,
2020-01-03 10:53:58 +01:00
};
Inputs.propTypes = {
2020-01-09 20:25:16 +01:00
error: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
2020-01-10 15:17:36 +01:00
label: PropTypes.string,
2020-01-03 10:53:58 +01:00
name: PropTypes.string.isRequired,
2020-01-08 18:28:52 +01:00
onBlur: PropTypes.func.isRequired,
2020-01-03 10:53:58 +01:00
onChange: PropTypes.func.isRequired,
onClick: PropTypes.func,
2020-01-06 16:46:30 +01:00
onRemove: PropTypes.func,
2020-01-03 10:53:58 +01:00
type: PropTypes.string,
validations: PropTypes.object,
2020-01-10 15:17:36 +01:00
value: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
2020-01-03 10:53:58 +01:00
};
export default Inputs;