Use useEffect to focus text input in the preview modal on open

[MAILPOET-6011]
This commit is contained in:
Jan Lysý
2024-04-30 16:44:00 +02:00
committed by Rostislav Wolný
parent 47d5fa93d4
commit e47ce06b4a

View File

@ -2,7 +2,11 @@ import { Button, Modal, TextControl } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data'; import { useDispatch, useSelect } from '@wordpress/data';
import { check, Icon } from '@wordpress/icons'; import { check, Icon } from '@wordpress/icons';
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { createInterpolateElement } from '@wordpress/element'; import {
useEffect,
useRef,
createInterpolateElement,
} from '@wordpress/element';
import { ENTER } from '@wordpress/keycodes'; import { ENTER } from '@wordpress/keycodes';
import { isEmail } from '@wordpress/url'; import { isEmail } from '@wordpress/url';
import { useEntityProp } from '@wordpress/core-data'; import { useEntityProp } from '@wordpress/core-data';
@ -13,6 +17,8 @@ import {
} from '../../store'; } from '../../store';
export function SendPreviewEmail() { export function SendPreviewEmail() {
const sendToRef = useRef(null);
const { const {
requestSendingNewsletterPreview, requestSendingNewsletterPreview,
togglePreviewModal, togglePreviewModal,
@ -38,6 +44,13 @@ export function SendPreviewEmail() {
const closeCallback = () => togglePreviewModal(false); const closeCallback = () => togglePreviewModal(false);
// We use this effect to focus on the input field when the modal is opened
useEffect(() => {
if (isModalOpened) {
sendToRef.current?.focus();
}
}, [isModalOpened]);
if (!isModalOpened) { if (!isModalOpened) {
return null; return null;
} }
@ -47,7 +60,7 @@ export function SendPreviewEmail() {
className="mailpoet-send-preview-email" className="mailpoet-send-preview-email"
title={__('Send a test email', 'mailpoet')} title={__('Send a test email', 'mailpoet')}
onRequestClose={closeCallback} onRequestClose={closeCallback}
focusOnMount="firstContentElement" focusOnMount={false}
> >
{sendingPreviewStatus === SendingPreviewStatus.ERROR ? ( {sendingPreviewStatus === SendingPreviewStatus.ERROR ? (
<div className="mailpoet-send-preview-modal-notice-error"> <div className="mailpoet-send-preview-modal-notice-error">
@ -109,7 +122,6 @@ export function SendPreviewEmail() {
href="https://www.mail-tester.com/" href="https://www.mail-tester.com/"
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
tabIndex={-1}
/> />
), ),
link2: ( link2: (
@ -118,7 +130,6 @@ export function SendPreviewEmail() {
href="https://kb.mailpoet.com/article/147-test-your-spam-score-with-mail-tester" href="https://kb.mailpoet.com/article/147-test-your-spam-score-with-mail-tester"
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
tabIndex={-1}
/> />
), ),
}, },
@ -138,6 +149,7 @@ export function SendPreviewEmail() {
}} }}
value={previewToEmail} value={previewToEmail}
type="email" type="email"
ref={sendToRef}
required required
/> />
{sendingPreviewStatus === SendingPreviewStatus.SUCCESS ? ( {sendingPreviewStatus === SendingPreviewStatus.SUCCESS ? (