🏷️ field-level errors for form inputs
Browse files at this commit0840786b2eef1b19b2be312f047eb698b982ac26Parent: a69482e- feat
- 🏷️ field-level errors for form inputs
5 changed files +63 -9
- src/components/ui/input.tsx +17 -3
- src/components/ui/textarea.tsx +18 -4
- src/i18n/messages.ts +3 -0
- src/pages/classes-page.test.tsx +15 -0
- src/pages/classes-page.tsx +10 -2
src/components/ui/input.tsxmodified+17 -3
| @@ -1,18 +1,32 @@ | |||
| 1 | import type { ComponentProps } from "solid-js"; | 1 | import type { ComponentProps } from "solid-js"; |
| 2 | -import { splitProps } from "solid-js"; | 2 | +import { Show, splitProps } from "solid-js"; |
| 3 | import { cn } from "@/lib/cn"; | 3 | import { cn } from "@/lib/cn"; |
| 4 | 4 | ||
| 5 | -export type InputProps = ComponentProps<"input">; | 5 | +export type InputProps = ComponentProps<"input"> & { |
| 6 | + /** | ||
| 7 | + * Message shown under the field. Marks it invalid and describes it for | ||
| 8 | + * assistive tech; needs an `id` so the message can be linked. | ||
| 9 | + */ | ||
| 10 | + error?: string; | ||
| 11 | +}; | ||
| 6 | 12 | ||
| 7 | export function Input(props: InputProps) { | 13 | export function Input(props: InputProps) { |
| 8 | - const [local, rest] = splitProps(props, ["class"]); | 14 | + const [local, rest] = splitProps(props, ["class", "error"]); |
| 15 | + const errorId = () => (props.id ? `${props.id}-error` : undefined); | ||
| 9 | return ( | 16 | return ( |
| 17 | + <> | ||
| 10 | <input | 18 | <input |
| 11 | class={cn( | 19 | class={cn( |
| 12 | "flex h-9 w-full rounded-md border border-input bg-surface-base px-3 py-1 text-base shadow-sm transition-all file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-text-placeholder hover:border-ring/45 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background aria-invalid:border-destructive aria-invalid:ring-destructive disabled:cursor-not-allowed disabled:bg-surface-tint disabled:text-muted-foreground disabled:opacity-70 md:text-sm", | 20 | "flex h-9 w-full rounded-md border border-input bg-surface-base px-3 py-1 text-base shadow-sm transition-all file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-text-placeholder hover:border-ring/45 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background aria-invalid:border-destructive aria-invalid:ring-destructive disabled:cursor-not-allowed disabled:bg-surface-tint disabled:text-muted-foreground disabled:opacity-70 md:text-sm", |
| 13 | local.class, | 21 | local.class, |
| 14 | )} | 22 | )} |
| 23 | + aria-invalid={local.error ? true : undefined} | ||
| 24 | + aria-describedby={local.error ? errorId() : undefined} | ||
| 15 | {...rest} | 25 | {...rest} |
| 16 | /> | 26 | /> |
| 27 | + <Show when={local.error}> | ||
| 28 | + <p id={errorId()} class="text-xs font-medium text-destructive">{local.error}</p> | ||
| 29 | + </Show> | ||
| 30 | + </> | ||
| 17 | ); | 31 | ); |
| 18 | } | 32 | } |
src/components/ui/textarea.tsxmodified+18 -4
| @@ -1,18 +1,32 @@ | |||
| 1 | import type { ComponentProps } from "solid-js"; | 1 | import type { ComponentProps } from "solid-js"; |
| 2 | -import { splitProps } from "solid-js"; | 2 | +import { Show, splitProps } from "solid-js"; |
| 3 | import { cn } from "@/lib/cn"; | 3 | import { cn } from "@/lib/cn"; |
| 4 | 4 | ||
| 5 | -export type TextareaProps = ComponentProps<"textarea">; | 5 | +export type TextareaProps = ComponentProps<"textarea"> & { |
| 6 | + /** | ||
| 7 | + * Message shown under the field. Marks it invalid and describes it for | ||
| 8 | + * assistive tech; needs an `id` so the message can be linked. | ||
| 9 | + */ | ||
| 10 | + error?: string; | ||
| 11 | +}; | ||
| 6 | 12 | ||
| 7 | export function Textarea(props: TextareaProps) { | 13 | export function Textarea(props: TextareaProps) { |
| 8 | - const [local, rest] = splitProps(props, ["class"]); | 14 | + const [local, rest] = splitProps(props, ["class", "error"]); |
| 15 | + const errorId = () => (props.id ? `${props.id}-error` : undefined); | ||
| 9 | return ( | 16 | return ( |
| 17 | + <> | ||
| 10 | <textarea | 18 | <textarea |
| 11 | class={cn( | 19 | class={cn( |
| 12 | - "flex min-h-15 w-full rounded-md border border-input bg-background/90 px-3 py-2 text-base shadow-sm transition-all placeholder:text-muted-foreground/80 hover:border-ring/45 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", | 20 | + "flex min-h-15 w-full rounded-md border border-input bg-background/90 px-3 py-2 text-base shadow-sm transition-all placeholder:text-muted-foreground/80 hover:border-ring/45 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background aria-invalid:border-destructive aria-invalid:ring-destructive disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", |
| 13 | local.class, | 21 | local.class, |
| 14 | )} | 22 | )} |
| 23 | + aria-invalid={local.error ? true : undefined} | ||
| 24 | + aria-describedby={local.error ? errorId() : undefined} | ||
| 15 | {...rest} | 25 | {...rest} |
| 16 | /> | 26 | /> |
| 27 | + <Show when={local.error}> | ||
| 28 | + <p id={errorId()} class="text-xs font-medium text-destructive">{local.error}</p> | ||
| 29 | + </Show> | ||
| 30 | + </> | ||
| 17 | ); | 31 | ); |
| 18 | } | 32 | } |
src/i18n/messages.tsmodified+3 -0
| @@ -616,6 +616,7 @@ export type MessageKey = | |||
| 616 | | "common.deleted" | 616 | | "common.deleted" |
| 617 | | "common.saved" | 617 | | "common.saved" |
| 618 | | "common.loadAll" | 618 | | "common.loadAll" |
| 619 | + | "form.fieldRequired" | ||
| 619 | | "common.close" | 620 | | "common.close" |
| 620 | | "common.dismiss" | 621 | | "common.dismiss" |
| 621 | | "sidePanel.discardTitle" | 622 | | "sidePanel.discardTitle" |
| @@ -2757,6 +2758,7 @@ const en: Dict = { | |||
| 2757 | "common.deleted": "Deleted.", | 2758 | "common.deleted": "Deleted.", |
| 2758 | "common.saved": "Saved successfully.", | 2759 | "common.saved": "Saved successfully.", |
| 2759 | "common.loadAll": "Load all", | 2760 | "common.loadAll": "Load all", |
| 2761 | + "form.fieldRequired": "This field is required.", | ||
| 2760 | "common.close": "Close", | 2762 | "common.close": "Close", |
| 2761 | "common.dismiss": "Dismiss", | 2763 | "common.dismiss": "Dismiss", |
| 2762 | "sidePanel.discardTitle": "Discard your changes?", | 2764 | "sidePanel.discardTitle": "Discard your changes?", |
| @@ -4900,6 +4902,7 @@ const tr: Dict = { | |||
| 4900 | "common.deleted": "Silindi.", | 4902 | "common.deleted": "Silindi.", |
| 4901 | "common.saved": "Başarıyla kaydedildi.", | 4903 | "common.saved": "Başarıyla kaydedildi.", |
| 4902 | "common.loadAll": "Tümünü yükle", | 4904 | "common.loadAll": "Tümünü yükle", |
| 4905 | + "form.fieldRequired": "Bu alan zorunlu.", | ||
| 4903 | "common.close": "Kapat", | 4906 | "common.close": "Kapat", |
| 4904 | "common.dismiss": "Gizle", | 4907 | "common.dismiss": "Gizle", |
| 4905 | "sidePanel.discardTitle": "Değişiklikler silinsin mi?", | 4908 | "sidePanel.discardTitle": "Değişiklikler silinsin mi?", |
src/pages/classes-page.test.tsxmodified+15 -0
| @@ -86,3 +86,18 @@ test("a create whose blueprint left pairs behind holds the page until the report | |||
| 86 | expect(navigate).toHaveBeenCalledWith({ to: "/management/classes/$id", params: { id: "c-new" } }), | 86 | expect(navigate).toHaveBeenCalledWith({ to: "/management/classes/$id", params: { id: "c-new" } }), |
| 87 | ); | 87 | ); |
| 88 | }); | 88 | }); |
| 89 | + | ||
| 90 | +test("a blank class name is flagged under the field, not sent", async () => { | ||
| 91 | + getClasses.mockResolvedValue({ items: [] }); | ||
| 92 | + render(() => <PreferencesProvider><ClassesPage /></PreferencesProvider>); | ||
| 93 | + | ||
| 94 | + fireEvent.click(await screen.findByRole("button", { name: /New class/ })); | ||
| 95 | + const field = await screen.findByLabelText(/^Name/); | ||
| 96 | + fireEvent.input(field, { target: { value: " " } }); | ||
| 97 | + fireEvent.click(screen.getByRole("button", { name: "Create" })); | ||
| 98 | + | ||
| 99 | + expect(await screen.findByText(/This field is required|Bu alan zorunlu/)).toBeTruthy(); | ||
| 100 | + expect(field.getAttribute("aria-invalid")).toBe("true"); | ||
| 101 | + expect(field.getAttribute("aria-describedby")).toBe("class-name-error"); | ||
| 102 | + expect(postClass).not.toHaveBeenCalled(); | ||
| 103 | +}); | ||
src/pages/classes-page.tsxmodified+10 -2
| @@ -57,6 +57,7 @@ function ClassesContent() { | |||
| 57 | const [yearId, setYearId] = createSignal(""); | 57 | const [yearId, setYearId] = createSignal(""); |
| 58 | const [teacherId, setTeacherId] = createSignal(""); | 58 | const [teacherId, setTeacherId] = createSignal(""); |
| 59 | const [error, setError] = createSignal(""); | 59 | const [error, setError] = createSignal(""); |
| 60 | + const [nameError, setNameError] = createSignal(""); | ||
| 60 | const [pending, setPending] = createSignal(false); | 61 | const [pending, setPending] = createSignal(false); |
| 61 | // A create whose grade's blueprint left pairs behind reports them here before | 62 | // A create whose grade's blueprint left pairs behind reports them here before |
| 62 | // it opens the class: the response is the only place those rows ever appear. | 63 | // it opens the class: the response is the only place those rows ever appear. |
| @@ -116,6 +117,13 @@ function ClassesContent() { | |||
| 116 | const createClass = async (event: SubmitEvent) => { | 117 | const createClass = async (event: SubmitEvent) => { |
| 117 | event.preventDefault(); | 118 | event.preventDefault(); |
| 118 | setError(""); | 119 | setError(""); |
| 120 | + // Checked here rather than by the browser, so the message sits under the | ||
| 121 | + // field in the app's own words — and a name of only spaces is caught too. | ||
| 122 | + if (!name().trim()) { | ||
| 123 | + setNameError(t("form.fieldRequired")); | ||
| 124 | + document.getElementById("class-name")?.focus(); | ||
| 125 | + return; | ||
| 126 | + } | ||
| 119 | setPending(true); | 127 | setPending(true); |
| 120 | try { | 128 | try { |
| 121 | const created = await postClass({ | 129 | const created = await postClass({ |
| @@ -174,9 +182,9 @@ function ClassesContent() { | |||
| 174 | return ( | 182 | return ( |
| 175 | <div class="space-y-5"> | 183 | <div class="space-y-5"> |
| 176 | <SidePanel open={canManage() && showForm()} onOpenChange={setShowForm} guardUnsaved title={t("classGroups.newClass")} description={t("classGroups.subtitle")}> | 184 | <SidePanel open={canManage() && showForm()} onOpenChange={setShowForm} guardUnsaved title={t("classGroups.newClass")} description={t("classGroups.subtitle")}> |
| 177 | - <form class="space-y-4" onSubmit={createClass}> | 185 | + <form class="space-y-4" noValidate onSubmit={createClass}> |
| 178 | <div class="space-y-3"> | 186 | <div class="space-y-3"> |
| 179 | - <div class="space-y-1.5"><Label for="class-name">{t("classGroups.className")}<span class="ml-0.5 text-destructive">*</span></Label><Input id="class-name" required maxlength={limits.latest?.course.max_class_name_len} value={name()} onInput={(e) => setName(e.currentTarget.value)} /></div> | 187 | + <div class="space-y-1.5"><Label for="class-name">{t("classGroups.className")}<span class="ml-0.5 text-destructive">*</span></Label><Input id="class-name" required aria-required="true" maxlength={limits.latest?.course.max_class_name_len} value={name()} error={nameError()} onInput={(e) => { setName(e.currentTarget.value); setNameError(""); }} /></div> |
| 180 | <div class="space-y-1.5"><Label for="class-grade">{t("classGroups.grade")}</Label><Input id="class-grade" maxlength={limits.latest?.course.max_class_grade_len} value={grade()} onInput={(e) => setGrade(e.currentTarget.value)} /></div> | 188 | <div class="space-y-1.5"><Label for="class-grade">{t("classGroups.grade")}</Label><Input id="class-grade" maxlength={limits.latest?.course.max_class_grade_len} value={grade()} onInput={(e) => setGrade(e.currentTarget.value)} /></div> |
| 181 | <div class="space-y-1.5"><Label for="class-year">{t("academicYears.year")}</Label><Select id="class-year" value={yearId()} onChange={(e) => setYearId(e.currentTarget.value)}><option value="">{t("academicYears.unassigned")}</option><For each={years.latest ?? []}>{(year) => <option value={year.id}>{year.name}</option>}</For></Select></div> | 189 | <div class="space-y-1.5"><Label for="class-year">{t("academicYears.year")}</Label><Select id="class-year" value={yearId()} onChange={(e) => setYearId(e.currentTarget.value)}><option value="">{t("academicYears.unassigned")}</option><For each={years.latest ?? []}>{(year) => <option value={year.id}>{year.name}</option>}</For></Select></div> |
| 182 | <UserSearchSelect id="class-teacher" label={t("classGroups.homeroomTeacher")} value={teacherId()} onChange={setTeacherId} placeholder={t("classGroups.selectTeacher")} role="teacher" /> | 190 | <UserSearchSelect id="class-teacher" label={t("classGroups.homeroomTeacher")} value={teacherId()} onChange={setTeacherId} placeholder={t("classGroups.selectTeacher")} role="teacher" /> |