Skip to content

Commit a170e56

Browse files
Merge pull request #5740 from dfe-analytical-services/EES-5961-publication-contact-form-modal-focus-on-close
EES-5961 Focus contact form modal trigger button on modal close
2 parents c308233 + f3ec77d commit a170e56

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/explore-education-statistics-admin/src/pages/publication/components/PublicationContactForm.tsx

+28-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Form from '@common/components/form/Form';
1010
import ModalConfirm from '@common/components/ModalConfirm';
1111
import useToggle from '@common/hooks/useToggle';
1212
import Yup from '@common/validation/yup';
13-
import React, { useMemo } from 'react';
13+
import React, { useEffect, useMemo, useRef } from 'react';
1414
import { ObjectSchema } from 'yup';
1515

1616
interface FormValues {
@@ -34,6 +34,13 @@ export default function PublicationContactForm({
3434
onSubmit,
3535
}: Props) {
3636
const [showConfirmModal, toggleConfirmModal] = useToggle(false);
37+
const submitButtonRef = useRef<HTMLButtonElement>(null);
38+
39+
useEffect(() => {
40+
if (showConfirmModal === false) {
41+
submitButtonRef.current?.focus();
42+
}
43+
}, [showConfirmModal]);
3744

3845
const handleSubmit = async (values: FormValues) => {
3946
const contact = values;
@@ -111,26 +118,30 @@ export default function PublicationContactForm({
111118
/>
112119

113120
<ButtonGroup>
114-
<Button type="submit">Update contact details</Button>
121+
<Button type="submit" ref={submitButtonRef}>
122+
Update contact details
123+
</Button>
115124
<ButtonText onClick={onCancel}>Cancel</ButtonText>
116125
</ButtonGroup>
117126
</Form>
118127

119-
<ModalConfirm
120-
title="Confirm contact changes"
121-
onConfirm={async () => {
122-
await form.handleSubmit(handleSubmit)();
123-
}}
124-
onExit={toggleConfirmModal.off}
125-
onCancel={toggleConfirmModal.off}
126-
open={showConfirmModal}
127-
>
128-
<p>
129-
Any changes made here will appear on the public site
130-
immediately.
131-
</p>
132-
<p>Are you sure you want to save the changes?</p>
133-
</ModalConfirm>
128+
{showConfirmModal && (
129+
<ModalConfirm
130+
title="Confirm contact changes"
131+
onConfirm={async () => {
132+
await form.handleSubmit(handleSubmit)();
133+
}}
134+
onExit={toggleConfirmModal.off}
135+
onCancel={toggleConfirmModal.off}
136+
open
137+
>
138+
<p>
139+
Any changes made here will appear on the public site
140+
immediately.
141+
</p>
142+
<p>Are you sure you want to save the changes?</p>
143+
</ModalConfirm>
144+
)}
134145
</>
135146
);
136147
}}

0 commit comments

Comments
 (0)