2019-01-24 15:51:50 +00:00
|
|
|
import React, { Fragment } from 'react';
|
|
|
|
import { Trans } from 'react-i18next';
|
2019-12-12 18:48:17 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-03-21 17:43:45 +00:00
|
|
|
import { R_IPV4, R_MAC, R_HOST, R_IPV6, R_CIDR, R_CIDR_IPV6, UNSAFE_PORTS, R_URL_REQUIRES_PROTOCOL } from '../helpers/constants';
|
2020-01-10 16:47:49 +00:00
|
|
|
import { createOnBlurHandler } from './helpers';
|
2019-01-24 15:51:50 +00:00
|
|
|
|
2019-12-12 18:48:17 +00:00
|
|
|
export const renderField = (props, elementType) => {
|
|
|
|
const {
|
2020-01-10 16:47:49 +00:00
|
|
|
input, id, className, placeholder, type, disabled, normalizeOnBlur,
|
2019-12-12 18:48:17 +00:00
|
|
|
autoComplete, meta: { touched, error },
|
|
|
|
} = props;
|
2019-01-24 15:51:50 +00:00
|
|
|
|
2020-01-10 16:47:49 +00:00
|
|
|
const onBlur = event => createOnBlurHandler(event, input, normalizeOnBlur);
|
|
|
|
|
2019-12-12 18:48:17 +00:00
|
|
|
const element = React.createElement(elementType, {
|
|
|
|
...input,
|
|
|
|
id,
|
|
|
|
className,
|
|
|
|
placeholder,
|
|
|
|
autoComplete,
|
|
|
|
disabled,
|
|
|
|
type,
|
2020-01-10 16:47:49 +00:00
|
|
|
onBlur,
|
2019-12-12 18:48:17 +00:00
|
|
|
});
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{element}
|
|
|
|
{!disabled && touched && (error && <span className="form__message form__message--error">{error}</span>)}
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
renderField.propTypes = {
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
input: PropTypes.object.isRequired,
|
|
|
|
meta: 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,
|
2019-12-12 18:48:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const renderTextareaField = props => renderField(props, 'textarea');
|
|
|
|
|
|
|
|
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,
|
|
|
|
}) => {
|
|
|
|
const onBlur = event => createOnBlurHandler(event, input, normalizeOnBlur);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<div className="input-group">
|
|
|
|
<input
|
|
|
|
{...input}
|
|
|
|
id={id}
|
|
|
|
placeholder={placeholder}
|
|
|
|
type={type}
|
|
|
|
className={className}
|
|
|
|
disabled={disabled}
|
|
|
|
autoComplete={autoComplete}
|
|
|
|
onBlur={onBlur}
|
|
|
|
/>
|
|
|
|
{isActionAvailable &&
|
|
|
|
<span className="input-group-append">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="btn btn-secondary btn-icon"
|
|
|
|
onClick={removeField}
|
|
|
|
>
|
|
|
|
<svg className="icon icon--close">
|
|
|
|
<use xlinkHref="#cross" />
|
|
|
|
</svg>
|
|
|
|
</button>
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
{!disabled &&
|
|
|
|
touched &&
|
|
|
|
(error && <span className="form__message form__message--error">{error}</span>)}
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
};
|
2019-11-28 11:47:06 +00:00
|
|
|
|
2019-07-18 11:52:47 +00:00
|
|
|
export const renderRadioField = ({
|
|
|
|
input, placeholder, disabled, meta: { touched, error },
|
|
|
|
}) => (
|
|
|
|
<Fragment>
|
|
|
|
<label className="custom-control custom-radio custom-control-inline">
|
2019-09-12 13:19:35 +00:00
|
|
|
<input {...input} type="radio" className="custom-control-input" disabled={disabled} />
|
2019-07-18 11:52:47 +00:00
|
|
|
<span className="custom-control-label">{placeholder}</span>
|
|
|
|
</label>
|
2019-09-12 13:19:35 +00:00
|
|
|
{!disabled &&
|
2019-12-12 18:48:17 +00:00
|
|
|
touched &&
|
|
|
|
(error && <span className="form__message form__message--error">{error}</span>)}
|
2019-07-18 11:52:47 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
2019-01-24 15:51:50 +00:00
|
|
|
export const renderSelectField = ({
|
2019-09-12 13:19:35 +00:00
|
|
|
input,
|
|
|
|
placeholder,
|
|
|
|
subtitle,
|
|
|
|
disabled,
|
|
|
|
modifier = 'checkbox--form',
|
|
|
|
meta: { touched, error },
|
2019-01-24 15:51:50 +00:00
|
|
|
}) => (
|
|
|
|
<Fragment>
|
2019-09-12 13:19:35 +00:00
|
|
|
<label className={`checkbox ${modifier}`}>
|
2019-07-22 12:32:12 +00:00
|
|
|
<span className="checkbox__marker" />
|
|
|
|
<input {...input} type="checkbox" className="checkbox__input" disabled={disabled} />
|
2019-01-24 15:51:50 +00:00
|
|
|
<span className="checkbox__label">
|
2019-03-06 11:45:21 +00:00
|
|
|
<span className="checkbox__label-text checkbox__label-text--long">
|
2019-01-24 15:51:50 +00:00
|
|
|
<span className="checkbox__label-title">{placeholder}</span>
|
2019-09-12 13:19:35 +00:00
|
|
|
{subtitle && (
|
|
|
|
<span
|
|
|
|
className="checkbox__label-subtitle"
|
|
|
|
dangerouslySetInnerHTML={{ __html: subtitle }}
|
|
|
|
/>
|
|
|
|
)}
|
2019-01-24 15:51:50 +00:00
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</label>
|
2019-07-22 12:32:12 +00:00
|
|
|
{!disabled &&
|
2019-12-12 18:48:17 +00:00
|
|
|
touched &&
|
|
|
|
(error && <span className="form__message form__message--error">{error}</span>)}
|
2019-01-24 15:51:50 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
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 },
|
2019-07-18 11:52:47 +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>
|
2019-09-12 13:19:35 +00:00
|
|
|
{!disabled &&
|
2019-12-12 18:48:17 +00:00
|
|
|
touched &&
|
|
|
|
(error && <span className="form__message form__message--error">{error}</span>)}
|
2019-07-18 11:52:47 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
2019-11-28 11:47:06 +00:00
|
|
|
// Validation functions
|
2019-12-12 18:48:17 +00:00
|
|
|
// If the value is valid, the validation function should return undefined.
|
|
|
|
// https://redux-form.com/6.6.3/examples/fieldlevelvalidation/
|
2019-01-24 15:51:50 +00:00
|
|
|
export const required = (value) => {
|
2019-12-12 18:48:17 +00:00
|
|
|
const formattedValue = typeof value === 'string' ? value.trim() : value;
|
|
|
|
if (formattedValue || formattedValue === 0 || (formattedValue && formattedValue.length !== 0)) {
|
|
|
|
return undefined;
|
2019-01-24 15:51:50 +00:00
|
|
|
}
|
|
|
|
return <Trans>form_error_required</Trans>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ipv4 = (value) => {
|
2019-12-12 18:48:17 +00:00
|
|
|
if (value && !R_IPV4.test(value)) {
|
2019-11-27 15:45:30 +00:00
|
|
|
return <Trans>form_error_ip4_format</Trans>;
|
|
|
|
}
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const clientId = (value) => {
|
|
|
|
if (!value) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const formattedValue = value ? value.trim() : value;
|
2020-03-21 17:43:45 +00:00
|
|
|
if (formattedValue && !(
|
|
|
|
R_IPV4.test(formattedValue)
|
|
|
|
|| R_IPV6.test(formattedValue)
|
|
|
|
|| R_MAC.test(formattedValue)
|
|
|
|
|| R_CIDR.test(formattedValue)
|
|
|
|
|| R_CIDR_IPV6.test(formattedValue)
|
|
|
|
)) {
|
2019-12-12 18:48:17 +00:00
|
|
|
return <Trans>form_error_client_id_format</Trans>;
|
|
|
|
}
|
|
|
|
return undefined;
|
2019-11-27 15:45:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const ipv6 = (value) => {
|
2019-12-12 18:48:17 +00:00
|
|
|
if (value && !R_IPV6.test(value)) {
|
2019-11-27 15:45:30 +00:00
|
|
|
return <Trans>form_error_ip6_format</Trans>;
|
|
|
|
}
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
2019-11-27 15:45:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const ip = (value) => {
|
2019-12-12 18:48:17 +00:00
|
|
|
if (value && !R_IPV4.test(value) && !R_IPV6.test(value)) {
|
2019-01-24 15:51:50 +00:00
|
|
|
return <Trans>form_error_ip_format</Trans>;
|
|
|
|
}
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
2019-01-24 15:51:50 +00:00
|
|
|
};
|
|
|
|
|
2019-05-22 14:59:57 +00:00
|
|
|
export const mac = (value) => {
|
2019-12-12 18:48:17 +00:00
|
|
|
if (value && !R_MAC.test(value)) {
|
2019-05-22 14:59:57 +00:00
|
|
|
return <Trans>form_error_mac_format</Trans>;
|
|
|
|
}
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
2019-05-22 14:59:57 +00:00
|
|
|
};
|
|
|
|
|
2019-01-24 15:51:50 +00:00
|
|
|
export const isPositive = (value) => {
|
2019-07-22 12:32:12 +00:00
|
|
|
if ((value || value === 0) && value <= 0) {
|
2019-01-24 15:51:50 +00:00
|
|
|
return <Trans>form_error_positive</Trans>;
|
|
|
|
}
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
2019-01-24 15:51:50 +00:00
|
|
|
};
|
|
|
|
|
2019-12-05 13:00:20 +00:00
|
|
|
export const biggerOrEqualZero = (value) => {
|
|
|
|
if (value < 0) {
|
|
|
|
return <Trans>form_error_negative</Trans>;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-01-24 15:51:50 +00:00
|
|
|
export const port = (value) => {
|
2019-02-21 16:16:09 +00:00
|
|
|
if ((value || value === 0) && (value < 80 || value > 65535)) {
|
2019-01-24 15:51:50 +00:00
|
|
|
return <Trans>form_error_port_range</Trans>;
|
|
|
|
}
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
2020-02-13 15:42:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const validInstallPort = (value) => {
|
|
|
|
if (value < 1 || value > 65535) {
|
|
|
|
return <Trans>form_error_port</Trans>;
|
|
|
|
}
|
|
|
|
return undefined;
|
2019-01-24 15:51:50 +00:00
|
|
|
};
|
|
|
|
|
2019-07-05 11:54:52 +00:00
|
|
|
export const portTLS = (value) => {
|
|
|
|
if (value === 0) {
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
2019-07-05 11:54:52 +00:00
|
|
|
} else if (value && (value < 80 || value > 65535)) {
|
|
|
|
return <Trans>form_error_port_range</Trans>;
|
|
|
|
}
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
2019-07-05 11:54:52 +00:00
|
|
|
};
|
|
|
|
|
2019-02-19 15:56:13 +00:00
|
|
|
export const isSafePort = (value) => {
|
|
|
|
if (UNSAFE_PORTS.includes(value)) {
|
|
|
|
return <Trans>form_error_port_unsafe</Trans>;
|
|
|
|
}
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
2019-02-19 15:56:13 +00:00
|
|
|
};
|
|
|
|
|
2019-07-22 12:32:12 +00:00
|
|
|
export const domain = (value) => {
|
2019-12-12 18:48:17 +00:00
|
|
|
if (value && !R_HOST.test(value)) {
|
2019-07-22 12:32:12 +00:00
|
|
|
return <Trans>form_error_domain_format</Trans>;
|
|
|
|
}
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
2019-07-22 12:32:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const answer = (value) => {
|
2019-12-12 18:48:17 +00:00
|
|
|
if (value && (!R_IPV4.test(value) && !R_IPV6.test(value) && !R_HOST.test(value))) {
|
2019-07-22 12:32:12 +00:00
|
|
|
return <Trans>form_error_answer_format</Trans>;
|
|
|
|
}
|
2019-12-12 18:48:17 +00:00
|
|
|
return undefined;
|
2019-07-22 12:32:12 +00:00
|
|
|
};
|
|
|
|
|
2020-01-20 12:47:10 +00:00
|
|
|
export const isValidUrl = (value) => {
|
|
|
|
if (value && !R_URL_REQUIRES_PROTOCOL.test(value)) {
|
|
|
|
return <Trans>form_error_url_format</Trans>;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
2019-01-24 15:51:50 +00:00
|
|
|
export const toNumber = value => value && parseInt(value, 10);
|