import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { Trans } from 'react-i18next'; import { createOnBlurHandler } from './helpers'; import { R_UNIX_ABSOLUTE_PATH, R_WIN_ABSOLUTE_PATH } from './constants'; export const renderField = (props, elementType) => { const { input, id, className, placeholder, type, disabled, normalizeOnBlur, onScroll, autoComplete, meta: { touched, error }, min, max, step, } = props; const onBlur = (event) => createOnBlurHandler(event, input, normalizeOnBlur); const element = React.createElement(elementType, { ...input, id, className, placeholder, autoComplete, disabled, type, min, max, step, onBlur, onScroll, }); 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, min: PropTypes.number, max: PropTypes.number, step: PropTypes.number, onScroll: PropTypes.func, meta: PropTypes.shape({ touched: PropTypes.bool, error: PropTypes.string, }).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, 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.string, }).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.string, }).isRequired, }; export const CheckboxField = ({ input, placeholder, subtitle, disabled, onClick, modifier = 'checkbox--form', meta: { touched, error }, }) => <> {!disabled && touched && error && {error}} ; CheckboxField.propTypes = { input: PropTypes.object.isRequired, placeholder: PropTypes.string, subtitle: PropTypes.node, disabled: PropTypes.bool, onClick: PropTypes.func, modifier: PropTypes.string, checked: PropTypes.bool, meta: PropTypes.shape({ touched: PropTypes.bool, error: PropTypes.string, }).isRequired, }; export const renderSelectField = ({ input, meta: { touched, error }, children, label, }) => { const showWarning = touched && error; return <> {label && } {showWarning && {error}} ; }; 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, }).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.string, }).isRequired, }; export const getLastIpv4Octet = (ipv4) => parseInt(ipv4.slice(ipv4.lastIndexOf('.') + 1), 10); /** * @param value {string} * @returns {*|number} */ export const toNumber = (value) => value && parseInt(value, 10); /** * @param value {string} * @returns {boolean} */ export const isValidAbsolutePath = (value) => R_WIN_ABSOLUTE_PATH.test(value) || R_UNIX_ABSOLUTE_PATH.test(value);