Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(a11y): enforce positive numbers by default on NumberInput #2982

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions editor.planx.uk/src/@planx/components/NumberInput/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useFormik } from "formik";
import React from "react";
import ModalSection from "ui/editor/ModalSection";
import ModalSectionContent from "ui/editor/ModalSectionContent";
import OptionButton from "ui/editor/OptionButton";
import RichTextInput from "ui/editor/RichTextInput";
import Input from "ui/shared/Input";
import InputRow from "ui/shared/InputRow";
Expand Down Expand Up @@ -74,6 +75,19 @@ export default function NumberInputComponent(props: Props): FCReturn {
/>
</InputRowItem>
</InputRow>
<InputRow>
<OptionButton
selected={formik.values.allowNegatives}
onClick={() => {
formik.setFieldValue(
"allowNegatives",
!formik.values.allowNegatives,
);
}}
>
Allow negative numbers to be input
</OptionButton>
</InputRow>
</ModalSectionContent>
</ModalSection>
<MoreInformation
Expand Down
31 changes: 31 additions & 0 deletions editor.planx.uk/src/@planx/components/NumberInput/Public.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ test("allows 0 to be input as a valid number", async () => {
expect(handleSubmit).toHaveBeenCalledWith({ data: { num: 0 } });
});

test("requires a positive number to be input by default", async () => {
const handleSubmit = jest.fn();

const { user } = setup(
<NumberInput fn="doors" title="How many doors are you adding?" handleSubmit={handleSubmit} />,
);

expect(screen.getByRole("heading")).toHaveTextContent("How many doors are you adding?");

await user.type(screen.getByLabelText("How many doors are you adding?"), "-1");
await user.click(screen.getByTestId("continue-button"));

expect(screen.getByText("Enter a positive number")).toBeInTheDocument();
expect(handleSubmit).toHaveBeenCalledTimes(0);
});

test("allows negative numbers to be input when toggled on by editor", async () => {
const handleSubmit = jest.fn();

const { user } = setup(
<NumberInput fn="fahrenheit" title="What's the temperature?" handleSubmit={handleSubmit} allowNegatives={true} />,
);

expect(screen.getByRole("heading")).toHaveTextContent("What's the temperature?");

await user.type(screen.getByLabelText("What's the temperature?"), "-10");
await user.click(screen.getByTestId("continue-button"));

expect(handleSubmit).toHaveBeenCalledWith({ data: { fahrenheit: -10 } });
});

test("requires a value before being able to continue", async () => {
const handleSubmit = jest.fn();

Expand Down
11 changes: 10 additions & 1 deletion editor.planx.uk/src/@planx/components/NumberInput/Public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,20 @@ export default function NumberInputComponent(props: Props): FCReturn {
.required("Enter your answer before continuing")
.test({
name: "not a number",
message: "Enter a number",
message: (() => {
if (!props.allowNegatives) {
return "Enter a positive number";
}

return "Enter a number";
})(),
test: (value: string | undefined) => {
if (!value) {
return false;
}
if (!props.allowNegatives && value.startsWith("-")) {
return false;
}
return value === "0" ? true : Boolean(parseNumber(value));
},
}),
Expand Down
2 changes: 2 additions & 0 deletions editor.planx.uk/src/@planx/components/NumberInput/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface NumberInput extends MoreInformation {
description?: string;
fn?: string;
units?: string;
allowNegatives?: boolean;
}

export type UserData = number;
Expand All @@ -24,5 +25,6 @@ export const parseNumberInput = (
description: data?.description,
fn: data?.fn || "",
units: data?.units,
allowNegatives: data?.allowNegatives || false,
...parseMoreInformation(data),
});
Loading