Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Remove validateOnMount in favour of validation on state change (#5381)
Browse files Browse the repository at this point in the history
* Remove validateOnMount in favour of validation on state change.

* Use useEffect

* test
  • Loading branch information
mikejolley authored Dec 20, 2021
1 parent d319e24 commit 1a26e02
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export const TotalsCoupon = ( {
onChange={ ( newCouponValue ) => {
setCouponValue( newCouponValue );
} }
validateOnMount={ false }
focusOnMount={ true }
showError={ false }
/>
Expand Down
45 changes: 21 additions & 24 deletions assets/js/base/components/text-input/validated-text-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ type ValidatedTextInputProps = (
className?: string;
ariaDescribedBy?: string;
errorId?: string;
validateOnMount?: boolean;
focusOnMount?: boolean;
showError?: boolean;
errorMessage?: string;
onChange: ( newValue: string ) => void;
value: string;
};

const ValidatedTextInput = ( {
Expand All @@ -47,11 +47,11 @@ const ValidatedTextInput = ( {
id,
ariaDescribedBy,
errorId,
validateOnMount = true,
focusOnMount = false,
onChange,
showError = true,
errorMessage: passedErrorMessage = '',
value = '',
...rest
}: ValidatedTextInputProps ) => {
const [ isPristine, setIsPristine ] = useState( true );
Expand Down Expand Up @@ -96,34 +96,31 @@ const ValidatedTextInput = ( {
);

/**
* Runs validation on change if the current element is not in focus. This is because autofilled elements do not
* trigger the blur() event.
* Focus on mount
*
* If the input is in pristine state, focus the element.
*/
const maybeValidateOnChange = useCallback( () => {
if (
inputRef.current?.ownerDocument.activeElement !== inputRef.current
) {
validateInput( true );
}
}, [ validateInput ] );

useEffect( () => {
if ( isPristine ) {
if ( focusOnMount ) {
inputRef.current?.focus();
}
setIsPristine( false );
if ( isPristine && focusOnMount ) {
inputRef.current?.focus();
}
setIsPristine( false );
}, [ focusOnMount, isPristine, setIsPristine ] );

/**
* Value Validation
*
* Runs validation on 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.
*/
useEffect( () => {
if ( isPristine ) {
if ( validateOnMount ) {
validateInput();
}
setIsPristine( false );
if (
inputRef.current?.ownerDocument?.activeElement !== inputRef.current
) {
validateInput( true );
}
}, [ isPristine, setIsPristine, validateOnMount, validateInput ] );
// We need to track value even if it is not directly used so we know when it changes.
}, [ value, validateInput ] );

// Remove validation errors when unmounted.
useEffect( () => {
Expand Down Expand Up @@ -170,9 +167,9 @@ const ValidatedTextInput = ( {
onChange={ ( val ) => {
hideValidationError( errorIdString );
onChange( val );
maybeValidateOnChange();
} }
ariaDescribedBy={ describedBy }
value={ value }
{ ...rest }
/>
);
Expand Down

0 comments on commit 1a26e02

Please sign in to comment.