2019-01-24 15:51:50 +00:00
|
|
|
import React, { Fragment } from 'react';
|
2019-12-12 18:48:17 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-08-19 15:23:05 +00:00
|
|
|
import { Trans } from 'react-i18next';
|
2020-01-10 16:47:49 +00:00
|
|
|
import { createOnBlurHandler } from './helpers';
|
2020-07-03 16:10:05 +00:00
|
|
|
import { R_UNIX_ABSOLUTE_PATH, R_WIN_ABSOLUTE_PATH } from './constants';
|
2019-01-24 15:51:50 +00:00
|
|
|
|
2019-12-12 18:48:17 +00:00
|
|
|
export const renderField = (props, elementType) => {
|
|
|
|
const {
|
2020-09-22 12:04:17 +00:00
|
|
|
input, id, className, placeholder, type, disabled, normalizeOnBlur, onScroll,
|
2020-07-03 13:53:53 +00:00
|
|
|
autoComplete, meta: { touched, error }, min, max, step,
|
2019-12-12 18:48:17 +00:00
|
|
|
} = props;
|
2019-01-24 15:51:50 +00:00
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
const onBlur = (event) => createOnBlurHandler(event, input, normalizeOnBlur);
|
2020-01-10 16:47:49 +00:00
|
|
|
|
2019-12-12 18:48:17 +00:00
|
|
|
const element = React.createElement(elementType, {
|
|
|
|
...input,
|
|
|
|
id,
|
|
|
|
className,
|
|
|
|
placeholder,
|
|
|
|
autoComplete,
|
|
|
|
disabled,
|
|
|
|
type,
|
2020-07-03 13:53:53 +00:00
|
|
|
min,
|
|
|
|
max,
|
|
|
|
step,
|
2020-01-10 16:47:49 +00:00
|
|
|
onBlur,
|
2020-09-22 12:04:17 +00:00
|
|
|
onScroll,
|
2019-12-12 18:48:17 +00:00
|
|
|
});
|
2020-08-19 15:23:05 +00:00
|
|
|
|
2019-12-12 18:48:17 +00:00
|
|
|
return (
|
2020-07-03 13:53:53 +00:00
|
|
|
<>
|
2019-12-12 18:48:17 +00:00
|
|
|
{element}
|
2020-05-22 14:06:05 +00:00
|
|
|
{!disabled && touched && error
|
2020-08-19 15:23:05 +00:00
|
|
|
&& <span className="form__message form__message--error"><Trans>{error}</Trans></span>}
|
2020-07-03 13:53:53 +00:00
|
|
|
</>
|
2019-12-12 18:48:17 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
renderField.propTypes = {
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
input: PropTypes.object.isRequired,
|
|
|
|
className: PropTypes.string,
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
type: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
autoComplete: PropTypes.bool,
|
2020-01-10 16:47:49 +00:00
|
|
|
normalizeOnBlur: PropTypes.func,
|
2020-07-03 13:53:53 +00:00
|
|
|
min: PropTypes.number,
|
|
|
|
max: PropTypes.number,
|
|
|
|
step: PropTypes.number,
|
2020-09-22 12:04:17 +00:00
|
|
|
onScroll: PropTypes.func,
|
2020-05-22 14:06:05 +00:00
|
|
|
meta: PropTypes.shape({
|
|
|
|
touched: PropTypes.bool,
|
2020-08-19 15:23:05 +00:00
|
|
|
error: PropTypes.string,
|
2020-05-22 14:06:05 +00:00
|
|
|
}).isRequired,
|
2019-12-12 18:48:17 +00:00
|
|
|
};
|
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
export const renderTextareaField = (props) => renderField(props, 'textarea');
|
2019-12-12 18:48:17 +00:00
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
export const renderInputField = (props) => renderField(props, 'input');
|
2019-01-24 15:51:50 +00:00
|
|
|
|
2019-11-28 11:47:06 +00:00
|
|
|
export const renderGroupField = ({
|
|
|
|
input,
|
|
|
|
id,
|
|
|
|
className,
|
|
|
|
placeholder,
|
|
|
|
type,
|
|
|
|
disabled,
|
|
|
|
autoComplete,
|
|
|
|
isActionAvailable,
|
|
|
|
removeField,
|
|
|
|
meta: { touched, error },
|
2020-01-10 16:47:49 +00:00
|
|
|
normalizeOnBlur,
|
|
|
|
}) => {
|
2020-05-22 14:06:05 +00:00
|
|
|
const onBlur = (event) => createOnBlurHandler(event, input, normalizeOnBlur);
|
2020-01-10 16:47:49 +00:00
|
|
|
|
|
|
|
return (
|
2020-08-19 15:23:05 +00:00
|
|
|
<>
|
2020-01-10 16:47:49 +00:00
|
|
|
<div className="input-group">
|
|
|
|
<input
|
|
|
|
{...input}
|
|
|
|
id={id}
|
|
|
|
placeholder={placeholder}
|
|
|
|
type={type}
|
|
|
|
className={className}
|
|
|
|
disabled={disabled}
|
|
|
|
autoComplete={autoComplete}
|
|
|
|
onBlur={onBlur}
|
|
|
|
/>
|
2020-05-22 14:06:05 +00:00
|
|
|
{isActionAvailable
|
|
|
|
&& <span className="input-group-append">
|
2020-01-10 16:47:49 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
2020-06-17 21:36:19 +00:00
|
|
|
className="btn btn-secondary btn-icon btn-icon--green"
|
2020-01-10 16:47:49 +00:00
|
|
|
onClick={removeField}
|
|
|
|
>
|
2020-07-17 12:24:39 +00:00
|
|
|
<svg className="icon icon--24">
|
2020-01-10 16:47:49 +00:00
|
|
|
<use xlinkHref="#cross" />
|
|
|
|
</svg>
|
|
|
|
</button>
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
</div>
|
2020-05-22 14:06:05 +00:00
|
|
|
{!disabled && touched && error
|
2020-08-19 15:23:05 +00:00
|
|
|
&& <span className="form__message form__message--error"><Trans>{error}</Trans></span>}
|
|
|
|
</>
|
2020-01-10 16:47:49 +00:00
|
|
|
);
|
|
|
|
};
|
2019-11-28 11:47:06 +00:00
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
renderGroupField.propTypes = {
|
|
|
|
input: PropTypes.object.isRequired,
|
2020-06-11 09:07:46 +00:00
|
|
|
id: PropTypes.string,
|
2020-05-22 14:06:05 +00:00
|
|
|
className: PropTypes.string,
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
type: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
autoComplete: PropTypes.bool,
|
|
|
|
isActionAvailable: PropTypes.bool,
|
|
|
|
removeField: PropTypes.func,
|
|
|
|
meta: PropTypes.shape({
|
|
|
|
touched: PropTypes.bool,
|
2020-08-19 15:23:05 +00:00
|
|
|
error: PropTypes.string,
|
2020-05-22 14:06:05 +00:00
|
|
|
}).isRequired,
|
|
|
|
normalizeOnBlur: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
2019-07-18 11:52:47 +00:00
|
|
|
export const renderRadioField = ({
|
2020-05-25 16:41:04 +00:00
|
|
|
input,
|
|
|
|
placeholder,
|
|
|
|
subtitle,
|
|
|
|
disabled,
|
|
|
|
meta: { touched, error },
|
2020-05-22 14:06:05 +00:00
|
|
|
}) => <Fragment>
|
2020-05-25 16:41:04 +00:00
|
|
|
<label className="custom-control custom-radio">
|
2020-05-22 14:06:05 +00:00
|
|
|
<input {...input} type="radio" className="custom-control-input" disabled={disabled} />
|
|
|
|
<span className="custom-control-label">{placeholder}</span>
|
2020-05-25 16:41:04 +00:00
|
|
|
{subtitle && <span
|
|
|
|
className="checkbox__label-subtitle"
|
|
|
|
dangerouslySetInnerHTML={{ __html: subtitle }}
|
|
|
|
/>}
|
2020-05-22 14:06:05 +00:00
|
|
|
</label>
|
|
|
|
{!disabled
|
|
|
|
&& touched
|
2020-08-19 15:23:05 +00:00
|
|
|
&& error
|
|
|
|
&& <span className="form__message form__message--error"><Trans>{error}</Trans></span>}
|
2020-05-22 14:06:05 +00:00
|
|
|
</Fragment>;
|
|
|
|
|
|
|
|
renderRadioField.propTypes = {
|
|
|
|
input: PropTypes.object.isRequired,
|
|
|
|
placeholder: PropTypes.string,
|
2020-05-25 16:41:04 +00:00
|
|
|
subtitle: PropTypes.string,
|
2020-05-22 14:06:05 +00:00
|
|
|
disabled: PropTypes.bool,
|
|
|
|
meta: PropTypes.shape({
|
|
|
|
touched: PropTypes.bool,
|
2020-08-19 15:23:05 +00:00
|
|
|
error: PropTypes.string,
|
2020-05-22 14:06:05 +00:00
|
|
|
}).isRequired,
|
|
|
|
};
|
2019-07-18 11:52:47 +00:00
|
|
|
|
2020-10-05 11:53:20 +00:00
|
|
|
export const CheckboxField = ({
|
2019-09-12 13:19:35 +00:00
|
|
|
input,
|
|
|
|
placeholder,
|
|
|
|
subtitle,
|
|
|
|
disabled,
|
2020-04-22 16:32:07 +00:00
|
|
|
onClick,
|
2019-09-12 13:19:35 +00:00
|
|
|
modifier = 'checkbox--form',
|
|
|
|
meta: { touched, error },
|
2020-07-06 16:58:44 +00:00
|
|
|
}) => <>
|
2020-05-22 14:06:05 +00:00
|
|
|
<label className={`checkbox ${modifier}`} onClick={onClick}>
|
|
|
|
<span className="checkbox__marker" />
|
2020-10-05 11:53:20 +00:00
|
|
|
<input {...input} type="checkbox" className="checkbox__input" disabled={disabled} />
|
2020-05-22 14:06:05 +00:00
|
|
|
<span className="checkbox__label">
|
2020-10-05 11:53:20 +00:00
|
|
|
<span className="checkbox__label-text checkbox__label-text--long">
|
|
|
|
<span className="checkbox__label-title">{placeholder}</span>
|
|
|
|
{subtitle && <span className="checkbox__label-subtitle">{subtitle}</span>}
|
|
|
|
</span>
|
|
|
|
</span>
|
2020-05-22 14:06:05 +00:00
|
|
|
</label>
|
|
|
|
{!disabled
|
|
|
|
&& touched
|
2020-08-19 15:23:05 +00:00
|
|
|
&& error
|
|
|
|
&& <span className="form__message form__message--error"><Trans>{error}</Trans></span>}
|
2020-07-06 16:58:44 +00:00
|
|
|
</>;
|
2020-05-22 14:06:05 +00:00
|
|
|
|
2020-10-05 11:53:20 +00:00
|
|
|
CheckboxField.propTypes = {
|
2020-05-22 14:06:05 +00:00
|
|
|
input: PropTypes.object.isRequired,
|
|
|
|
placeholder: PropTypes.string,
|
2020-10-12 15:08:22 +00:00
|
|
|
subtitle: PropTypes.node,
|
2020-05-22 14:06:05 +00:00
|
|
|
disabled: PropTypes.bool,
|
|
|
|
onClick: PropTypes.func,
|
|
|
|
modifier: PropTypes.string,
|
2020-07-06 16:58:44 +00:00
|
|
|
checked: PropTypes.bool,
|
2020-05-22 14:06:05 +00:00
|
|
|
meta: PropTypes.shape({
|
|
|
|
touched: PropTypes.bool,
|
2020-08-19 15:23:05 +00:00
|
|
|
error: PropTypes.string,
|
|
|
|
}).isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const renderSelectField = ({
|
|
|
|
input,
|
|
|
|
meta: { touched, error },
|
|
|
|
children,
|
|
|
|
label,
|
|
|
|
}) => {
|
|
|
|
const showWarning = touched && error;
|
|
|
|
|
|
|
|
return <>
|
|
|
|
{label && <label><Trans>{label}</Trans></label>}
|
2020-10-06 08:54:06 +00:00
|
|
|
<select {...input} className='form-control custom-select'>{children}</select>
|
2020-08-19 15:23:05 +00:00
|
|
|
{showWarning
|
|
|
|
&& <span className="form__message form__message--error form__message--left-pad"><Trans>{error}</Trans></span>}
|
|
|
|
</>;
|
|
|
|
};
|
|
|
|
|
|
|
|
renderSelectField.propTypes = {
|
|
|
|
input: PropTypes.object.isRequired,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
label: PropTypes.string,
|
|
|
|
children: PropTypes.oneOfType([PropTypes.array, PropTypes.element]).isRequired,
|
|
|
|
meta: PropTypes.shape({
|
|
|
|
touched: PropTypes.bool,
|
|
|
|
error: PropTypes.string,
|
2020-05-22 14:06:05 +00:00
|
|
|
}).isRequired,
|
|
|
|
};
|
2019-01-24 15:51:50 +00:00
|
|
|
|
2019-07-18 11:52:47 +00:00
|
|
|
export const renderServiceField = ({
|
2019-09-12 13:19:35 +00:00
|
|
|
input,
|
|
|
|
placeholder,
|
|
|
|
disabled,
|
|
|
|
modifier,
|
|
|
|
icon,
|
|
|
|
meta: { touched, error },
|
2020-05-22 14:06:05 +00:00
|
|
|
}) => <Fragment>
|
|
|
|
<label className={`service custom-switch ${modifier}`}>
|
|
|
|
<input
|
|
|
|
{...input}
|
|
|
|
type="checkbox"
|
|
|
|
className="custom-switch-input"
|
|
|
|
value={placeholder.toLowerCase()}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
|
|
|
<span className="service__switch custom-switch-indicator"></span>
|
|
|
|
<span className="service__text">{placeholder}</span>
|
|
|
|
<svg className="service__icon">
|
|
|
|
<use xlinkHref={`#${icon}`} />
|
|
|
|
</svg>
|
|
|
|
</label>
|
|
|
|
{!disabled && touched && error
|
2020-08-19 15:23:05 +00:00
|
|
|
&& <span className="form__message form__message--error"><Trans>{error}</Trans></span>}
|
2020-05-22 14:06:05 +00:00
|
|
|
</Fragment>;
|
|
|
|
|
|
|
|
renderServiceField.propTypes = {
|
|
|
|
input: PropTypes.object.isRequired,
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
modifier: PropTypes.string,
|
|
|
|
icon: PropTypes.string,
|
|
|
|
meta: PropTypes.shape({
|
|
|
|
touched: PropTypes.bool,
|
2020-08-19 15:23:05 +00:00
|
|
|
error: PropTypes.string,
|
2020-05-22 14:06:05 +00:00
|
|
|
}).isRequired,
|
|
|
|
};
|
2019-07-18 11:52:47 +00:00
|
|
|
|
2020-08-19 15:23:05 +00:00
|
|
|
export const getLastIpv4Octet = (ipv4) => parseInt(ipv4.slice(ipv4.lastIndexOf('.') + 1), 10);
|
|
|
|
|
2020-07-03 16:10:05 +00:00
|
|
|
/**
|
|
|
|
* @param value {string}
|
|
|
|
* @returns {*|number}
|
|
|
|
*/
|
|
|
|
export const toNumber = (value) => value && parseInt(value, 10);
|
2020-01-20 12:47:10 +00:00
|
|
|
|
2020-07-03 16:10:05 +00:00
|
|
|
/**
|
|
|
|
* @param value {string}
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2020-05-22 14:06:05 +00:00
|
|
|
export const isValidAbsolutePath = (value) => R_WIN_ABSOLUTE_PATH.test(value)
|
2020-04-06 17:07:32 +00:00
|
|
|
|| R_UNIX_ABSOLUTE_PATH.test(value);
|