mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-29 10:28:53 +03:00
- client: add wrapper for input normalization on blur
Close #1314 * commit '600781bb8096e814ad0671af28be94061453c3a8': - client: add wrapper for input normalization on blur
This commit is contained in:
commit
2d4a73bbcf
3 changed files with 61 additions and 34 deletions
|
@ -78,7 +78,7 @@ const renderFieldsWrapper = (placeholder, buttonTitle) =>
|
||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
isActionAvailable={index !== 0}
|
isActionAvailable={index !== 0}
|
||||||
removeField={() => fields.remove(index)}
|
removeField={() => fields.remove(index)}
|
||||||
normalize={data => data && data.trim()}
|
normalizeOnBlur={data => data.trim()}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
@ -127,7 +127,7 @@ let Form = (props) => {
|
||||||
type="text"
|
type="text"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('form_client_name')}
|
placeholder={t('form_client_name')}
|
||||||
normalize={data => data && data.trim()}
|
normalizeOnBlur={data => data.trim()}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,16 @@ import React, { Fragment } from 'react';
|
||||||
import { Trans } from 'react-i18next';
|
import { Trans } from 'react-i18next';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { R_IPV4, R_MAC, R_HOST, R_IPV6, R_CIDR, UNSAFE_PORTS } from '../helpers/constants';
|
import { R_IPV4, R_MAC, R_HOST, R_IPV6, R_CIDR, UNSAFE_PORTS } from '../helpers/constants';
|
||||||
|
import { createOnBlurHandler } from './helpers';
|
||||||
|
|
||||||
export const renderField = (props, elementType) => {
|
export const renderField = (props, elementType) => {
|
||||||
const {
|
const {
|
||||||
input, id, className, placeholder, type, disabled,
|
input, id, className, placeholder, type, disabled, normalizeOnBlur,
|
||||||
autoComplete, meta: { touched, error },
|
autoComplete, meta: { touched, error },
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
|
const onBlur = event => createOnBlurHandler(event, input, normalizeOnBlur);
|
||||||
|
|
||||||
const element = React.createElement(elementType, {
|
const element = React.createElement(elementType, {
|
||||||
...input,
|
...input,
|
||||||
id,
|
id,
|
||||||
|
@ -17,6 +20,7 @@ export const renderField = (props, elementType) => {
|
||||||
autoComplete,
|
autoComplete,
|
||||||
disabled,
|
disabled,
|
||||||
type,
|
type,
|
||||||
|
onBlur,
|
||||||
});
|
});
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
|
@ -35,6 +39,7 @@ renderField.propTypes = {
|
||||||
type: PropTypes.string,
|
type: PropTypes.string,
|
||||||
disabled: PropTypes.bool,
|
disabled: PropTypes.bool,
|
||||||
autoComplete: PropTypes.bool,
|
autoComplete: PropTypes.bool,
|
||||||
|
normalizeOnBlur: PropTypes.func,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const renderTextareaField = props => renderField(props, 'textarea');
|
export const renderTextareaField = props => renderField(props, 'textarea');
|
||||||
|
@ -52,7 +57,11 @@ export const renderGroupField = ({
|
||||||
isActionAvailable,
|
isActionAvailable,
|
||||||
removeField,
|
removeField,
|
||||||
meta: { touched, error },
|
meta: { touched, error },
|
||||||
}) => (
|
normalizeOnBlur,
|
||||||
|
}) => {
|
||||||
|
const onBlur = event => createOnBlurHandler(event, input, normalizeOnBlur);
|
||||||
|
|
||||||
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
<div className="input-group">
|
<div className="input-group">
|
||||||
<input
|
<input
|
||||||
|
@ -63,6 +72,7 @@ export const renderGroupField = ({
|
||||||
className={className}
|
className={className}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
autoComplete={autoComplete}
|
autoComplete={autoComplete}
|
||||||
|
onBlur={onBlur}
|
||||||
/>
|
/>
|
||||||
{isActionAvailable &&
|
{isActionAvailable &&
|
||||||
<span className="input-group-append">
|
<span className="input-group-append">
|
||||||
|
@ -83,6 +93,7 @@ export const renderGroupField = ({
|
||||||
(error && <span className="form__message form__message--error">{error}</span>)}
|
(error && <span className="form__message form__message--error">{error}</span>)}
|
||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const renderRadioField = ({
|
export const renderRadioField = ({
|
||||||
input, placeholder, disabled, meta: { touched, error },
|
input, placeholder, disabled, meta: { touched, error },
|
||||||
|
|
|
@ -373,3 +373,19 @@ export const getParamsForClientsSearch = (data, param) => {
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates onBlur handler that can normalize input if normalization function is specified
|
||||||
|
*
|
||||||
|
* @param {Object} event
|
||||||
|
* @param {Object} event.target
|
||||||
|
* @param {string} event.target.value
|
||||||
|
* @param {Object} input
|
||||||
|
* @param {function} input.onBlur
|
||||||
|
* @param {function} [normalizeOnBlur]
|
||||||
|
* @returns {function}
|
||||||
|
*/
|
||||||
|
export const createOnBlurHandler = (event, input, normalizeOnBlur) => (
|
||||||
|
normalizeOnBlur
|
||||||
|
? input.onBlur(normalizeOnBlur(event.target.value))
|
||||||
|
: input.onBlur());
|
||||||
|
|
Loading…
Reference in a new issue