2019-01-24 15:51:50 +00:00
|
|
|
import React, { Fragment } from 'react';
|
|
|
|
import { Trans } from 'react-i18next';
|
|
|
|
|
2019-07-22 12:32:12 +00:00
|
|
|
import { R_IPV4, R_MAC, R_HOST, R_IPV6, UNSAFE_PORTS } from '../helpers/constants';
|
2019-01-24 15:51:50 +00:00
|
|
|
|
|
|
|
export const renderField = ({
|
2019-07-22 12:32:12 +00:00
|
|
|
input,
|
|
|
|
id,
|
|
|
|
className,
|
|
|
|
placeholder,
|
|
|
|
type,
|
|
|
|
disabled,
|
|
|
|
meta: { touched, error },
|
2019-01-24 15:51:50 +00:00
|
|
|
}) => (
|
|
|
|
<Fragment>
|
|
|
|
<input
|
|
|
|
{...input}
|
|
|
|
id={id}
|
|
|
|
placeholder={placeholder}
|
|
|
|
type={type}
|
|
|
|
className={className}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
2019-07-22 12:32:12 +00:00
|
|
|
{!disabled &&
|
|
|
|
touched &&
|
|
|
|
(error && <span className="form__message form__message--error">{error}</span>)}
|
2019-01-24 15:51:50 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
export const renderSelectField = ({
|
|
|
|
input, placeholder, disabled, meta: { touched, error },
|
|
|
|
}) => (
|
|
|
|
<Fragment>
|
|
|
|
<label className="checkbox checkbox--form">
|
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>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</label>
|
2019-07-22 12:32:12 +00:00
|
|
|
{!disabled &&
|
|
|
|
touched &&
|
|
|
|
(error && <span className="form__message form__message--error">{error}</span>)}
|
2019-01-24 15:51:50 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
export const required = (value) => {
|
|
|
|
if (value || value === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return <Trans>form_error_required</Trans>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ipv4 = (value) => {
|
|
|
|
if (value && !new RegExp(R_IPV4).test(value)) {
|
|
|
|
return <Trans>form_error_ip_format</Trans>;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-05-22 14:59:57 +00:00
|
|
|
export const mac = (value) => {
|
|
|
|
if (value && !new RegExp(R_MAC).test(value)) {
|
|
|
|
return <Trans>form_error_mac_format</Trans>;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
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>;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
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>;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-07-05 11:54:52 +00:00
|
|
|
export const portTLS = (value) => {
|
|
|
|
if (value === 0) {
|
|
|
|
return false;
|
|
|
|
} else if (value && (value < 80 || value > 65535)) {
|
|
|
|
return <Trans>form_error_port_range</Trans>;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-02-19 15:56:13 +00:00
|
|
|
export const isSafePort = (value) => {
|
|
|
|
if (UNSAFE_PORTS.includes(value)) {
|
|
|
|
return <Trans>form_error_port_unsafe</Trans>;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-07-22 12:32:12 +00:00
|
|
|
export const domain = (value) => {
|
|
|
|
if (value && !new RegExp(R_HOST).test(value)) {
|
|
|
|
return <Trans>form_error_domain_format</Trans>;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const answer = (value) => {
|
|
|
|
if (
|
|
|
|
value &&
|
|
|
|
(!new RegExp(R_IPV4).test(value) &&
|
|
|
|
!new RegExp(R_IPV6).test(value) &&
|
|
|
|
!new RegExp(R_HOST).test(value))
|
|
|
|
) {
|
|
|
|
return <Trans>form_error_answer_format</Trans>;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-01-24 15:51:50 +00:00
|
|
|
export const toNumber = value => value && parseInt(value, 10);
|