import React, { Fragment } from 'react'; import { Trans } from 'react-i18next'; import PropTypes from 'prop-types'; import { R_IPV4, R_MAC, R_HOST, R_IPV6, R_CIDR, R_CIDR_IPV6, UNSAFE_PORTS, R_URL_REQUIRES_PROTOCOL, R_WIN_ABSOLUTE_PATH, R_UNIX_ABSOLUTE_PATH, } from './constants'; import { createOnBlurHandler } from './helpers'; export const renderField = (props, elementType) => { const { input, id, className, placeholder, type, disabled, normalizeOnBlur, autoComplete, meta: { touched, error }, } = props; const onBlur = (event) => createOnBlurHandler(event, input, normalizeOnBlur); const element = React.createElement(elementType, { ...input, id, className, placeholder, autoComplete, disabled, type, onBlur, }); return ( {element} {!disabled && touched && error && {error}} ); }; 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, normalizeOnBlur: PropTypes.func, meta: PropTypes.shape({ touched: PropTypes.bool, error: PropTypes.object, }).isRequired, }; export const renderTextareaField = (props) => renderField(props, 'textarea'); export const renderInputField = (props) => renderField(props, 'input'); export const renderGroupField = ({ input, id, className, placeholder, type, disabled, autoComplete, isActionAvailable, removeField, meta: { touched, error }, normalizeOnBlur, }) => { const onBlur = (event) => createOnBlurHandler(event, input, normalizeOnBlur); return (
{isActionAvailable && }
{!disabled && touched && error && {error}}
); }; renderGroupField.propTypes = { input: PropTypes.object.isRequired, id: PropTypes.string.isRequired, 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, error: PropTypes.object, }).isRequired, normalizeOnBlur: PropTypes.func, }; export const renderRadioField = ({ input, placeholder, subtitle, disabled, meta: { touched, error }, }) => {!disabled && touched && (error && {error})} ; renderRadioField.propTypes = { input: PropTypes.object.isRequired, placeholder: PropTypes.string, subtitle: PropTypes.string, disabled: PropTypes.bool, meta: PropTypes.shape({ touched: PropTypes.bool, error: PropTypes.object, }).isRequired, }; export const renderSelectField = ({ input, placeholder, subtitle, disabled, onClick, modifier = 'checkbox--form', meta: { touched, error }, }) => {!disabled && touched && (error && {error})} ; renderSelectField.propTypes = { input: PropTypes.object.isRequired, placeholder: PropTypes.string, subtitle: PropTypes.string, disabled: PropTypes.bool, onClick: PropTypes.func, modifier: PropTypes.string, meta: PropTypes.shape({ touched: PropTypes.bool, error: PropTypes.object, }).isRequired, }; export const renderServiceField = ({ input, placeholder, disabled, modifier, icon, meta: { touched, error }, }) => {!disabled && touched && error && {error}} ; renderServiceField.propTypes = { input: PropTypes.object.isRequired, placeholder: PropTypes.string, disabled: PropTypes.bool, modifier: PropTypes.string, icon: PropTypes.string, meta: PropTypes.shape({ touched: PropTypes.bool, error: PropTypes.object, }).isRequired, }; // Validation functions // If the value is valid, the validation function should return undefined. // https://redux-form.com/6.6.3/examples/fieldlevelvalidation/ export const required = (value) => { const formattedValue = typeof value === 'string' ? value.trim() : value; if (formattedValue || formattedValue === 0 || (formattedValue && formattedValue.length !== 0)) { return undefined; } return form_error_required; }; export const ipv4 = (value) => { if (value && !R_IPV4.test(value)) { return form_error_ip4_format; } return undefined; }; export const clientId = (value) => { if (!value) { return undefined; } const formattedValue = value ? value.trim() : value; if (formattedValue && !( R_IPV4.test(formattedValue) || R_IPV6.test(formattedValue) || R_MAC.test(formattedValue) || R_CIDR.test(formattedValue) || R_CIDR_IPV6.test(formattedValue) )) { return form_error_client_id_format; } return undefined; }; export const ipv6 = (value) => { if (value && !R_IPV6.test(value)) { return form_error_ip6_format; } return undefined; }; export const ip = (value) => { if (value && !R_IPV4.test(value) && !R_IPV6.test(value)) { return form_error_ip_format; } return undefined; }; export const mac = (value) => { if (value && !R_MAC.test(value)) { return form_error_mac_format; } return undefined; }; export const isPositive = (value) => { if ((value || value === 0) && value <= 0) { return form_error_positive; } return undefined; }; export const biggerOrEqualZero = (value) => { if (value < 0) { return form_error_negative; } return false; }; export const port = (value) => { if ((value || value === 0) && (value < 80 || value > 65535)) { return form_error_port_range; } return undefined; }; export const validInstallPort = (value) => { if (value < 1 || value > 65535) { return form_error_port; } return undefined; }; export const portTLS = (value) => { if (value === 0) { return undefined; } if (value && (value < 80 || value > 65535)) { return form_error_port_range; } return undefined; }; export const isSafePort = (value) => { if (UNSAFE_PORTS.includes(value)) { return form_error_port_unsafe; } return undefined; }; export const domain = (value) => { if (value && !R_HOST.test(value)) { return form_error_domain_format; } return undefined; }; export const answer = (value) => { if (value && (!R_IPV4.test(value) && !R_IPV6.test(value) && !R_HOST.test(value))) { return form_error_answer_format; } return undefined; }; export const isValidUrl = (value) => { if (value && !R_URL_REQUIRES_PROTOCOL.test(value)) { return form_error_url_format; } return undefined; }; export const isValidAbsolutePath = (value) => R_WIN_ABSOLUTE_PATH.test(value) || R_UNIX_ABSOLUTE_PATH.test(value); export const isValidPath = (value) => { if (value && !isValidAbsolutePath(value) && !R_URL_REQUIRES_PROTOCOL.test(value)) { return form_error_url_or_path_format; } return undefined; }; export const toNumber = (value) => value && parseInt(value, 10);