mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
fix(sheets): ask before a dismissal drops typed input
The eight-step medication wizard, the add sheets on /measurements, /mood and /labs, and the visit, vaccination, practitioner and checkup forms closed on a stray backdrop tap or swipe and lost what was typed. The dashboard quick entry and the capture menu asked first, through two hand-copied dialogs and a DOM check that read the first sheet body in the document and counted the date field's prefilled "now" as input. ResponsiveSheet now takes a dirty flag, or a check it hands its own body, and asks with the existing discard dialog. Forms that keep a seed pass whether they differ from it; the wizard compares what a save would send. Plain add forms pass the DOM check, which skips the date and time fields again. The three copies of the dialog are gone.
This commit is contained in:
1 parent
f519df4433
commit
ffde57869b
19 files changed
+580
-644
No files matched your search
@@ -1,140 +0,0 @@
|
||||
/**
|
||||
* v1.11.5 — `sheetBodyHasUnsavedInput` dirty-check coverage.
|
||||
*
|
||||
* The quick-entry sheets confirm-on-dismiss only when the body holds unsaved
|
||||
* input. The original walk read `input` / `textarea` only, so a user who
|
||||
* changed just the mood `role="radio"` selector or opened a Radix Select
|
||||
* (type / medication) could lose the change on a backdrop dismiss with no
|
||||
* confirm. These cases lock the extended detection in — and pin the
|
||||
* over-trigger guard so a pristine sheet (no radio chosen, every Select on its
|
||||
* default and closed) still reads clean.
|
||||
*
|
||||
* The repo runs vitest in the Node environment (no jsdom / happy-dom). The
|
||||
* function only ever touches `document.querySelector` once (the sheet body) and
|
||||
* then `body.querySelectorAll` / `body.querySelector` against three fixed
|
||||
* selectors, so we drive it with a hand-rolled fake `document` that answers
|
||||
* exactly those selectors — no DOM engine, matching the repo's node-only test
|
||||
* convention (cf. the SSR-string tests elsewhere).
|
||||
*/
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { sheetBodyHasUnsavedInput } from "@/components/dashboard/quick-entry-sheets";
|
||||
|
||||
const BODY_SELECTOR = '[data-slot="responsive-sheet-body"]';
|
||||
const MOOD_RADIO_SELECTOR = '[role="radio"][aria-checked="true"]';
|
||||
const OPEN_SELECT_SELECTOR = '[role="combobox"][data-state="open"]';
|
||||
|
||||
interface FakeField {
|
||||
type: string;
|
||||
value?: string;
|
||||
disabled?: boolean;
|
||||
checked?: boolean;
|
||||
defaultChecked?: boolean;
|
||||
}
|
||||
|
||||
interface SheetFixture {
|
||||
fields?: FakeField[];
|
||||
moodRadioSelected?: boolean;
|
||||
openSelect?: boolean;
|
||||
}
|
||||
|
||||
/** Install a fake `document` whose queries answer the given fixture. */
|
||||
function installFakeDocument(fixture: SheetFixture | null): void {
|
||||
const body = fixture
|
||||
? {
|
||||
querySelectorAll: (sel: string) =>
|
||||
sel === "input, textarea" ? (fixture.fields ?? []) : [],
|
||||
querySelector: (sel: string) => {
|
||||
if (sel === MOOD_RADIO_SELECTOR)
|
||||
return fixture.moodRadioSelected ? {} : null;
|
||||
if (sel === OPEN_SELECT_SELECTOR)
|
||||
return fixture.openSelect ? {} : null;
|
||||
return null;
|
||||
},
|
||||
}
|
||||
: null;
|
||||
|
||||
(globalThis as { document?: unknown }).document = {
|
||||
querySelector: (sel: string) => (sel === BODY_SELECTOR ? body : null),
|
||||
};
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
delete (globalThis as { document?: unknown }).document;
|
||||
});
|
||||
|
||||
describe("sheetBodyHasUnsavedInput — mood radio", () => {
|
||||
it("is clean when no mood radio is selected (pristine)", () => {
|
||||
installFakeDocument({ moodRadioSelected: false });
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(false);
|
||||
});
|
||||
|
||||
it("is dirty when a mood radio is selected", () => {
|
||||
installFakeDocument({ moodRadioSelected: true });
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sheetBodyHasUnsavedInput — Radix Select", () => {
|
||||
it("is clean for a closed Select on its default value (no over-trigger)", () => {
|
||||
// A measurement-type / medication Select mounts with a non-placeholder
|
||||
// DEFAULT and is closed — a pristine sheet must NOT prompt the confirm.
|
||||
installFakeDocument({ openSelect: false });
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(false);
|
||||
});
|
||||
|
||||
it("is dirty while a Select dropdown is open (mid-interaction)", () => {
|
||||
installFakeDocument({ openSelect: true });
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sheetBodyHasUnsavedInput — text fields still honoured", () => {
|
||||
it("is clean for an empty pristine sheet", () => {
|
||||
installFakeDocument({
|
||||
fields: [
|
||||
{ type: "text", value: "" },
|
||||
{ type: "textarea", value: "" },
|
||||
{ type: "datetime-local", value: "2026-06-04T08:00" },
|
||||
],
|
||||
});
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(false);
|
||||
});
|
||||
|
||||
it("is dirty when a text input carries a value", () => {
|
||||
installFakeDocument({ fields: [{ type: "text", value: "75.5" }] });
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(true);
|
||||
});
|
||||
|
||||
it("ignores a prefilled date/time picker (v1.11.4 H1)", () => {
|
||||
installFakeDocument({
|
||||
fields: [{ type: "datetime-local", value: "2026-06-04T08:00" }],
|
||||
});
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(false);
|
||||
});
|
||||
|
||||
it("ignores a disabled field carrying a value", () => {
|
||||
installFakeDocument({
|
||||
fields: [{ type: "text", value: "ignored", disabled: true }],
|
||||
});
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(false);
|
||||
});
|
||||
|
||||
it("is dirty for a native checkbox toggled off its default", () => {
|
||||
installFakeDocument({
|
||||
fields: [{ type: "checkbox", checked: true, defaultChecked: false }],
|
||||
});
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sheetBodyHasUnsavedInput — no sheet body", () => {
|
||||
it("is clean when no sheet body is mounted", () => {
|
||||
installFakeDocument(null);
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(false);
|
||||
});
|
||||
|
||||
it("is clean when document is undefined (SSR)", () => {
|
||||
delete (globalThis as { document?: unknown }).document;
|
||||
expect(sheetBodyHasUnsavedInput()).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -31,7 +31,10 @@ import {
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { PageHeader } from "@/components/ui/page-header";
|
||||
import { PullToRefreshIndicator } from "@/components/ui/pull-to-refresh-indicator";
|
||||
import { ResponsiveSheet } from "@/components/ui/responsive-sheet";
|
||||
import {
|
||||
ResponsiveSheet,
|
||||
sheetBodyHasUnsavedInput,
|
||||
} from "@/components/ui/responsive-sheet";
|
||||
import { useAuth } from "@/hooks/use-auth";
|
||||
import { useMounted } from "@/hooks/use-mounted";
|
||||
import { useRecordCapabilities } from "@/hooks/use-record-capabilities";
|
||||
@@ -218,6 +221,7 @@ export default function LabsPage() {
|
||||
<ResponsiveSheet
|
||||
open={dialogOpen}
|
||||
onOpenChange={setDialogOpen}
|
||||
dirty={sheetBodyHasUnsavedInput}
|
||||
title={t("labs.addResult")}
|
||||
description={t("labs.addDescription")}
|
||||
footer={
|
||||
|
||||
@@ -16,7 +16,10 @@ import { Button } from "@/components/ui/button";
|
||||
import { PageHeader } from "@/components/ui/page-header";
|
||||
import { PageAuthGate } from "@/components/ui/page-auth-gate";
|
||||
import { PullToRefreshIndicator } from "@/components/ui/pull-to-refresh-indicator";
|
||||
import { ResponsiveSheet } from "@/components/ui/responsive-sheet";
|
||||
import {
|
||||
ResponsiveSheet,
|
||||
sheetBodyHasUnsavedInput,
|
||||
} from "@/components/ui/responsive-sheet";
|
||||
import { RevealWhenSettled } from "@/components/ui/reveal-when-settled";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { Plus } from "lucide-react";
|
||||
@@ -179,6 +182,7 @@ export default function MeasurementsPage() {
|
||||
refuses. */}
|
||||
<ResponsiveSheet
|
||||
open={dialogOpen && canAddMeasurement}
|
||||
dirty={sheetBodyHasUnsavedInput}
|
||||
onOpenChange={(open) => {
|
||||
setDialogOpen(open);
|
||||
if (!open) {
|
||||
|
||||
@@ -13,7 +13,10 @@ import { Button } from "@/components/ui/button";
|
||||
import { PageHeader } from "@/components/ui/page-header";
|
||||
import { PageAuthGate } from "@/components/ui/page-auth-gate";
|
||||
import { PullToRefreshIndicator } from "@/components/ui/pull-to-refresh-indicator";
|
||||
import { ResponsiveSheet } from "@/components/ui/responsive-sheet";
|
||||
import {
|
||||
ResponsiveSheet,
|
||||
sheetBodyHasUnsavedInput,
|
||||
} from "@/components/ui/responsive-sheet";
|
||||
import { ArrowRight, Plus, Wrench } from "lucide-react";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
@@ -177,6 +180,7 @@ export default function MoodPageClient() {
|
||||
<ResponsiveSheet
|
||||
open={sheetOpen}
|
||||
onOpenChange={setSheetOpen}
|
||||
dirty={sheetBodyHasUnsavedInput}
|
||||
title={t("dashboard.quickAddMood")}
|
||||
footer={<div ref={setFooterEl} className="flex w-full" />}
|
||||
>
|
||||
|
||||
Reference in new issue
Block a user