2020-03-23 11:22:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2022-11-10 10:05:41 +00:00
|
|
|
import {
|
|
|
|
useRef,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
2022-12-06 13:13:21 +00:00
|
|
|
useCallback,
|
2022-11-10 10:05:41 +00:00
|
|
|
InputHTMLAttributes,
|
|
|
|
} from 'react';
|
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';
|
2020-03-23 11:22:00 +00:00
|
|
|
|
2022-11-10 10:05:41 +00:00
|
|
|
interface ValidatedTextInputProps
|
|
|
|
extends Omit<
|
|
|
|
InputHTMLAttributes< HTMLInputElement >,
|
|
|
|
'onChange' | 'onBlur'
|
|
|
|
> {
|
2021-05-19 09:55:15 +00:00
|
|
|
id?: string;
|
2022-07-01 23:06:25 +00:00
|
|
|
instanceId: string;
|
2022-11-10 10:05:41 +00:00
|
|
|
className?: string | undefined;
|
|
|
|
ariaDescribedBy?: string | undefined;
|
2021-05-19 09:55:15 +00:00
|
|
|
errorId?: string;
|
|
|
|
focusOnMount?: boolean;
|
|
|
|
showError?: boolean;
|
2022-11-10 10:05:41 +00:00
|
|
|
errorMessage?: string | undefined;
|
2021-05-19 09:55:15 +00:00
|
|
|
onChange: ( newValue: string ) => void;
|
2022-11-10 10:05:41 +00:00
|
|
|
label?: string | undefined;
|
2021-12-20 12:16:41 +00:00
|
|
|
value: string;
|
2022-12-06 13:13:21 +00:00
|
|
|
requiredMessage?: string | undefined;
|
|
|
|
customValidation?:
|
|
|
|
| ( ( inputObject: HTMLInputElement ) => boolean )
|
|
|
|
| undefined;
|
2022-07-01 23:06:25 +00:00
|
|
|
}
|
2021-05-19 09:55:15 +00:00
|
|
|
|
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 = '',
|
2022-12-06 13:13:21 +00:00
|
|
|
requiredMessage,
|
|
|
|
customValidation,
|
2020-03-23 11:22:00 +00:00
|
|
|
...rest
|
2022-07-01 23:06:25 +00:00
|
|
|
}: ValidatedTextInputProps ): JSX.Element => {
|
2020-10-27 14:37:18 +00:00
|
|
|
const [ isPristine, setIsPristine ] = useState( true );
|
2021-05-19 09:55:15 +00:00
|
|
|
const inputRef = useRef< HTMLInputElement >( null );
|
2022-12-06 13:13:21 +00:00
|
|
|
const previousValue = usePrevious( value );
|
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( '' );
|
|
|
|
|
|
|
|
const inputIsValid = customValidation
|
|
|
|
? inputObject.checkValidity() && customValidation( inputObject )
|
|
|
|
: inputObject.checkValidity();
|
|
|
|
|
2020-12-17 14:52:44 +00:00
|
|
|
if ( inputIsValid ) {
|
2021-05-19 09:55:15 +00:00
|
|
|
clearValidationError( errorIdString );
|
2022-12-06 13:13:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const validityState = inputObject.validity;
|
|
|
|
|
|
|
|
if ( validityState.valueMissing && requiredMessage ) {
|
|
|
|
inputObject.setCustomValidity( requiredMessage );
|
2020-10-27 14:37:18 +00:00
|
|
|
}
|
2022-12-06 13:13:21 +00:00
|
|
|
|
|
|
|
setValidationErrors( {
|
|
|
|
[ errorIdString ]: {
|
|
|
|
message:
|
|
|
|
inputObject.validationMessage ||
|
|
|
|
__( 'Invalid value.', 'woo-gutenberg-products-block' ),
|
|
|
|
hidden: errorsHidden,
|
|
|
|
},
|
|
|
|
} );
|
2020-10-27 14:37:18 +00:00
|
|
|
},
|
2022-12-06 13:13:21 +00:00
|
|
|
[
|
|
|
|
clearValidationError,
|
|
|
|
customValidation,
|
|
|
|
errorIdString,
|
|
|
|
requiredMessage,
|
|
|
|
setValidationErrors,
|
|
|
|
]
|
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-06 13:13:21 +00:00
|
|
|
* Trigger validation on state change if the current element is not in focus. This is because autofilled elements do not
|
2021-12-20 12:16:41 +00:00
|
|
|
* trigger the blur() event, and so values can be validated in the background if the state changes elsewhere.
|
|
|
|
*/
|
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
|
|
|
|
) {
|
2022-12-06 13:13:21 +00:00
|
|
|
validateInput( false );
|
2020-03-23 11:22:00 +00:00
|
|
|
}
|
2021-12-20 12:16:41 +00:00
|
|
|
// We need to track value even if it is not directly used so we know when it changes.
|
2022-12-06 13:13:21 +00:00
|
|
|
}, [ value, previousValue, validateInput ] );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the input is in pristine state on mount, focus the element.
|
|
|
|
*/
|
|
|
|
useEffect( () => {
|
|
|
|
if ( isPristine && focusOnMount ) {
|
|
|
|
inputRef.current?.focus();
|
|
|
|
}
|
|
|
|
setIsPristine( false );
|
|
|
|
}, [ focusOnMount, isPristine, setIsPristine ] );
|
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 }
|
|
|
|
onChange={ ( val ) => {
|
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
|
|
|
|
|
|
|
// Revalidate on user input so we know if the value is valid.
|
|
|
|
validateInput( true );
|
|
|
|
|
|
|
|
// Push the changes up to the parent component if the value is valid.
|
2020-03-23 11:22:00 +00:00
|
|
|
onChange( val );
|
|
|
|
} }
|
2022-12-06 13:13:21 +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 }
|
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 );
|