From 2cfca50570ddd15da84247138cbb5ea338f6587f Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 21 Jun 2023 17:19:55 +0800 Subject: [PATCH 1/7] Ensure the confirm dialog cannnot be submitted using enter when the cancel button is focused --- packages/components/src/confirm-dialog/component.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/components/src/confirm-dialog/component.tsx b/packages/components/src/confirm-dialog/component.tsx index 92fb3fa08fbb1..78f3bf6cafa51 100644 --- a/packages/components/src/confirm-dialog/component.tsx +++ b/packages/components/src/confirm-dialog/component.tsx @@ -7,7 +7,7 @@ import type { ForwardedRef, KeyboardEvent } from 'react'; * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { useCallback, useEffect, useState } from '@wordpress/element'; +import { useCallback, useEffect, useRef, useState } from '@wordpress/element'; /** * Internal dependencies @@ -42,6 +42,7 @@ function ConfirmDialog( const cx = useCx(); const wrapperClassName = cx( styles.wrapper ); + const cancelButtonRef = useRef(); const [ isOpen, setIsOpen ] = useState< boolean >(); const [ shouldSelfClose, setShouldSelfClose ] = useState< boolean >(); @@ -69,6 +70,10 @@ function ConfirmDialog( const handleEnter = useCallback( ( event: KeyboardEvent< HTMLDivElement > ) => { + // Avoid triggering the 'confirm' action when the cancel button is focused. + if ( event.target === cancelButtonRef.current ) { + return; + } if ( event.key === 'Enter' ) { handleEvent( onConfirm )( event ); } @@ -96,6 +101,7 @@ function ConfirmDialog( { children }