Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Paste handler for the TOTP interface. #154

Merged
merged 6 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions settings/src/components/auto-tabbing-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,13 @@ import { useCallback } from '@wordpress/element';
import NumericControl from './numeric-control';

const AutoTabbingInput = ( props ) => {
const { inputs, setInputs, onComplete, error } = props;
const { inputs, setInputs, error, setError } = props;

const handleChange = useCallback( ( value, event, index, inputRef ) => {
setInputs( ( prevInputs ) => {
const newInputs = [ ...prevInputs ];

// Clean input
if ( value.trim() === '' ) {
event.target.value = '';
value = '';
}

newInputs[ index ] = value;

// Check if all inputs are filled
const allFilled = newInputs.every( ( input ) => '' !== input );
if ( allFilled && onComplete ) {
onComplete( true );
} else {
onComplete( false );
}
newInputs[ index ] = value.trim() === '' ? '' : value;
Copy link
Contributor

@adamwoodnz adamwoodnz May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than modifying the value of the DOM element here, we can simply update the inputs, and the data will flow down to the DOM.

The complete state can also be derived from the inputs values, rather than checking on every change to an input, see the check for every input having a value here


return newInputs;
} );
Expand All @@ -45,8 +31,26 @@ const AutoTabbingInput = ( props ) => {
}
}, [] );

const handlePaste = useCallback( ( event ) => {
event.preventDefault();

const newInputs = event.clipboardData
.getData( 'Text' )
.replace( /[^0-9]/g, '' )
.split( '' );

if ( inputs.length === newInputs.length ) {
setInputs( newInputs );
} else {
setError( 'The code you pasted is not the correct length.' );
}
}, [] );

return (
<div className={ 'wporg-2fa__auto-tabbing-input' + ( error ? ' is-error' : '' ) }>
<div
className={ 'wporg-2fa__auto-tabbing-input' + ( error ? ' is-error' : '' ) }
onPaste={ handlePaste }
>
{ inputs.map( ( value, index ) => (
<NumericControl
{ ...props }
Expand Down
21 changes: 13 additions & 8 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,18 +222,23 @@ function createQrCode( data ) {
* @param props.setError
*/
function SetupForm( { handleEnable, qrCodeUrl, secretKey, inputs, setInputs, error, setError } ) {
const [ isInputComplete, setIsInputComplete ] = useState( false );
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 handleComplete = useCallback( ( isComplete ) => setIsInputComplete( isComplete ), [] );
const handleClearClick = useCallback( () => setInputs( Array( 6 ).fill( '' ) ), [] );
const handleClearClick = useCallback( () => {
setInputs( Array( 6 ).fill( '' ) );
}, [] );

const canSubmit = qrCodeUrl && secretKey && isInputComplete;
const canSubmit = qrCodeUrl && secretKey && inputs.every( ( input ) => !! input );

return (
<Flex
Expand All @@ -255,7 +260,7 @@ function SetupForm( { handleEnable, qrCodeUrl, secretKey, inputs, setInputs, err
inputs={ inputs }
setInputs={ setInputs }
error={ error }
onComplete={ handleComplete }
setError={ setError }
/>

<div className="wporg-2fa__submit-btn-container">
Expand Down