@photonhealth/react 2.0.0-rc.1 → 2.0.0-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -9
- package/dist/adapters/tailwind.d.ts.map +1 -1
- package/dist/adapters/tailwind.js +7 -4
- package/dist/adapters/tailwind.js.map +1 -1
- package/dist/analytics/PhotonAnalyticsProvider.d.ts +19 -0
- package/dist/analytics/PhotonAnalyticsProvider.d.ts.map +1 -0
- package/dist/analytics/PhotonAnalyticsProvider.js +76 -0
- package/dist/analytics/PhotonAnalyticsProvider.js.map +1 -0
- package/dist/analytics/cardLifecycle.d.ts +10 -0
- package/dist/analytics/cardLifecycle.d.ts.map +1 -0
- package/dist/analytics/cardLifecycle.js +43 -0
- package/dist/analytics/cardLifecycle.js.map +1 -0
- package/dist/analytics/fieldInteraction.d.ts +18 -0
- package/dist/analytics/fieldInteraction.d.ts.map +1 -0
- package/dist/analytics/fieldInteraction.js +102 -0
- package/dist/analytics/fieldInteraction.js.map +1 -0
- package/dist/analytics/lifecycleOutcomes.d.ts +8 -0
- package/dist/analytics/lifecycleOutcomes.d.ts.map +1 -0
- package/dist/analytics/lifecycleOutcomes.js +16 -0
- package/dist/analytics/lifecycleOutcomes.js.map +1 -0
- package/dist/analytics/tracker.d.ts +19 -0
- package/dist/analytics/tracker.d.ts.map +1 -0
- package/dist/analytics/tracker.js +41 -0
- package/dist/analytics/tracker.js.map +1 -0
- package/dist/analytics/viewed.d.ts +11 -0
- package/dist/analytics/viewed.d.ts.map +1 -0
- package/dist/analytics/viewed.js +16 -0
- package/dist/analytics/viewed.js.map +1 -0
- package/dist/components/PhotonOrder.d.ts +18 -18
- package/dist/components/PhotonOrder.d.ts.map +1 -1
- package/dist/components/PhotonOrder.js +34 -19
- package/dist/components/PhotonOrder.js.map +1 -1
- package/dist/components/PhotonPatient.d.ts +7 -35
- package/dist/components/PhotonPatient.d.ts.map +1 -1
- package/dist/components/PhotonPatient.js +6 -78
- package/dist/components/PhotonPatient.js.map +1 -1
- package/dist/components/PhotonPrescription.d.ts +15 -26
- package/dist/components/PhotonPrescription.d.ts.map +1 -1
- package/dist/components/PhotonPrescription.js +6 -360
- package/dist/components/PhotonPrescription.js.map +1 -1
- package/dist/components/format.d.ts +2 -2
- package/dist/components/format.js +2 -2
- package/dist/components/index.d.ts +1 -1
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +1 -1
- package/dist/components/index.js.map +1 -1
- package/dist/components/open.d.ts +11 -13
- package/dist/components/open.d.ts.map +1 -1
- package/dist/components/open.js +7 -2
- package/dist/components/open.js.map +1 -1
- package/dist/components/order/DraftOrderPrompt.d.ts +18 -0
- package/dist/components/order/DraftOrderPrompt.d.ts.map +1 -0
- package/dist/components/order/DraftOrderPrompt.js +26 -0
- package/dist/components/order/DraftOrderPrompt.js.map +1 -0
- package/dist/components/patient/PatientCard/PatientCard.d.ts +30 -0
- package/dist/components/patient/PatientCard/PatientCard.d.ts.map +1 -0
- package/dist/components/patient/PatientCard/PatientCard.js +101 -0
- package/dist/components/patient/PatientCard/PatientCard.js.map +1 -0
- package/dist/components/patient/PatientCard/usePatientToShow.d.ts +22 -0
- package/dist/components/patient/PatientCard/usePatientToShow.d.ts.map +1 -0
- package/dist/components/patient/PatientCard/usePatientToShow.js +40 -0
- package/dist/components/patient/PatientCard/usePatientToShow.js.map +1 -0
- package/dist/components/patient/PatientIntakeForm.d.ts +16 -0
- package/dist/components/patient/PatientIntakeForm.d.ts.map +1 -0
- package/dist/components/patient/PatientIntakeForm.js +114 -0
- package/dist/components/patient/PatientIntakeForm.js.map +1 -0
- package/dist/components/patient/PatientSearch.d.ts +12 -0
- package/dist/components/patient/PatientSearch.d.ts.map +1 -0
- package/dist/components/patient/PatientSearch.js +148 -0
- package/dist/components/patient/PatientSearch.js.map +1 -0
- package/dist/components/patient/{Benefits.d.ts → PatientSummary/BenefitsInputForm.d.ts} +2 -2
- package/dist/components/patient/PatientSummary/BenefitsInputForm.d.ts.map +1 -0
- package/dist/components/patient/{Benefits.js → PatientSummary/BenefitsInputForm.js} +6 -5
- package/dist/components/patient/PatientSummary/BenefitsInputForm.js.map +1 -0
- package/dist/components/patient/{Clinical.d.ts → PatientSummary/ClinicalInputForm.d.ts} +15 -16
- package/dist/components/patient/PatientSummary/ClinicalInputForm.d.ts.map +1 -0
- package/dist/components/patient/{Clinical.js → PatientSummary/ClinicalInputForm.js} +29 -28
- package/dist/components/patient/PatientSummary/ClinicalInputForm.js.map +1 -0
- package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts +5 -0
- package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts.map +1 -0
- package/dist/components/patient/PatientSummary/CollapsedSummary.js +30 -0
- package/dist/components/patient/PatientSummary/CollapsedSummary.js.map +1 -0
- package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts +10 -0
- package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts.map +1 -0
- package/dist/components/patient/PatientSummary/PatientDetailsForm.js +65 -0
- package/dist/components/patient/PatientSummary/PatientDetailsForm.js.map +1 -0
- package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts +53 -0
- package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts.map +1 -0
- package/dist/components/patient/PatientSummary/PatientDetailsSchema.js +78 -0
- package/dist/components/patient/PatientSummary/PatientDetailsSchema.js.map +1 -0
- package/dist/components/patient/PatientSummary/PatientSummary.d.ts +46 -0
- package/dist/components/patient/PatientSummary/PatientSummary.d.ts.map +1 -0
- package/dist/components/patient/PatientSummary/PatientSummary.js +57 -0
- package/dist/components/patient/PatientSummary/PatientSummary.js.map +1 -0
- package/dist/components/patient/PatientSummary/SummaryHeader.d.ts +19 -0
- package/dist/components/patient/PatientSummary/SummaryHeader.d.ts.map +1 -0
- package/dist/components/patient/PatientSummary/SummaryHeader.js +22 -0
- package/dist/components/patient/PatientSummary/SummaryHeader.js.map +1 -0
- package/dist/components/patient/fields.d.ts +7 -26
- package/dist/components/patient/fields.d.ts.map +1 -1
- package/dist/components/patient/fields.js +11 -52
- package/dist/components/patient/fields.js.map +1 -1
- package/dist/components/patient/parsePatientSearchQuery.d.ts +16 -0
- package/dist/components/patient/parsePatientSearchQuery.d.ts.map +1 -0
- package/dist/components/patient/{query.js → parsePatientSearchQuery.js} +3 -10
- package/dist/components/patient/parsePatientSearchQuery.js.map +1 -0
- package/dist/components/prescription/PrescriptionCard.d.ts +51 -0
- package/dist/components/prescription/PrescriptionCard.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionCard.js +186 -0
- package/dist/components/prescription/PrescriptionCard.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts +15 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js +18 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts +23 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js +42 -0
- package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts +27 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js +169 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts +8 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js +12 -0
- package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts +11 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts.map +1 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js +25 -0
- package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js.map +1 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts +50 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts.map +1 -0
- package/dist/components/{TreatmentPicker.js → prescription/TreatmentSearch/TreatmentPicker.js} +113 -105
- package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js.map +1 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts +28 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts.map +1 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js +146 -0
- package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js.map +1 -0
- package/dist/components/schemaErrors.d.ts +13 -0
- package/dist/components/schemaErrors.d.ts.map +1 -0
- package/dist/components/schemaErrors.js +25 -0
- package/dist/components/schemaErrors.js.map +1 -0
- package/dist/components/search.d.ts +1 -1
- package/dist/components/search.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/meta/version.d.ts +5 -0
- package/dist/meta/version.d.ts.map +1 -0
- package/dist/meta/version.js +5 -0
- package/dist/meta/version.js.map +1 -0
- package/dist/primitives/PhotonProvider.d.ts +3 -1
- package/dist/primitives/PhotonProvider.d.ts.map +1 -1
- package/dist/primitives/PhotonProvider.js +23 -11
- package/dist/primitives/PhotonProvider.js.map +1 -1
- package/dist/primitives/defaults.d.ts.map +1 -1
- package/dist/primitives/defaults.js +6 -3
- package/dist/primitives/defaults.js.map +1 -1
- package/dist/primitives/photonContext.d.ts +3 -0
- package/dist/primitives/photonContext.d.ts.map +1 -0
- package/dist/primitives/photonContext.js +3 -0
- package/dist/primitives/photonContext.js.map +1 -0
- package/dist/primitives/types.d.ts +15 -3
- package/dist/primitives/types.d.ts.map +1 -1
- package/dist/primitives/types.js.map +1 -1
- package/dist/primitives/useProvidedPhoton.d.ts +8 -0
- package/dist/primitives/useProvidedPhoton.d.ts.map +1 -0
- package/dist/primitives/useProvidedPhoton.js +8 -0
- package/dist/primitives/useProvidedPhoton.js.map +1 -0
- package/dist/styles.css +45 -3
- package/dist/test-utils/contract.d.ts.map +1 -1
- package/dist/test-utils/contract.js +23 -2
- package/dist/test-utils/contract.js.map +1 -1
- package/package.json +8 -3
- package/src/adapters/tailwind.tsx +52 -24
- package/src/analytics/PhotonAnalyticsProvider.test.tsx +190 -0
- package/src/analytics/PhotonAnalyticsProvider.tsx +151 -0
- package/src/analytics/cardLifecycle.test.tsx +272 -0
- package/src/analytics/cardLifecycle.ts +44 -0
- package/src/analytics/fieldInteraction.test.tsx +186 -0
- package/src/analytics/fieldInteraction.tsx +148 -0
- package/src/analytics/lifecycleOutcomes.test.ts +78 -0
- package/src/analytics/lifecycleOutcomes.ts +29 -0
- package/src/analytics/tracker.test.ts +52 -0
- package/src/analytics/tracker.ts +49 -0
- package/src/analytics/usePhotonAnalytics.test.tsx +106 -0
- package/src/analytics/viewed.ts +27 -0
- package/src/components/PhotonOrder.test.tsx +394 -0
- package/src/components/PhotonOrder.tsx +79 -53
- package/src/components/PhotonPatient.tsx +15 -166
- package/src/components/PhotonPrescription.test.tsx +120 -0
- package/src/components/PhotonPrescription.tsx +23 -583
- package/src/components/ScreeningAlerts.test.tsx +81 -0
- package/src/components/format.ts +2 -2
- package/src/components/index.ts +1 -1
- package/src/components/open.ts +13 -16
- package/src/components/order/DraftOrderPrompt.test.tsx +77 -0
- package/src/components/order/DraftOrderPrompt.tsx +81 -0
- package/src/components/patient/PatientCard/PatientCard.test.tsx +255 -0
- package/src/components/patient/PatientCard/PatientCard.tsx +177 -0
- package/src/components/patient/PatientCard/usePatientToShow.ts +44 -0
- package/src/components/patient/PatientIntakeForm.test.tsx +262 -0
- package/src/components/patient/PatientIntakeForm.tsx +222 -0
- package/src/components/patient/PatientSearch.test.tsx +204 -0
- package/src/components/patient/PatientSearch.tsx +221 -0
- package/src/components/patient/PatientSummary/BenefitsInputForm.test.tsx +164 -0
- package/src/components/patient/{Benefits.tsx → PatientSummary/BenefitsInputForm.tsx} +24 -16
- package/src/components/patient/PatientSummary/ClinicalInputForm.test.tsx +247 -0
- package/src/components/patient/{Clinical.tsx → PatientSummary/ClinicalInputForm.tsx} +53 -48
- package/src/components/patient/PatientSummary/CollapsedSummary.test.tsx +90 -0
- package/src/components/patient/PatientSummary/CollapsedSummary.tsx +46 -0
- package/src/components/patient/PatientSummary/PatientDetailsForm.test.tsx +201 -0
- package/src/components/patient/PatientSummary/PatientDetailsForm.tsx +320 -0
- package/src/components/patient/PatientSummary/PatientDetailsSchema.test.ts +296 -0
- package/src/components/patient/PatientSummary/PatientDetailsSchema.ts +95 -0
- package/src/components/patient/PatientSummary/PatientSummary.test.tsx +194 -0
- package/src/components/patient/PatientSummary/PatientSummary.tsx +225 -0
- package/src/components/patient/PatientSummary/SummaryHeader.test.tsx +183 -0
- package/src/components/patient/PatientSummary/SummaryHeader.tsx +85 -0
- package/src/components/patient/fields.tsx +12 -124
- package/src/components/patient/parsePatientSearchQuery.test.ts +107 -0
- package/src/components/patient/{query.ts → parsePatientSearchQuery.ts} +4 -15
- package/src/components/prescription/PrescriptionCard.test.tsx +247 -0
- package/src/components/prescription/PrescriptionCard.tsx +361 -0
- package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.test.tsx +82 -0
- package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.tsx +41 -0
- package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.test.tsx +206 -0
- package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.tsx +99 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.test.tsx +453 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.tsx +238 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.test.ts +63 -0
- package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.ts +15 -0
- package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.test.tsx +72 -0
- package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.tsx +41 -0
- package/src/components/prescription/TreatmentSearch/TreatmentPicker.tsx +375 -0
- package/src/components/prescription/TreatmentSearch/TreatmentSearch.test.tsx +695 -0
- package/src/components/prescription/TreatmentSearch/TreatmentSearch.tsx +220 -0
- package/src/components/schemaErrors.test.ts +49 -0
- package/src/components/schemaErrors.ts +34 -0
- package/src/components/search.tsx +1 -1
- package/src/index.ts +2 -0
- package/src/meta/version.test.ts +11 -0
- package/src/meta/version.ts +4 -0
- package/src/primitives/PhotonProvider.tsx +30 -15
- package/src/primitives/defaults.tsx +44 -21
- package/src/primitives/photonContext.ts +4 -0
- package/src/primitives/types.ts +15 -3
- package/src/primitives/useProvidedPhoton.ts +21 -0
- package/src/styles.css +45 -3
- package/src/test-utils/contract.tsx +45 -2
- package/dist/components/TreatmentPicker.d.ts +0 -45
- package/dist/components/TreatmentPicker.d.ts.map +0 -1
- package/dist/components/TreatmentPicker.js.map +0 -1
- package/dist/components/patient/Benefits.d.ts.map +0 -1
- package/dist/components/patient/Benefits.js.map +0 -1
- package/dist/components/patient/Clinical.d.ts.map +0 -1
- package/dist/components/patient/Clinical.js.map +0 -1
- package/dist/components/patient/IntakeForm.d.ts +0 -10
- package/dist/components/patient/IntakeForm.d.ts.map +0 -1
- package/dist/components/patient/IntakeForm.js +0 -324
- package/dist/components/patient/IntakeForm.js.map +0 -1
- package/dist/components/patient/Summary.d.ts +0 -48
- package/dist/components/patient/Summary.d.ts.map +0 -1
- package/dist/components/patient/Summary.js +0 -80
- package/dist/components/patient/Summary.js.map +0 -1
- package/dist/components/patient/query.d.ts +0 -23
- package/dist/components/patient/query.d.ts.map +0 -1
- package/dist/components/patient/query.js.map +0 -1
- package/src/components/TreatmentPicker.tsx +0 -350
- package/src/components/patient/IntakeForm.tsx +0 -532
- package/src/components/patient/Summary.tsx +0 -310
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { schemaErrors } from "../../schemaErrors.ts";
|
|
3
|
+
import { PrescriptionDetailsSchema, type PrescriptionDetails } from "./PrescriptionDetailsSchema.ts";
|
|
4
|
+
|
|
5
|
+
const errorsOf = (inputs: PrescriptionDetails) => schemaErrors(PrescriptionDetailsSchema, inputs);
|
|
6
|
+
|
|
7
|
+
const valid = { instructions: "Take 1 capsule by mouth 3 times per day", quantity: "30", daysSupply: "10" };
|
|
8
|
+
|
|
9
|
+
describe("PrescriptionDetailsSchema", () => {
|
|
10
|
+
it("accepts directions, quantity and days supply that are filled in", () => {
|
|
11
|
+
expect(PrescriptionDetailsSchema.safeParse(valid).success).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("parses values by trimming directions, converting text to numbers", () => {
|
|
15
|
+
expect(PrescriptionDetailsSchema.parse({ instructions: " test ", quantity: " 2.5 ", daysSupply: "7" })).toEqual({
|
|
16
|
+
instructions: "test",
|
|
17
|
+
quantity: 2.5,
|
|
18
|
+
daysSupply: 7,
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("PrescriptionDetailsSchema errors", () => {
|
|
24
|
+
it("has no errors when every field is valid", () => {
|
|
25
|
+
expect(errorsOf(valid)).toEqual({});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("says each empty field is required", () => {
|
|
29
|
+
expect(errorsOf({ instructions: "", quantity: "", daysSupply: "" })).toEqual({
|
|
30
|
+
instructions: "Required",
|
|
31
|
+
quantity: "Required",
|
|
32
|
+
daysSupply: "Required",
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it.each([
|
|
37
|
+
["instructions", " "],
|
|
38
|
+
["quantity", " "],
|
|
39
|
+
["daysSupply", " "],
|
|
40
|
+
] as const)("counts %s of only spaces as empty", (field, value) => {
|
|
41
|
+
expect(errorsOf({ ...valid, [field]: value })).toEqual({ [field]: "Required" });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it.each([
|
|
45
|
+
["quantity", "abc", "Must be a number"],
|
|
46
|
+
["quantity", "12abc", "Must be a number"],
|
|
47
|
+
["quantity", "0", "Must be greater than 0"],
|
|
48
|
+
["quantity", "-5", "Must be greater than 0"],
|
|
49
|
+
["daysSupply", "abc", "Must be a number"],
|
|
50
|
+
["daysSupply", "0", "Must be greater than 0"],
|
|
51
|
+
["daysSupply", "-1", "Must be greater than 0"],
|
|
52
|
+
] as const)("says %s of %j: %s", (field, value, message) => {
|
|
53
|
+
expect(errorsOf({ ...valid, [field]: value })).toEqual({ [field]: message });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it.each([
|
|
57
|
+
["quantity", "2.5"],
|
|
58
|
+
["quantity", "0.5"],
|
|
59
|
+
["daysSupply", "90"],
|
|
60
|
+
] as const)("accepts %s of %j", (field, value) => {
|
|
61
|
+
expect(errorsOf({ ...valid, [field]: value })).toEqual({});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
const textToNumber = z
|
|
4
|
+
.string()
|
|
5
|
+
.trim()
|
|
6
|
+
.min(1, "Required")
|
|
7
|
+
.pipe(z.coerce.number<string>({ error: "Must be a number" }).gt(0, "Must be greater than 0"));
|
|
8
|
+
|
|
9
|
+
export const PrescriptionDetailsSchema = z.object({
|
|
10
|
+
instructions: z.string().trim().min(1, "Required"),
|
|
11
|
+
quantity: textToNumber,
|
|
12
|
+
daysSupply: textToNumber,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export type PrescriptionDetails = z.input<typeof PrescriptionDetailsSchema>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { PrescriptionTemplate } from "@photonhealth/client";
|
|
2
|
+
import { fireEvent, render, screen, within } from "@testing-library/react";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { TemplatePicker, type TemplatePickerProps } from "./TemplatePicker.tsx";
|
|
5
|
+
|
|
6
|
+
const sig = (instructions: string): PrescriptionTemplate => ({
|
|
7
|
+
id: null,
|
|
8
|
+
source: "RECOMMENDED",
|
|
9
|
+
name: null,
|
|
10
|
+
instructions,
|
|
11
|
+
dispense: null,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const options = () => screen.queryByRole("list", { name: "Templates" });
|
|
15
|
+
|
|
16
|
+
const renderPicker = (props: Partial<TemplatePickerProps> = {}) => {
|
|
17
|
+
const all: TemplatePickerProps = { templates: [], hasDirections: false, onChoose: vi.fn(), ...props };
|
|
18
|
+
render(<TemplatePicker {...all} />);
|
|
19
|
+
return all;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
describe("TemplatePicker", () => {
|
|
23
|
+
it("renders an expanded list for a new draft", () => {
|
|
24
|
+
renderPicker({ templates: [sig("Take 1 capsule by mouth daily"), sig("Take 2 capsules by mouth daily")] });
|
|
25
|
+
const offered = within(options()!)
|
|
26
|
+
.getAllByRole("button")
|
|
27
|
+
.map((button) => button.textContent);
|
|
28
|
+
expect(offered).toEqual(["Take 1 capsule by mouth daily", "Take 2 capsules by mouth daily"]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("names a catalog template, with its directions under the name", () => {
|
|
32
|
+
renderPicker({
|
|
33
|
+
templates: [
|
|
34
|
+
{ ...sig("Take 1 capsule by mouth twice daily"), id: "tmp_1", source: "ORG_CATALOG", name: "Sinusitis" },
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
const option = within(options()!).getByRole("button");
|
|
38
|
+
expect(option.textContent).toContain("Sinusitis");
|
|
39
|
+
expect(option.textContent).toContain("Take 1 capsule by mouth twice daily");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("renders a collapsed list when the prescription already has directions", () => {
|
|
43
|
+
renderPicker({ hasDirections: true, templates: [sig("Take 1 capsule by mouth daily")] });
|
|
44
|
+
expect(screen.getByRole("button", { name: "Templates" }).getAttribute("aria-expanded")).toBe("false");
|
|
45
|
+
expect(options()).toBeNull();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("renders nothing when there are no templates", () => {
|
|
49
|
+
renderPicker();
|
|
50
|
+
expect(screen.queryByRole("button", { name: "Templates" })).toBeNull();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("hands off the template I choose", () => {
|
|
54
|
+
const chosen = sig("Take 2 capsules by mouth daily");
|
|
55
|
+
const { onChoose } = renderPicker({ templates: [sig("Take 1 capsule by mouth daily"), chosen] });
|
|
56
|
+
fireEvent.click(screen.getByRole("button", { name: "Take 2 capsules by mouth daily" }));
|
|
57
|
+
expect(onChoose).toHaveBeenCalledExactlyOnceWith(chosen);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("collapses the list once I choose one", () => {
|
|
61
|
+
renderPicker({ templates: [sig("Take 1 capsule by mouth daily")] });
|
|
62
|
+
fireEvent.click(screen.getByRole("button", { name: "Take 1 capsule by mouth daily" }));
|
|
63
|
+
expect(options()).toBeNull();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("expands the list when the toggle is clicked", () => {
|
|
67
|
+
renderPicker({ templates: [sig("Take 1 capsule by mouth daily")] });
|
|
68
|
+
fireEvent.click(screen.getByRole("button", { name: "Take 1 capsule by mouth daily" }));
|
|
69
|
+
fireEvent.click(screen.getByRole("button", { name: "Templates" }));
|
|
70
|
+
expect(options()).not.toBeNull();
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { PrescriptionTemplate } from "@photonhealth/client";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { useComponents } from "../../../primitives/index.ts";
|
|
4
|
+
|
|
5
|
+
/** The templates a prescription's directions and dispense can be filled from. */
|
|
6
|
+
export interface TemplatePickerProps {
|
|
7
|
+
/** Each has a name, directions, or both. */
|
|
8
|
+
templates: readonly PrescriptionTemplate[];
|
|
9
|
+
/** The prescription already has directions, so the templates start closed. */
|
|
10
|
+
hasDirections: boolean;
|
|
11
|
+
onChoose: (template: PrescriptionTemplate) => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function TemplatePicker({ templates, hasDirections, onChoose }: TemplatePickerProps) {
|
|
15
|
+
const { ChoiceList, Disclosure } = useComponents();
|
|
16
|
+
// Each new set of templates opens, unless someone already entered directions (e.g. an existing draft's).
|
|
17
|
+
// Choosing one closes them, and the toggle opens them again.
|
|
18
|
+
const [open, setOpen] = useState(!hasDirections);
|
|
19
|
+
const [shownTemplates, setShownTemplates] = useState(templates);
|
|
20
|
+
if (templates !== shownTemplates) {
|
|
21
|
+
setShownTemplates(templates);
|
|
22
|
+
setOpen(!hasDirections);
|
|
23
|
+
}
|
|
24
|
+
if (templates.length === 0) return null;
|
|
25
|
+
return (
|
|
26
|
+
<Disclosure summary="Templates" open={open} onOpenChange={setOpen}>
|
|
27
|
+
<ChoiceList
|
|
28
|
+
label="Templates"
|
|
29
|
+
choices={templates.map((template, index) => ({
|
|
30
|
+
key: String(index),
|
|
31
|
+
label: template.name ?? template.instructions ?? "",
|
|
32
|
+
description: (template.name && template.instructions) ?? undefined,
|
|
33
|
+
}))}
|
|
34
|
+
onChoose={(key) => {
|
|
35
|
+
setOpen(false);
|
|
36
|
+
onChoose(templates[Number(key)]!);
|
|
37
|
+
}}
|
|
38
|
+
/>
|
|
39
|
+
</Disclosure>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { useComponents } from "../../../primitives/index.ts";
|
|
2
|
+
import type { Change, ChangeOption } from "@photonhealth/client";
|
|
3
|
+
import { describeTreatmentDrug, groupTreatmentOptions, orderedOptions, strengthsOf } from "../../display.ts";
|
|
4
|
+
import { afterPress, digitKey, edge, SHOWN, took, typesBack, vertical, type SearchHandle } from "../../search.tsx";
|
|
5
|
+
import { useEffect, useRef, useState, type KeyboardEvent } from "react";
|
|
6
|
+
import { useFieldInteractions } from "../../../analytics/fieldInteraction.tsx";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The drug search's results (search.tsx): the server's drug choices, grouped the way a prescriber decides —
|
|
10
|
+
* drug, then form, then strength (instead of one long list of product names), from the parts each option's
|
|
11
|
+
* treatment carries (display.ts). Treatment works like a select: the card owns which drug is picked (`drug`, its
|
|
12
|
+
* name — shown in the field) and whether the list is open (`listOpen`); this draws one or the other.
|
|
13
|
+
*
|
|
14
|
+
* - The list (`listOpen`): a row per drug, its one form or one strength said under its name, else how many
|
|
15
|
+
* ("2 forms · 3 strengths"). Picking a row picks the drug (`onDrug`) — or, for a drug that's one product, chooses
|
|
16
|
+
* that product (no chevron).
|
|
17
|
+
* - The drug picked (`drug`, the list closed): its forms and strengths as chips under the field — never inside its
|
|
18
|
+
* row. Nothing is picked for the prescriber: no chip starts pressed, and they're picked in either order. A pick greys out what doesn't go with it in the other group (a
|
|
19
|
+
* form: the strengths it doesn't come in; a strength: the forms that don't come in it); picking it again takes
|
|
20
|
+
* it back. A greyed chip can still be picked: it drops the other group's pick, and that's the prescriber's to
|
|
21
|
+
* pick again. The pick that leaves nothing more to pick chooses the product: the second chip — or the first,
|
|
22
|
+
* when it settles both (a form with one strength, a strength only one form comes in). While the choice saves
|
|
23
|
+
* (`chosen`), the chips stay as they were picked.
|
|
24
|
+
*
|
|
25
|
+
* Keys. In the list: as every search's (1–9 / 0 picks a drug; Option/Alt + the digit from Treatment), plus ↓ ↑ (or J
|
|
26
|
+
* K, or Tab / Shift+Tab — past the last / first, Tab leaves the list) move the highlight, Home / End (or g / G) to
|
|
27
|
+
* the first / last, D to the next one, Enter picks the one highlighted; the list is one tab stop. Picking a drug puts
|
|
28
|
+
* focus on its first chips; there, Tab moves between chips, F / S to the next form / strength chip, Enter (or Space)
|
|
29
|
+
* picks the one focused. Esc goes back to Treatment, from either. ← → leave the card (the card flow's: flow.ts). A
|
|
30
|
+
* few drugs for what's typed and Treatment puts focus on the first (`focus(true)`): typing carries on there until a
|
|
31
|
+
* key acts on the list (search.tsx). `handleRef` is how the Treatment field reaches the list.
|
|
32
|
+
*/
|
|
33
|
+
export interface TreatmentPickerHandle extends SearchHandle {
|
|
34
|
+
/** Enter from Treatment: pick the drug highlighted (the list open), else go to the picked drug's first chips. */
|
|
35
|
+
enter: () => void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type Chips = "form" | "strength";
|
|
39
|
+
type Drugs = ReturnType<typeof groupTreatmentOptions<ChangeOption>>;
|
|
40
|
+
|
|
41
|
+
export function TreatmentPicker({
|
|
42
|
+
change,
|
|
43
|
+
drug: drugName,
|
|
44
|
+
listOpen,
|
|
45
|
+
onDrug,
|
|
46
|
+
onChoose,
|
|
47
|
+
busy,
|
|
48
|
+
chosen,
|
|
49
|
+
handleRef,
|
|
50
|
+
onBack,
|
|
51
|
+
}: {
|
|
52
|
+
change: Change;
|
|
53
|
+
/** The drug picked, by name (null: none) — the card's; shown in Treatment. */
|
|
54
|
+
drug: string | null;
|
|
55
|
+
/** The list of drugs is open (else the picked drug's chips show). */
|
|
56
|
+
listOpen: boolean;
|
|
57
|
+
/** A drug was picked from the list: its name. */
|
|
58
|
+
onDrug: (name: string) => void;
|
|
59
|
+
onChoose?: (option: ChangeOption) => void;
|
|
60
|
+
busy?: boolean;
|
|
61
|
+
/** The product chosen, while its save runs: shown picked; nothing else can be picked. */
|
|
62
|
+
chosen?: ChangeOption | null;
|
|
63
|
+
handleRef?: { current: TreatmentPickerHandle | null };
|
|
64
|
+
/** Esc: back to the field that searched (Treatment). */
|
|
65
|
+
onBack?: () => void;
|
|
66
|
+
}) {
|
|
67
|
+
const { Button, OptionList, Stack, ToggleGroup } = useComponents();
|
|
68
|
+
const fieldInteraction = useFieldInteractions("add_prescription_form");
|
|
69
|
+
const typed = change.inputText;
|
|
70
|
+
const all = groupTreatmentOptions(orderedOptions({ options: change.options.filter((o) => o.path) }, typed));
|
|
71
|
+
const [showAll, setShowAll] = useState(false);
|
|
72
|
+
const drugs = showAll ? all : all.slice(0, SHOWN);
|
|
73
|
+
// The row the list's keys act on.
|
|
74
|
+
const [highlight, setHighlight] = useState(0);
|
|
75
|
+
const at = Math.min(highlight, Math.max(0, drugs.length - 1));
|
|
76
|
+
// The picked drug, and its form and strength as the prescriber picks them (null: not yet) — kept per drug, so
|
|
77
|
+
// picking another drug starts it empty.
|
|
78
|
+
const drug = all.find((d) => d.name === drugName) ?? null;
|
|
79
|
+
const [picks, setPicks] = useState<{ drug: string | null; form: number | null; strength: string | null }>(() =>
|
|
80
|
+
empty(null),
|
|
81
|
+
);
|
|
82
|
+
const pick = picks.drug === drugName ? picks : empty(drugName);
|
|
83
|
+
const setPick = (next: { form: number | null; strength: string | null }) => setPicks({ drug: drugName, ...next });
|
|
84
|
+
|
|
85
|
+
const root = useRef<HTMLDivElement>(null);
|
|
86
|
+
const list = () => root.current?.querySelector<HTMLElement>('[tabindex="0"]');
|
|
87
|
+
const chips = (group: Chips) => [
|
|
88
|
+
...(root.current?.querySelectorAll<HTMLElement>(`[data-chips="${group}"] button:not(:disabled)`) ?? []),
|
|
89
|
+
];
|
|
90
|
+
// Focus is on the list itself (not a row): the highlighted row has the outline.
|
|
91
|
+
const [onList, setOnList] = useState(false);
|
|
92
|
+
// Focus was put here for the prescriber, mid-typing: keys that type go back to Treatment (typesBack).
|
|
93
|
+
const typing = useRef(false);
|
|
94
|
+
// Where focus goes once the next render is in: the list, or a chip (the first that fits, once a drug is picked).
|
|
95
|
+
const focusNext = useRef<"list" | { group: Chips; at: number } | "chips" | null>(null);
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
// One tab stop for the list: Tab moves the highlight (onKeyDown), not into a row.
|
|
98
|
+
list()
|
|
99
|
+
?.querySelectorAll<HTMLElement>("button")
|
|
100
|
+
.forEach((button) => (button.tabIndex = -1));
|
|
101
|
+
const next = focusNext.current;
|
|
102
|
+
focusNext.current = null;
|
|
103
|
+
if (next === "list") list()?.focus();
|
|
104
|
+
else if (next === "chips") (chips("form")[0] ?? chips("strength")[0])?.focus();
|
|
105
|
+
else if (next) chips(next.group)[next.at]?.focus();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// What's shown picked: the prescriber's picks — or, while a choice saves, the product chosen.
|
|
109
|
+
const saving = chosen ? locate(all, chosen) : null;
|
|
110
|
+
const view = saving && all[saving.drug]?.name === drugName ? saving : pick;
|
|
111
|
+
// One form is the drug's form; with more, none until it's picked.
|
|
112
|
+
const formAt = drug?.forms.length === 1 ? 0 : view.form;
|
|
113
|
+
// The product a form comes in at a strength, if it does.
|
|
114
|
+
const productOf = (formIndex: number, strength: string) =>
|
|
115
|
+
drug?.forms[formIndex]?.strengths.find((s) => s.label === strength)?.option ?? null;
|
|
116
|
+
// The forms a strength could be: those that come in it — of the one picked, once it is.
|
|
117
|
+
const formsFor = (strength: string) =>
|
|
118
|
+
drug?.forms.flatMap((_, i) => ((formAt === null || i === formAt) && productOf(i, strength) ? [i] : [])) ?? [];
|
|
119
|
+
|
|
120
|
+
const formPicked = fieldInteraction("form", { isOptional: false, hasValue: formAt !== null });
|
|
121
|
+
const strengthPicked = fieldInteraction("strength", { isOptional: false, hasValue: view.strength !== null });
|
|
122
|
+
|
|
123
|
+
// Busy, or a choice saving: nothing can be picked (a saving choice still looks as picked, not disabled).
|
|
124
|
+
const disabled = (busy && !chosen) || !onChoose;
|
|
125
|
+
const locked = disabled || Boolean(chosen);
|
|
126
|
+
const choose = (option: ChangeOption) => !locked && onChoose?.(option);
|
|
127
|
+
// A drug with one product: its row is the choice.
|
|
128
|
+
const onlyOption = (i: number) => {
|
|
129
|
+
const forms = drugs[i]!.forms;
|
|
130
|
+
return forms.length === 1 && forms[0]!.strengths.length === 1 ? forms[0]!.strengths[0]!.option : null;
|
|
131
|
+
};
|
|
132
|
+
const pickDrug = (i: number) => {
|
|
133
|
+
if (locked || !drugs[i]) return;
|
|
134
|
+
const only = onlyOption(i);
|
|
135
|
+
onDrug(drugs[i]!.name);
|
|
136
|
+
if (only) choose(only);
|
|
137
|
+
else focusNext.current = "chips";
|
|
138
|
+
};
|
|
139
|
+
// The first chip in a group that goes with the other group's pick (`form` / `strength`; null: nothing picked).
|
|
140
|
+
const firstFitting = (group: Chips, form: number | null, strength: string | null) => {
|
|
141
|
+
if (!drug) return 0;
|
|
142
|
+
const found =
|
|
143
|
+
group === "form"
|
|
144
|
+
? drug.forms.findIndex((_, i) => strength === null || productOf(i, strength))
|
|
145
|
+
: strengthsOf(drug).findIndex((s) => form === null || productOf(form, s));
|
|
146
|
+
return Math.max(0, found);
|
|
147
|
+
};
|
|
148
|
+
const pickForm = (i: number) => {
|
|
149
|
+
if (locked || !drug) return;
|
|
150
|
+
formPicked.onChange(pick.form !== i);
|
|
151
|
+
if (pick.form === i) {
|
|
152
|
+
setPick({ ...pick, form: null }); // picked again: taken back
|
|
153
|
+
focusNext.current = { group: "form", at: i };
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (pick.strength !== null && !productOf(i, pick.strength)) {
|
|
157
|
+
// Greyed (not in the strength picked): the strength goes, and it's the prescriber's to pick again.
|
|
158
|
+
setPick({ form: i, strength: null });
|
|
159
|
+
focusNext.current = { group: "strength", at: firstFitting("strength", i, null) };
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const strengths = drug.forms[i]!.strengths;
|
|
163
|
+
const product =
|
|
164
|
+
pick.strength !== null ? productOf(i, pick.strength) : strengths.length === 1 ? strengths[0]!.option : null;
|
|
165
|
+
setPick({ ...pick, form: i });
|
|
166
|
+
if (product) choose(product);
|
|
167
|
+
else focusNext.current = { group: "strength", at: firstFitting("strength", i, null) };
|
|
168
|
+
};
|
|
169
|
+
const pickStrength = (strength: string) => {
|
|
170
|
+
if (locked || !drug) return;
|
|
171
|
+
strengthPicked.onChange(pick.strength !== strength);
|
|
172
|
+
const index = strengthsOf(drug).indexOf(strength);
|
|
173
|
+
if (pick.strength === strength) {
|
|
174
|
+
setPick({ ...pick, strength: null }); // picked again: taken back
|
|
175
|
+
focusNext.current = { group: "strength", at: index };
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (drug.forms.length > 1 && pick.form !== null && !productOf(pick.form, strength)) {
|
|
179
|
+
// Greyed (the form picked doesn't come in it): the form goes, and it's the prescriber's to pick again.
|
|
180
|
+
setPick({ form: null, strength });
|
|
181
|
+
focusNext.current = { group: "form", at: firstFitting("form", null, strength) };
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const forms = formsFor(strength);
|
|
185
|
+
setPick({ ...pick, strength });
|
|
186
|
+
if (forms.length === 1) choose(productOf(forms[0]!, strength)!);
|
|
187
|
+
else focusNext.current = { group: "form", at: firstFitting("form", null, strength) };
|
|
188
|
+
};
|
|
189
|
+
const enter = () => {
|
|
190
|
+
if (chosen) return;
|
|
191
|
+
if (listOpen) return pickDrug(at);
|
|
192
|
+
(chips("form")[0] ?? chips("strength")[0])?.focus();
|
|
193
|
+
};
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
if (!handleRef) return;
|
|
196
|
+
handleRef.current = {
|
|
197
|
+
total: listOpen ? all.length : 0,
|
|
198
|
+
enter,
|
|
199
|
+
focus: (fromTyping) => {
|
|
200
|
+
typing.current = Boolean(fromTyping && onBack);
|
|
201
|
+
const target = listOpen ? list() : (chips("form")[0] ?? chips("strength")[0]);
|
|
202
|
+
target?.focus();
|
|
203
|
+
return Boolean(target);
|
|
204
|
+
},
|
|
205
|
+
pick: (index) => {
|
|
206
|
+
if (!listOpen || index >= drugs.length) return false;
|
|
207
|
+
pickDrug(index);
|
|
208
|
+
return true;
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
return () => {
|
|
212
|
+
handleRef.current = null;
|
|
213
|
+
};
|
|
214
|
+
});
|
|
215
|
+
// F / S: the next chip in that group after the one focused (the first, from anywhere else).
|
|
216
|
+
const nextChip = (group: Chips) => {
|
|
217
|
+
const buttons = chips(group);
|
|
218
|
+
if (!buttons.length) return false;
|
|
219
|
+
buttons[(buttons.indexOf(document.activeElement as HTMLElement) + 1) % buttons.length]!.focus();
|
|
220
|
+
return true;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const onListKeyDown = (e: KeyboardEvent<HTMLElement>) => {
|
|
224
|
+
if (typing.current && typesBack(e)) return onBack?.(); // not taken: the browser types it in Treatment
|
|
225
|
+
typing.current = false;
|
|
226
|
+
if (e.metaKey || e.ctrlKey) return;
|
|
227
|
+
const digit = digitKey(e);
|
|
228
|
+
if (e.altKey && digit < 0) return;
|
|
229
|
+
const move = (to: number) => {
|
|
230
|
+
setHighlight(to);
|
|
231
|
+
focusNext.current = "list";
|
|
232
|
+
};
|
|
233
|
+
if (digit >= 0 && digit < drugs.length) pickDrug(digit);
|
|
234
|
+
else if (e.key.toLowerCase() === "d") move((at + 1) % drugs.length);
|
|
235
|
+
else if (vertical(e)) move(Math.max(0, Math.min(drugs.length - 1, at + vertical(e))));
|
|
236
|
+
else if (edge(e)) move(edge(e) === "first" ? 0 : drugs.length - 1);
|
|
237
|
+
else if (e.key === "Tab" && !e.altKey) {
|
|
238
|
+
// The next / previous drug — past the last / first, on out of the list (the browser's Tab).
|
|
239
|
+
const to = at + (e.shiftKey ? -1 : 1);
|
|
240
|
+
if (to < 0 || to >= drugs.length) return;
|
|
241
|
+
move(to);
|
|
242
|
+
} else if (e.key === "Enter") pickDrug(at);
|
|
243
|
+
else if (e.key === "Escape" && onBack) onBack();
|
|
244
|
+
// A key that types and isn't one of the list's: back to Treatment, where the browser types it.
|
|
245
|
+
else if (typesBack(e)) return onBack?.();
|
|
246
|
+
else return;
|
|
247
|
+
took(e);
|
|
248
|
+
};
|
|
249
|
+
const onChipsKeyDown = (e: KeyboardEvent<HTMLElement>) => {
|
|
250
|
+
if (e.metaKey || e.ctrlKey || e.altKey) return;
|
|
251
|
+
const key = e.key.toLowerCase();
|
|
252
|
+
if (key === "f" || key === "s") {
|
|
253
|
+
if (!nextChip(key === "f" ? "form" : "strength")) return;
|
|
254
|
+
} else if (e.key === "Escape" && onBack) onBack();
|
|
255
|
+
else return;
|
|
256
|
+
took(e);
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const strengths = drug ? strengthsOf(drug) : [];
|
|
260
|
+
// The drug's strengths differ by form (the row can't say one): a form with one strength says it on its chip.
|
|
261
|
+
const strengthByForm = strengths.length > 1;
|
|
262
|
+
|
|
263
|
+
return (
|
|
264
|
+
<div
|
|
265
|
+
ref={root}
|
|
266
|
+
style={{ paddingBottom: "0.75em" }}
|
|
267
|
+
onFocus={(e) =>
|
|
268
|
+
setOnList((highlighted) => e.target === list() || (highlighted && Boolean(list()?.contains(e.target))))
|
|
269
|
+
}
|
|
270
|
+
onBlur={(e) => {
|
|
271
|
+
if (root.current?.contains(e.relatedTarget)) return;
|
|
272
|
+
afterPress(() => {
|
|
273
|
+
if (root.current?.contains(document.activeElement)) return;
|
|
274
|
+
setOnList(false);
|
|
275
|
+
typing.current = false;
|
|
276
|
+
});
|
|
277
|
+
}}
|
|
278
|
+
>
|
|
279
|
+
<Stack gap="3">
|
|
280
|
+
{listOpen && drugs.length > 0 && (
|
|
281
|
+
<OptionList
|
|
282
|
+
label="Pick a treatment"
|
|
283
|
+
disabled={disabled}
|
|
284
|
+
// The highlight shows only while the keys act on it (focus on the list).
|
|
285
|
+
activeKey={onList ? String(at) : null}
|
|
286
|
+
activeSelected={onList}
|
|
287
|
+
onKeyDown={onListKeyDown}
|
|
288
|
+
options={drugs.map((d, i) => {
|
|
289
|
+
const only = onlyOption(i);
|
|
290
|
+
return {
|
|
291
|
+
key: String(i),
|
|
292
|
+
label: d.name,
|
|
293
|
+
description: describeTreatmentDrug(d) ?? undefined,
|
|
294
|
+
// The row chooses the product, so it's named as the product.
|
|
295
|
+
ariaLabel: only?.displayName,
|
|
296
|
+
direct: Boolean(only),
|
|
297
|
+
loading: Boolean(chosen) && chosen === only,
|
|
298
|
+
};
|
|
299
|
+
})}
|
|
300
|
+
onSelect={(key) => pickDrug(Number(key))}
|
|
301
|
+
/>
|
|
302
|
+
)}
|
|
303
|
+
{listOpen && all.length > drugs.length && (
|
|
304
|
+
<div>
|
|
305
|
+
<Button variant="ghost" onClick={() => setShowAll(true)}>
|
|
306
|
+
Show {all.length - drugs.length} more
|
|
307
|
+
</Button>
|
|
308
|
+
</div>
|
|
309
|
+
)}
|
|
310
|
+
{!listOpen && drug && (
|
|
311
|
+
<div onKeyDown={onChipsKeyDown} style={{ display: "contents" }}>
|
|
312
|
+
{drug.forms.length > 1 && (
|
|
313
|
+
<div data-chips="form" onFocus={formPicked.onFocus} onBlur={formPicked.onBlur}>
|
|
314
|
+
<ToggleGroup
|
|
315
|
+
label="Form"
|
|
316
|
+
disabled={disabled}
|
|
317
|
+
value={formAt === null ? null : String(formAt)}
|
|
318
|
+
onChange={(value) => pickForm(Number(value))}
|
|
319
|
+
options={drug.forms.map((f, i) => {
|
|
320
|
+
const one = f.strengths.length === 1 ? f.strengths[0]! : null;
|
|
321
|
+
// With the strength picked (or its only one), the chip chooses the product: named as the product.
|
|
322
|
+
const product = view.strength !== null ? productOf(i, view.strength) : (one?.option ?? null);
|
|
323
|
+
return {
|
|
324
|
+
value: String(i),
|
|
325
|
+
label: f.label || "As listed",
|
|
326
|
+
detail: (one && strengthByForm && one.label) || undefined,
|
|
327
|
+
ariaLabel: product?.displayName,
|
|
328
|
+
// Not in the strength picked: greyed, still pickable (it drops the strength).
|
|
329
|
+
muted: view.strength !== null && !product,
|
|
330
|
+
};
|
|
331
|
+
})}
|
|
332
|
+
/>
|
|
333
|
+
</div>
|
|
334
|
+
)}
|
|
335
|
+
{strengths.length > 1 && (
|
|
336
|
+
<div data-chips="strength" onFocus={strengthPicked.onFocus} onBlur={strengthPicked.onBlur}>
|
|
337
|
+
<ToggleGroup
|
|
338
|
+
label="Strength"
|
|
339
|
+
disabled={disabled}
|
|
340
|
+
value={view.strength === null ? null : String(strengths.indexOf(view.strength))}
|
|
341
|
+
onChange={(value) => pickStrength(strengths[Number(value)]!)}
|
|
342
|
+
options={strengths.map((strength, i) => {
|
|
343
|
+
const forms = formsFor(strength);
|
|
344
|
+
// In one form (the one picked, or the only one with it), the chip chooses the product: named as it.
|
|
345
|
+
const product = forms.length === 1 ? productOf(forms[0]!, strength) : null;
|
|
346
|
+
return {
|
|
347
|
+
value: String(i),
|
|
348
|
+
label: strength || "As listed",
|
|
349
|
+
ariaLabel: product?.displayName,
|
|
350
|
+
// The form picked doesn't come in it: greyed, still pickable (it drops the form).
|
|
351
|
+
muted: forms.length === 0,
|
|
352
|
+
};
|
|
353
|
+
})}
|
|
354
|
+
/>
|
|
355
|
+
</div>
|
|
356
|
+
)}
|
|
357
|
+
</div>
|
|
358
|
+
)}
|
|
359
|
+
</Stack>
|
|
360
|
+
</div>
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const empty = (drug: string | null) => ({ drug, form: null, strength: null });
|
|
365
|
+
|
|
366
|
+
/** Where a product is among the drugs: its drug, its form, its strength — as picks. Null if it isn't there. */
|
|
367
|
+
function locate(drugs: Drugs, option?: ChangeOption | null) {
|
|
368
|
+
if (!option) return null;
|
|
369
|
+
const same = (o: ChangeOption) => o === option || (option.id !== null && o.id === option.id);
|
|
370
|
+
for (const [drug, { forms }] of drugs.entries())
|
|
371
|
+
for (const [form, { strengths }] of forms.entries())
|
|
372
|
+
for (const strength of strengths)
|
|
373
|
+
if (same(strength.option)) return { drug, form: forms.length > 1 ? form : null, strength: strength.label };
|
|
374
|
+
return null;
|
|
375
|
+
}
|