2020-03-23 11:22:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
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';
|
2023-01-13 15:54:35 +00:00
|
|
|
import { getValidityMessageForInput } from '../../utils';
|
2020-03-23 11:22:00 +00:00
|
|
|
|
2022-11-10 10:05:41 +00:00
|
|
|
interface ValidatedTextInputProps
|
|
|
|
extends Omit<
|
|
|
|
InputHTMLAttributes< HTMLInputElement >,
|
|
|
|
'onChange' | 'onBlur'
|
|
|
|
> {
|
2023-01-13 15:54:35 +00:00
|
|
|
// id to use for the input. If not provided, an id will be generated.
|
2021-05-19 09:55:15 +00:00
|
|
|
id?: string;
|
2023-01-13 15:54:35 +00:00
|
|
|
// Unique instance ID. id will be used instead if provided.
|
2022-07-01 23:06:25 +00:00
|
|
|
instanceId: string;
|
2023-01-13 15:54:35 +00:00
|
|
|
// Class name to add to the input.
|
2022-11-10 10:05:41 +00:00
|
|
|
className?: string | undefined;
|
2023-01-13 15:54:35 +00:00
|
|
|
// aria-describedby attribute to add to the input.
|
2022-11-10 10:05:41 +00:00
|
|
|
ariaDescribedBy?: string | undefined;
|
2023-01-13 15:54:35 +00:00
|
|
|
// id to use for the error message. If not provided, an id will be generated.
|
2021-05-19 09:55:15 +00:00
|
|
|
errorId?: string;
|
2023-01-13 15:54:35 +00:00
|
|
|
// if true, the input will be focused on mount.
|
2021-05-19 09:55:15 +00:00
|
|
|
focusOnMount?: boolean;
|
2023-01-13 15:54:35 +00:00
|
|
|
// Callback to run on change which is passed the updated value.
|
2021-05-19 09:55:15 +00:00
|
|
|
onChange: ( newValue: string ) => void;
|
2023-01-13 15:54:35 +00:00
|
|
|
// Optional label for the field.
|
2022-11-10 10:05:41 +00:00
|
|
|
label?: string | undefined;
|
2023-01-13 15:54:35 +00:00
|
|
|
// Field value.
|
2021-12-20 12:16:41 +00:00
|
|
|
value: string;
|
2023-01-13 15:54:35 +00:00
|
|
|
// If true, validation errors will be shown.
|
|
|
|
showError?: boolean;
|
|
|
|
// Error message to display alongside the field regardless of validation.
|
|
|
|
errorMessage?: string | undefined;
|
|
|
|
// Custom validation function that is run on change. Use setCustomValidity to set an error message.
|
2022-12-06 13:13:21 +00:00
|
|
|
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
|
|
|
customValidation,
|
2023-01-13 15:54:35 +00:00
|
|
|
label,
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
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.
|
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
|
|
|
|
) {
|
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 ] );
|
|
|
|
|
|
|
|
/**
|
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();
|
|
|
|
}
|
2022-12-08 12:14:14 +00:00
|
|
|
validateInput( true );
|
2022-12-06 13:13:21 +00:00
|
|
|
setIsPristine( false );
|
2022-12-08 12:14:14 +00:00
|
|
|
}, [ 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 }
|
|
|
|
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 }
|
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 );
|