Skip to content

Commit

Permalink
fix: Prevent event bubbling on click of checkbox (#3316)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Jun 26, 2024
1 parent 36016b9 commit 582e066
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ describe("Checklist Component - Grouped Layout", () => {
answers: ["S1_Option1", "S3_Option1"],
});
});

it("should not have any accessibility violations", async () => {
const { container } = setup(
<Checklist
Expand All @@ -185,6 +186,7 @@ describe("Checklist Component - Grouped Layout", () => {
const results = await axe(container);
expect(results).toHaveNoViolations();
});

it("should be navigable by keyboard", async () => {
const handleSubmit = jest.fn();

Expand Down Expand Up @@ -218,13 +220,11 @@ describe("Checklist Component - Grouped Layout", () => {
expect(screen.getByTestId("S2_Option1")).toHaveFocus();
// Select option using keyboard
await user.keyboard("[Space]");
expect(screen.getByTestId("S2_Option1")).toBeChecked();
// Tab to Section 2, Option 2
await user.tab();
expect(screen.getByTestId("S2_Option2")).toHaveFocus();
// Select option using keyboard
await user.keyboard("[Space]");
expect(screen.getByTestId("S2_Option2")).toBeChecked();
// Tab to Section 3, and navigate through to "Continue" without selecting anything
await user.tab();
expect(section3Button).toHaveFocus();
Expand Down Expand Up @@ -279,6 +279,7 @@ describe("Checklist Component - Basic & Images Layout", () => {
answers: ["flat_id", "house_id", "spaceship_id"],
});
});

it(`recovers checkboxes state when clicking the back button (${ChecklistLayout[type]} layout)`, async () => {
const handleSubmit = jest.fn();

Expand Down
8 changes: 6 additions & 2 deletions editor.planx.uk/src/ui/shared/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ export default function Checkbox({
onChange,
inputProps,
}: Props): FCReturn {
const handleChange = (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
e.preventDefault()
onChange && onChange();
}

return (
<Root onClick={() => onChange && onChange()}>
<Root onClick={handleChange}>
<Input
defaultChecked={checked}
type="checkbox"
id={id}
data-testid={id}
onChange={() => onChange && onChange()}
{...inputProps}
/>
<Icon checked={checked} />
Expand Down

0 comments on commit 582e066

Please sign in to comment.