Skip to content

Commit

Permalink
Clear the error when the inputs have changed
Browse files Browse the repository at this point in the history
Instead of when one is empty
  • Loading branch information
adamwoodnz committed May 23, 2023
1 parent a0a06c6 commit 345c6c4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions settings/src/components/totp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import apiFetch from '@wordpress/api-fetch';
import { Button, Notice, Flex } from '@wordpress/components';
import { Icon, cancelCircleFilled } from '@wordpress/icons';
import { RawHTML, useCallback, useContext, useEffect, useState } from '@wordpress/element';
import { RawHTML, useCallback, useContext, useEffect, useRef, useState } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -222,11 +222,17 @@ function createQrCode( data ) {
* @param props.setError
*/
function SetupForm( { handleEnable, qrCodeUrl, secretKey, inputs, setInputs, error, setError } ) {
const inputsRef = useRef( inputs );

useEffect( () => {
if ( error && inputs.some( ( input ) => input === '' ) ) {
const prevInputs = inputsRef.current;
inputsRef.current = inputs;

// Clear the error if any of the inputs have changed
if ( error && inputs.some( ( input, index ) => input !== prevInputs[ index ] ) ) {
setError( '' );
}
}, [ error, inputs ] );
}, [ error, inputs, inputsRef ] );

const handleClearClick = useCallback( () => {
setInputs( Array( 6 ).fill( '' ) );
Expand Down

0 comments on commit 345c6c4

Please sign in to comment.