Skip to content

Commit

Permalink
Add live region to page
Browse files Browse the repository at this point in the history
  • Loading branch information
lortimer committed Aug 27, 2023
1 parent 88b9d2d commit 9902def
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/components/form/Form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe(Form.name, () => {
({ container } = render(<FormWithInputs/>));

form = screen.getByRole("form");
liveRegion = screen.getByTestId("live-region");
liveRegion = screen.getByTestId("form-live-region");
});

test("has no AxE violations", async () => {
Expand Down Expand Up @@ -83,7 +83,7 @@ describe(Form.name, () => {
render(<FormWithInputs validator={() => nameErrorMessage}/>);

form = screen.getByRole("form");
liveRegion = screen.getByTestId("live-region");
liveRegion = screen.getByTestId("form-live-region");

nameInput = within(form).getByRole("textbox", { name: "Name" });
ageInput = within(form).getByRole("textbox", { name: "Age" });
Expand Down Expand Up @@ -132,7 +132,7 @@ describe(Form.name, () => {
render(<FormWithInputs/>);

form = screen.getByRole("form");
liveRegion = screen.getByTestId("live-region");
liveRegion = screen.getByTestId("form-live-region");

nameInput = within(form).getByRole("textbox", { name: "Name" });
ageInput = within(form).getByRole("textbox", { name: "Age" });
Expand Down Expand Up @@ -265,4 +265,4 @@ const FormWithInputs = (props: { validator?: Validator }) => {
</Form>
</>
);
};
};
11 changes: 6 additions & 5 deletions src/components/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,21 @@ export const Form = ({ children, ariaLabelledBy, submitEndpoint, successMessage
}, [postFormData, onSubmitObservable]);

return (<div className={"form-container"}>
{showSuccessMessage && <UserMessageNotification message={successMessage} clearMessage={removeResponseMessage}
ref={responseMessage} className={"success"}/>
{showSuccessMessage &&
<UserMessageNotification message={successMessage} clearMessage={removeResponseMessage}
ref={responseMessage} className={"success"}/>
}

{errorMessage && <UserMessageNotification message={errorMessage} clearMessage={removeResponseMessage}
ref={responseMessage} className={"error"}/>
ref={responseMessage} className={"error"}/>
}
<form onSubmit={onSubmit} aria-labelledby={ariaLabelledBy} className={"form"}
data-testid={"form-component"}>
<FormProvider onSubmit={onSubmitObservable}>
{children}
</FormProvider>
<button type={"submit"} className={"form-submit"} disabled={submitting}>Submit</button>
<div aria-live={"polite"} ref={liveRegion} data-testid={"live-region"}>
<div aria-live={"polite"} ref={liveRegion} data-testid={"form-live-region"}>
{submitting && <span>Submitting...</span>}
</div>
</form>
Expand All @@ -99,4 +100,4 @@ function extractFormData(form: HTMLFormElement) {
}

return output;
}
}
10 changes: 9 additions & 1 deletion src/pages/Page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe(Page.name, () => {
});

describe("under normal conditions", () => {
let liveRegion: HTMLDivElement;

beforeEach(() => {
render(
Expand All @@ -27,6 +28,7 @@ describe(Page.name, () => {
</Page>
</ContextProvider>
);

});

test("sets the document title", () => {
Expand All @@ -42,6 +44,12 @@ describe(Page.name, () => {
expect(documentStateUpdated).toBe(true);
});
});

test("displays an empty live region on mount", () => {
liveRegion = screen.getByTestId("page-live-region");
expect(liveRegion).toHaveAttribute("aria-live", "polite");
expect(liveRegion.textContent).toBe("");
});
});

describe("under error conditions", () => {
Expand Down Expand Up @@ -104,4 +112,4 @@ const ContextProvider = ({ children, freshDocument }: { freshDocument: boolean }
{children}
</DocumentStateContext.Provider>
);
};
};
3 changes: 2 additions & 1 deletion src/pages/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const Page = ({ title, children }: PageProps) => {
<HeadingCheck/>
<div className={"page-container"}>
{children}
<div aria-live={"polite"} data-testid={"page-live-region"}/>
</div>
</>);
};
Expand Down Expand Up @@ -57,4 +58,4 @@ const HeadingCheck = () => {
return (<>
{error && process.env.NODE_ENV !== "production" && <ErrorMessageNotification message={error}/>}
</>);
};
};

0 comments on commit 9902def

Please sign in to comment.