2020-03-23 11:22:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-03-02 14:26:00 +00:00
|
|
|
import { useRef, useEffect, useState, useCallback } from '@wordpress/element';
|
2020-03-23 11:22:00 +00:00
|
|
|
import classnames from 'classnames';
|
2021-08-05 09:26:00 +00:00
|
|
|
import { withInstanceId } from '@wordpress/compose';
|
2022-12-06 13:13:21 +00:00
|
|
|
import { isObject } from '@woocommerce/types';
|
2022-11-10 10:05:41 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2022-07-01 23:06:25 +00:00
|
|
|
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
|
2022-12-06 13:13:21 +00:00
|
|
|
import { usePrevious } from '@woocommerce/base-hooks';
|
2020-03-23 11:22:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-01-19 15:55:44 +00:00
|
|
|
import TextInput from './text-input';
|
2020-03-23 11:22:00 +00:00
|
|
|
import './style.scss';
|
2022-07-01 23:06:25 +00:00
|
|
|
import { ValidationInputError } from '../validation-input-error';
|
2023-01-13 15:54:35 +00:00
|
|
|
import { getValidityMessageForInput } from '../../utils';
|
2023-08-03 11:02:20 +00:00
|
|
|
import { ValidatedTextInputProps } from './types';
|
2020-03-23 11:22:00 +00:00
|
|
|
|
2023-08-03 11:02:20 +00:00
|
|
|
/**
|
|
|
|
* A text based input which validates the input value.
|
|
|
|
*/
|
2020-03-23 11:22:00 +00:00
|
|
|
const ValidatedTextInput = ( {
|
|
|
|
className,
|
|
|
|
instanceId,
|
|
|
|
id,
|
|
|
|
ariaDescribedBy,
|
|
|
|
errorId,
|
2020-05-06 19:04:00 +00:00
|
|
|
focusOnMount = false,
|
2020-03-23 11:22:00 +00:00
|
|
|
onChange,
|
|
|
|
showError = true,
|
2021-06-04 08:44:26 +00:00
|
|
|
errorMessage: passedErrorMessage = '',
|
2021-12-20 12:16:41 +00:00
|
|
|
value = '',
|
2023-08-03 11:02:20 +00:00
|
|
|
customValidation = () => true,
|
|
|
|
customFormatter = ( newValue: string ) => newValue,
|
2023-01-13 15:54:35 +00:00
|
|
|
label,
|
2023-04-11 08:50:59 +00:00
|
|
|
validateOnMount = true,
|
2020-03-23 11:22:00 +00:00
|
|
|
...rest
|
2022-07-01 23:06:25 +00:00
|
|
|
}: ValidatedTextInputProps ): JSX.Element => {
|
2023-08-03 11:02:20 +00:00
|
|
|
// True on mount.
|
2020-10-27 14:37:18 +00:00
|
|
|
const [ isPristine, setIsPristine ] = useState( true );
|
2023-08-03 11:02:20 +00:00
|
|
|
|
|
|
|
// Track incoming value.
|
2022-12-06 13:13:21 +00:00
|
|
|
const previousValue = usePrevious( value );
|
2023-08-03 11:02:20 +00:00
|
|
|
|
|
|
|
// Ref for the input element.
|
|
|
|
const inputRef = useRef< HTMLInputElement >( null );
|
|
|
|
|
2021-05-19 09:55:15 +00:00
|
|
|
const textInputId =
|
|
|
|
typeof id !== 'undefined' ? id : 'textinput-' + instanceId;
|
|
|
|
const errorIdString = errorId !== undefined ? errorId : textInputId;
|
2020-05-06 10:21:30 +00:00
|
|
|
|
2022-12-06 13:13:21 +00:00
|
|
|
const { setValidationErrors, hideValidationError, clearValidationError } =
|
|
|
|
useDispatch( VALIDATION_STORE_KEY );
|
|
|
|
|
2022-09-22 09:54:02 +00:00
|
|
|
const { validationError, validationErrorId } = useSelect( ( select ) => {
|
|
|
|
const store = select( VALIDATION_STORE_KEY );
|
|
|
|
return {
|
|
|
|
validationError: store.getValidationError( errorIdString ),
|
|
|
|
validationErrorId: store.getValidationErrorId( errorIdString ),
|
|
|
|
};
|
|
|
|
} );
|
2022-07-01 23:06:25 +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;
|
2022-12-06 13:13:21 +00:00
|
|
|
|
|
|
|
if ( inputObject === null ) {
|
2020-12-17 14:52:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-06 13:13:21 +00:00
|
|
|
|
2020-12-17 14:52:44 +00:00
|
|
|
// Trim white space before validation.
|
|
|
|
inputObject.value = inputObject.value.trim();
|
2022-12-06 13:13:21 +00:00
|
|
|
inputObject.setCustomValidity( '' );
|
|
|
|
|
2023-08-03 11:02:20 +00:00
|
|
|
if (
|
|
|
|
inputObject.checkValidity() &&
|
|
|
|
customValidation( inputObject )
|
|
|
|
) {
|
2021-05-19 09:55:15 +00:00
|
|
|
clearValidationError( errorIdString );
|
2022-12-06 13:13:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setValidationErrors( {
|
|
|
|
[ errorIdString ]: {
|
2023-01-13 15:54:35 +00:00
|
|
|
message: label
|
|
|
|
? getValidityMessageForInput( label, inputObject )
|
|
|
|
: inputObject.validationMessage,
|
2022-12-06 13:13:21 +00:00
|
|
|
hidden: errorsHidden,
|
|
|
|
},
|
|
|
|
} );
|
2020-10-27 14:37:18 +00:00
|
|
|
},
|
2022-12-06 13:13:21 +00:00
|
|
|
[
|
|
|
|
clearValidationError,
|
|
|
|
customValidation,
|
|
|
|
errorIdString,
|
|
|
|
setValidationErrors,
|
2023-01-13 15:54:35 +00:00
|
|
|
label,
|
2022-12-06 13:13:21 +00:00
|
|
|
]
|
2020-10-27 14:37:18 +00:00
|
|
|
);
|
2020-03-23 11:22:00 +00:00
|
|
|
|
2021-12-13 16:44:28 +00:00
|
|
|
/**
|
2022-12-08 12:14:14 +00:00
|
|
|
* Handle browser autofill / changes via data store.
|
|
|
|
*
|
2023-08-03 11:02:20 +00:00
|
|
|
* Trigger validation on incoming state change if the current element is not in focus. This is because autofilled
|
|
|
|
* elements do not trigger the blur() event, and so values can be validated in the background if the state changes
|
|
|
|
* elsewhere.
|
2022-12-08 12:14:14 +00:00
|
|
|
*
|
|
|
|
* Errors are immediately visible.
|
2021-12-20 12:16:41 +00:00
|
|
|
*/
|
2020-03-23 11:22:00 +00:00
|
|
|
useEffect( () => {
|
2021-12-20 12:16:41 +00:00
|
|
|
if (
|
2022-12-06 13:13:21 +00:00
|
|
|
value !== previousValue &&
|
|
|
|
( value || previousValue ) &&
|
|
|
|
inputRef &&
|
|
|
|
inputRef.current !== null &&
|
2021-12-20 12:16:41 +00:00
|
|
|
inputRef.current?.ownerDocument?.activeElement !== inputRef.current
|
|
|
|
) {
|
2023-08-03 11:02:20 +00:00
|
|
|
onChange( customFormatter( inputRef.current.value ) );
|
2020-03-23 11:22:00 +00:00
|
|
|
}
|
2023-08-03 11:02:20 +00:00
|
|
|
}, [ validateInput, customFormatter, value, previousValue, onChange ] );
|
2022-12-06 13:13:21 +00:00
|
|
|
|
|
|
|
/**
|
2022-12-08 12:14:14 +00:00
|
|
|
* Validation on mount.
|
|
|
|
*
|
|
|
|
* If the input is in pristine state on mount, focus the element (if focusOnMount is enabled), and validate in the
|
|
|
|
* background.
|
|
|
|
*
|
|
|
|
* Errors are hidden until blur.
|
2022-12-06 13:13:21 +00:00
|
|
|
*/
|
|
|
|
useEffect( () => {
|
2022-12-08 12:14:14 +00:00
|
|
|
if ( ! isPristine ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( focusOnMount ) {
|
2022-12-06 13:13:21 +00:00
|
|
|
inputRef.current?.focus();
|
|
|
|
}
|
2023-04-11 08:50:59 +00:00
|
|
|
|
|
|
|
// if validateOnMount is false, only validate input if focusOnMount is also false
|
|
|
|
if ( validateOnMount || ! focusOnMount ) {
|
|
|
|
validateInput( true );
|
|
|
|
}
|
|
|
|
|
2022-12-06 13:13:21 +00:00
|
|
|
setIsPristine( false );
|
2023-04-11 08:50:59 +00:00
|
|
|
}, [
|
|
|
|
validateOnMount,
|
|
|
|
focusOnMount,
|
|
|
|
isPristine,
|
|
|
|
setIsPristine,
|
|
|
|
validateInput,
|
|
|
|
] );
|
2020-03-23 11:22:00 +00:00
|
|
|
|
2020-04-02 09:27:54 +00:00
|
|
|
// Remove validation errors when unmounted.
|
|
|
|
useEffect( () => {
|
|
|
|
return () => {
|
2021-05-19 09:55:15 +00:00
|
|
|
clearValidationError( errorIdString );
|
2020-04-02 09:27:54 +00:00
|
|
|
};
|
2021-05-19 09:55:15 +00:00
|
|
|
}, [ clearValidationError, errorIdString ] );
|
2020-04-02 09:27:54 +00:00
|
|
|
|
2022-12-06 13:13:21 +00:00
|
|
|
if ( passedErrorMessage !== '' && isObject( validationError ) ) {
|
2022-09-22 09:54:02 +00:00
|
|
|
validationError.message = passedErrorMessage;
|
2021-06-04 08:44:26 +00:00
|
|
|
}
|
2021-12-13 16:44:28 +00:00
|
|
|
|
2022-09-22 09:54:02 +00:00
|
|
|
const hasError = validationError?.message && ! validationError?.hidden;
|
2020-03-23 11:22:00 +00:00
|
|
|
const describedBy =
|
2022-09-22 09:54:02 +00:00
|
|
|
showError && hasError && validationErrorId
|
|
|
|
? validationErrorId
|
2020-03-23 11:22:00 +00:00
|
|
|
: ariaDescribedBy;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TextInput
|
|
|
|
className={ classnames( className, {
|
|
|
|
'has-error': hasError,
|
|
|
|
} ) }
|
2021-09-14 11:26:41 +00:00
|
|
|
aria-invalid={ hasError === true }
|
2020-03-23 11:22:00 +00:00
|
|
|
id={ textInputId }
|
|
|
|
feedback={
|
2021-05-19 09:55:15 +00:00
|
|
|
showError && (
|
2021-06-07 09:16:47 +00:00
|
|
|
<ValidationInputError
|
|
|
|
errorMessage={ passedErrorMessage }
|
|
|
|
propertyName={ errorIdString }
|
|
|
|
/>
|
2021-05-19 09:55:15 +00:00
|
|
|
)
|
2020-03-23 11:22:00 +00:00
|
|
|
}
|
|
|
|
ref={ inputRef }
|
2023-08-03 11:02:20 +00:00
|
|
|
onChange={ ( newValue ) => {
|
2022-12-06 13:13:21 +00:00
|
|
|
// Hide errors while typing.
|
2021-05-19 09:55:15 +00:00
|
|
|
hideValidationError( errorIdString );
|
2022-12-06 13:13:21 +00:00
|
|
|
|
2023-08-03 11:02:20 +00:00
|
|
|
// Validate the input value.
|
2022-12-06 13:13:21 +00:00
|
|
|
validateInput( true );
|
|
|
|
|
2023-08-03 11:02:20 +00:00
|
|
|
// Push the changes up to the parent component.
|
|
|
|
onChange( customFormatter( newValue ) );
|
2022-12-06 13:13:21 +00:00
|
|
|
} }
|
2023-08-03 11:02:20 +00:00
|
|
|
onBlur={ () => validateInput( false ) }
|
2020-03-23 11:22:00 +00:00
|
|
|
ariaDescribedBy={ describedBy }
|
2021-12-20 12:16:41 +00:00
|
|
|
value={ value }
|
2023-01-13 15:54:35 +00:00
|
|
|
title=""
|
|
|
|
label={ label }
|
2020-03-23 11:22:00 +00:00
|
|
|
{ ...rest }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2022-11-10 10:05:41 +00:00
|
|
|
export const __ValidatedTexInputWithoutId = ValidatedTextInput;
|
2020-03-23 11:22:00 +00:00
|
|
|
export default withInstanceId( ValidatedTextInput );
|