2020-03-23 11:22:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-10-27 14:37:18 +00:00
|
|
|
import { useCallback, useRef, useEffect, useState } from 'react';
|
2020-03-23 11:22:00 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { useValidationContext } from '@woocommerce/base-context';
|
|
|
|
import { ValidationInputError } from '@woocommerce/base-components/validation';
|
2020-03-26 12:39:54 +00:00
|
|
|
import { withInstanceId } from '@woocommerce/base-hocs/with-instance-id';
|
2020-03-23 11:22:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import TextInput from './index';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
const ValidatedTextInput = ( {
|
|
|
|
className,
|
|
|
|
instanceId,
|
|
|
|
id,
|
|
|
|
ariaDescribedBy,
|
|
|
|
errorId,
|
|
|
|
validateOnMount = true,
|
2020-05-06 19:04:00 +00:00
|
|
|
focusOnMount = false,
|
2020-03-23 11:22:00 +00:00
|
|
|
onChange,
|
|
|
|
showError = true,
|
|
|
|
...rest
|
|
|
|
} ) => {
|
2020-10-27 14:37:18 +00:00
|
|
|
const [ isPristine, setIsPristine ] = useState( true );
|
2020-03-23 11:22:00 +00:00
|
|
|
const inputRef = useRef();
|
|
|
|
const {
|
|
|
|
getValidationError,
|
|
|
|
hideValidationError,
|
|
|
|
setValidationErrors,
|
|
|
|
clearValidationError,
|
|
|
|
getValidationErrorId,
|
|
|
|
} = useValidationContext();
|
|
|
|
|
|
|
|
const textInputId = id || 'textinput-' + instanceId;
|
|
|
|
errorId = errorId || textInputId;
|
2020-05-06 10:21:30 +00:00
|
|
|
|
2020-10-27 14:37:18 +00:00
|
|
|
const validateInput = useCallback(
|
|
|
|
( errorsHidden = true ) => {
|
2020-12-17 14:52:44 +00:00
|
|
|
const inputObject = inputRef.current || null;
|
|
|
|
if ( ! inputObject ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Trim white space before validation.
|
|
|
|
inputObject.value = inputObject.value.trim();
|
|
|
|
const inputIsValid = inputObject.checkValidity();
|
|
|
|
if ( inputIsValid ) {
|
2020-10-27 14:37:18 +00:00
|
|
|
clearValidationError( errorId );
|
|
|
|
} else {
|
|
|
|
setValidationErrors( {
|
|
|
|
[ errorId ]: {
|
|
|
|
message:
|
2020-12-17 14:52:44 +00:00
|
|
|
inputObject.validationMessage ||
|
2020-10-27 14:37:18 +00:00
|
|
|
__(
|
|
|
|
'Invalid value.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
hidden: errorsHidden,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[ clearValidationError, errorId, setValidationErrors ]
|
|
|
|
);
|
2020-03-23 11:22:00 +00:00
|
|
|
|
2020-05-06 19:04:00 +00:00
|
|
|
useEffect( () => {
|
2020-10-27 14:37:18 +00:00
|
|
|
if ( isPristine ) {
|
|
|
|
if ( focusOnMount ) {
|
|
|
|
inputRef.current.focus();
|
|
|
|
}
|
|
|
|
setIsPristine( false );
|
2020-05-06 19:04:00 +00:00
|
|
|
}
|
2020-10-27 14:37:18 +00:00
|
|
|
}, [ focusOnMount, isPristine, setIsPristine ] );
|
2020-05-06 19:04:00 +00:00
|
|
|
|
2020-03-23 11:22:00 +00:00
|
|
|
useEffect( () => {
|
2020-10-27 14:37:18 +00:00
|
|
|
if ( isPristine ) {
|
|
|
|
if ( validateOnMount ) {
|
|
|
|
validateInput();
|
|
|
|
}
|
|
|
|
setIsPristine( false );
|
2020-03-23 11:22:00 +00:00
|
|
|
}
|
2020-10-27 14:37:18 +00:00
|
|
|
}, [ isPristine, setIsPristine, validateOnMount, validateInput ] );
|
2020-03-23 11:22:00 +00:00
|
|
|
|
2020-04-02 09:27:54 +00:00
|
|
|
// Remove validation errors when unmounted.
|
|
|
|
useEffect( () => {
|
|
|
|
return () => {
|
|
|
|
clearValidationError( errorId );
|
|
|
|
};
|
2020-10-27 14:37:18 +00:00
|
|
|
}, [ clearValidationError, errorId ] );
|
2020-04-02 09:27:54 +00:00
|
|
|
|
2020-03-23 11:22:00 +00:00
|
|
|
const errorMessage = getValidationError( errorId ) || {};
|
|
|
|
const hasError = errorMessage.message && ! errorMessage.hidden;
|
|
|
|
const describedBy =
|
2020-03-30 12:43:42 +00:00
|
|
|
showError && hasError && getValidationErrorId( errorId )
|
|
|
|
? getValidationErrorId( errorId )
|
2020-03-23 11:22:00 +00:00
|
|
|
: ariaDescribedBy;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TextInput
|
|
|
|
className={ classnames( className, {
|
|
|
|
'has-error': hasError,
|
|
|
|
} ) }
|
|
|
|
id={ textInputId }
|
|
|
|
onBlur={ () => {
|
|
|
|
validateInput( false );
|
|
|
|
} }
|
|
|
|
feedback={
|
|
|
|
showError && <ValidationInputError propertyName={ errorId } />
|
|
|
|
}
|
|
|
|
ref={ inputRef }
|
|
|
|
onChange={ ( val ) => {
|
|
|
|
hideValidationError( errorId );
|
|
|
|
onChange( val );
|
|
|
|
} }
|
|
|
|
ariaDescribedBy={ describedBy }
|
|
|
|
{ ...rest }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ValidatedTextInput.propTypes = {
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
id: PropTypes.string,
|
|
|
|
value: PropTypes.string,
|
|
|
|
ariaDescribedBy: PropTypes.string,
|
|
|
|
errorId: PropTypes.string,
|
|
|
|
validateOnMount: PropTypes.bool,
|
2020-05-06 19:04:00 +00:00
|
|
|
focusOnMount: PropTypes.bool,
|
2020-03-23 11:22:00 +00:00
|
|
|
showError: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withInstanceId( ValidatedTextInput );
|