Skip to content

Commit b7b9ce1

Browse files
authored
chore: Add auto focus for page headings (#5515)
* update focus heading
1 parent f1b4037 commit b7b9ce1

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

app/(gcforms)/[locale]/(support)/components/client/FocusHeader.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
import { useFocusIt } from "@lib/hooks/useFocusIt";
33
import { ReactElement, useRef } from "react";
44

5-
export const FocusHeader = ({ children }: { children: ReactElement | string }) => {
5+
export const FocusHeader = ({
6+
children,
7+
headingTag: HeadingTag = "h1",
8+
}: {
9+
children: ReactElement | string;
10+
headingTag?: "h1" | "h2" | "h3" | "h4" | "h5" | "h6";
11+
}) => {
612
const headingSuccessRef = useRef(null);
713
useFocusIt({ elRef: headingSuccessRef });
814

9-
return <h1 ref={headingSuccessRef}>{children}</h1>;
15+
return <HeadingTag ref={headingSuccessRef}>{children}</HeadingTag>;
1016
};

components/clientComponents/forms/Form/Form.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use client";
2-
import React, { useEffect, useState, useRef, useCallback } from "react";
2+
import React, { useEffect, useState, useCallback } from "react";
33
import { withFormik } from "formik";
44
import { getFormInitialValues } from "@lib/formBuilder";
55
import { getErrorList, setFocusOnErrorMessage, validateOnSubmit } from "@lib/validation/validation";
@@ -36,6 +36,8 @@ import { FormStatus } from "@gcforms/types";
3636
import { CaptchaFail } from "@clientComponents/globals/FormCaptcha/CaptchaFail";
3737
import { ga } from "@lib/client/clientHelpers";
3838

39+
import { FocusHeader } from "app/(gcforms)/[locale]/(support)/components/client/FocusHeader";
40+
3941
/**
4042
* This is the "inner" form component that isn't connected to Formik and just renders a simple form
4143
* @param props
@@ -59,7 +61,6 @@ const InnerForm: React.FC<InnerFormProps> = (props) => {
5961
const isGroupsCheck = groupsCheck(props.allowGrouping);
6062
const isShowReviewPage = showReviewPage(form);
6163
const showIntro = isGroupsCheck ? currentGroup === LockedSections.START : true;
62-
const groupsHeadingRef = useRef<HTMLHeadingElement>(null);
6364
const { getFormDelayWithGroups, getFormDelayWithoutGroups } = useFormDelay();
6465

6566
// Used to set any values we'd like added for use in the below withFormik handleSubmit().
@@ -181,9 +182,9 @@ const InnerForm: React.FC<InnerFormProps> = (props) => {
181182
isShowReviewPage &&
182183
currentGroup !== LockedSections.REVIEW &&
183184
currentGroup !== LockedSections.START && (
184-
<h2 tabIndex={-1} ref={groupsHeadingRef}>
185+
<FocusHeader headingTag="h2">
185186
{getGroupTitle(currentGroup, language as Language)}
186-
</h2>
187+
</FocusHeader>
187188
)}
188189

189190
{children}

lib/hooks/useFocusIt.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const useFocusIt = ({
2727
}) => {
2828
useEffect(() => {
2929
const el = elRef.current;
30+
3031
if (el) {
3132
// Add a tabindex if one does not exist on the element. This needed to focus an element,
3233
// unless it is a form control like an input or button

0 commit comments

Comments
 (0)