@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.
Files changed (272) hide show
  1. package/README.md +52 -9
  2. package/dist/adapters/tailwind.d.ts.map +1 -1
  3. package/dist/adapters/tailwind.js +7 -4
  4. package/dist/adapters/tailwind.js.map +1 -1
  5. package/dist/analytics/PhotonAnalyticsProvider.d.ts +19 -0
  6. package/dist/analytics/PhotonAnalyticsProvider.d.ts.map +1 -0
  7. package/dist/analytics/PhotonAnalyticsProvider.js +76 -0
  8. package/dist/analytics/PhotonAnalyticsProvider.js.map +1 -0
  9. package/dist/analytics/cardLifecycle.d.ts +10 -0
  10. package/dist/analytics/cardLifecycle.d.ts.map +1 -0
  11. package/dist/analytics/cardLifecycle.js +43 -0
  12. package/dist/analytics/cardLifecycle.js.map +1 -0
  13. package/dist/analytics/fieldInteraction.d.ts +18 -0
  14. package/dist/analytics/fieldInteraction.d.ts.map +1 -0
  15. package/dist/analytics/fieldInteraction.js +102 -0
  16. package/dist/analytics/fieldInteraction.js.map +1 -0
  17. package/dist/analytics/lifecycleOutcomes.d.ts +8 -0
  18. package/dist/analytics/lifecycleOutcomes.d.ts.map +1 -0
  19. package/dist/analytics/lifecycleOutcomes.js +16 -0
  20. package/dist/analytics/lifecycleOutcomes.js.map +1 -0
  21. package/dist/analytics/tracker.d.ts +19 -0
  22. package/dist/analytics/tracker.d.ts.map +1 -0
  23. package/dist/analytics/tracker.js +41 -0
  24. package/dist/analytics/tracker.js.map +1 -0
  25. package/dist/analytics/viewed.d.ts +11 -0
  26. package/dist/analytics/viewed.d.ts.map +1 -0
  27. package/dist/analytics/viewed.js +16 -0
  28. package/dist/analytics/viewed.js.map +1 -0
  29. package/dist/components/PhotonOrder.d.ts +18 -18
  30. package/dist/components/PhotonOrder.d.ts.map +1 -1
  31. package/dist/components/PhotonOrder.js +34 -19
  32. package/dist/components/PhotonOrder.js.map +1 -1
  33. package/dist/components/PhotonPatient.d.ts +7 -35
  34. package/dist/components/PhotonPatient.d.ts.map +1 -1
  35. package/dist/components/PhotonPatient.js +6 -78
  36. package/dist/components/PhotonPatient.js.map +1 -1
  37. package/dist/components/PhotonPrescription.d.ts +15 -26
  38. package/dist/components/PhotonPrescription.d.ts.map +1 -1
  39. package/dist/components/PhotonPrescription.js +6 -360
  40. package/dist/components/PhotonPrescription.js.map +1 -1
  41. package/dist/components/format.d.ts +2 -2
  42. package/dist/components/format.js +2 -2
  43. package/dist/components/index.d.ts +1 -1
  44. package/dist/components/index.d.ts.map +1 -1
  45. package/dist/components/index.js +1 -1
  46. package/dist/components/index.js.map +1 -1
  47. package/dist/components/open.d.ts +11 -13
  48. package/dist/components/open.d.ts.map +1 -1
  49. package/dist/components/open.js +7 -2
  50. package/dist/components/open.js.map +1 -1
  51. package/dist/components/order/DraftOrderPrompt.d.ts +18 -0
  52. package/dist/components/order/DraftOrderPrompt.d.ts.map +1 -0
  53. package/dist/components/order/DraftOrderPrompt.js +26 -0
  54. package/dist/components/order/DraftOrderPrompt.js.map +1 -0
  55. package/dist/components/patient/PatientCard/PatientCard.d.ts +30 -0
  56. package/dist/components/patient/PatientCard/PatientCard.d.ts.map +1 -0
  57. package/dist/components/patient/PatientCard/PatientCard.js +101 -0
  58. package/dist/components/patient/PatientCard/PatientCard.js.map +1 -0
  59. package/dist/components/patient/PatientCard/usePatientToShow.d.ts +22 -0
  60. package/dist/components/patient/PatientCard/usePatientToShow.d.ts.map +1 -0
  61. package/dist/components/patient/PatientCard/usePatientToShow.js +40 -0
  62. package/dist/components/patient/PatientCard/usePatientToShow.js.map +1 -0
  63. package/dist/components/patient/PatientIntakeForm.d.ts +16 -0
  64. package/dist/components/patient/PatientIntakeForm.d.ts.map +1 -0
  65. package/dist/components/patient/PatientIntakeForm.js +114 -0
  66. package/dist/components/patient/PatientIntakeForm.js.map +1 -0
  67. package/dist/components/patient/PatientSearch.d.ts +12 -0
  68. package/dist/components/patient/PatientSearch.d.ts.map +1 -0
  69. package/dist/components/patient/PatientSearch.js +148 -0
  70. package/dist/components/patient/PatientSearch.js.map +1 -0
  71. package/dist/components/patient/{Benefits.d.ts → PatientSummary/BenefitsInputForm.d.ts} +2 -2
  72. package/dist/components/patient/PatientSummary/BenefitsInputForm.d.ts.map +1 -0
  73. package/dist/components/patient/{Benefits.js → PatientSummary/BenefitsInputForm.js} +6 -5
  74. package/dist/components/patient/PatientSummary/BenefitsInputForm.js.map +1 -0
  75. package/dist/components/patient/{Clinical.d.ts → PatientSummary/ClinicalInputForm.d.ts} +15 -16
  76. package/dist/components/patient/PatientSummary/ClinicalInputForm.d.ts.map +1 -0
  77. package/dist/components/patient/{Clinical.js → PatientSummary/ClinicalInputForm.js} +29 -28
  78. package/dist/components/patient/PatientSummary/ClinicalInputForm.js.map +1 -0
  79. package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts +5 -0
  80. package/dist/components/patient/PatientSummary/CollapsedSummary.d.ts.map +1 -0
  81. package/dist/components/patient/PatientSummary/CollapsedSummary.js +30 -0
  82. package/dist/components/patient/PatientSummary/CollapsedSummary.js.map +1 -0
  83. package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts +10 -0
  84. package/dist/components/patient/PatientSummary/PatientDetailsForm.d.ts.map +1 -0
  85. package/dist/components/patient/PatientSummary/PatientDetailsForm.js +65 -0
  86. package/dist/components/patient/PatientSummary/PatientDetailsForm.js.map +1 -0
  87. package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts +53 -0
  88. package/dist/components/patient/PatientSummary/PatientDetailsSchema.d.ts.map +1 -0
  89. package/dist/components/patient/PatientSummary/PatientDetailsSchema.js +78 -0
  90. package/dist/components/patient/PatientSummary/PatientDetailsSchema.js.map +1 -0
  91. package/dist/components/patient/PatientSummary/PatientSummary.d.ts +46 -0
  92. package/dist/components/patient/PatientSummary/PatientSummary.d.ts.map +1 -0
  93. package/dist/components/patient/PatientSummary/PatientSummary.js +57 -0
  94. package/dist/components/patient/PatientSummary/PatientSummary.js.map +1 -0
  95. package/dist/components/patient/PatientSummary/SummaryHeader.d.ts +19 -0
  96. package/dist/components/patient/PatientSummary/SummaryHeader.d.ts.map +1 -0
  97. package/dist/components/patient/PatientSummary/SummaryHeader.js +22 -0
  98. package/dist/components/patient/PatientSummary/SummaryHeader.js.map +1 -0
  99. package/dist/components/patient/fields.d.ts +7 -26
  100. package/dist/components/patient/fields.d.ts.map +1 -1
  101. package/dist/components/patient/fields.js +11 -52
  102. package/dist/components/patient/fields.js.map +1 -1
  103. package/dist/components/patient/parsePatientSearchQuery.d.ts +16 -0
  104. package/dist/components/patient/parsePatientSearchQuery.d.ts.map +1 -0
  105. package/dist/components/patient/{query.js → parsePatientSearchQuery.js} +3 -10
  106. package/dist/components/patient/parsePatientSearchQuery.js.map +1 -0
  107. package/dist/components/prescription/PrescriptionCard.d.ts +51 -0
  108. package/dist/components/prescription/PrescriptionCard.d.ts.map +1 -0
  109. package/dist/components/prescription/PrescriptionCard.js +186 -0
  110. package/dist/components/prescription/PrescriptionCard.js.map +1 -0
  111. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts +15 -0
  112. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts.map +1 -0
  113. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js +18 -0
  114. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js.map +1 -0
  115. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts +23 -0
  116. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts.map +1 -0
  117. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js +42 -0
  118. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js.map +1 -0
  119. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts +27 -0
  120. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts.map +1 -0
  121. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js +169 -0
  122. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js.map +1 -0
  123. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts +8 -0
  124. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts.map +1 -0
  125. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js +12 -0
  126. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js.map +1 -0
  127. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts +11 -0
  128. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts.map +1 -0
  129. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js +25 -0
  130. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js.map +1 -0
  131. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts +50 -0
  132. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts.map +1 -0
  133. package/dist/components/{TreatmentPicker.js → prescription/TreatmentSearch/TreatmentPicker.js} +113 -105
  134. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js.map +1 -0
  135. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts +28 -0
  136. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts.map +1 -0
  137. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js +146 -0
  138. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js.map +1 -0
  139. package/dist/components/schemaErrors.d.ts +13 -0
  140. package/dist/components/schemaErrors.d.ts.map +1 -0
  141. package/dist/components/schemaErrors.js +25 -0
  142. package/dist/components/schemaErrors.js.map +1 -0
  143. package/dist/components/search.d.ts +1 -1
  144. package/dist/components/search.js +1 -1
  145. package/dist/index.d.ts +2 -0
  146. package/dist/index.d.ts.map +1 -1
  147. package/dist/index.js +1 -0
  148. package/dist/index.js.map +1 -1
  149. package/dist/meta/version.d.ts +5 -0
  150. package/dist/meta/version.d.ts.map +1 -0
  151. package/dist/meta/version.js +5 -0
  152. package/dist/meta/version.js.map +1 -0
  153. package/dist/primitives/PhotonProvider.d.ts +3 -1
  154. package/dist/primitives/PhotonProvider.d.ts.map +1 -1
  155. package/dist/primitives/PhotonProvider.js +23 -11
  156. package/dist/primitives/PhotonProvider.js.map +1 -1
  157. package/dist/primitives/defaults.d.ts.map +1 -1
  158. package/dist/primitives/defaults.js +6 -3
  159. package/dist/primitives/defaults.js.map +1 -1
  160. package/dist/primitives/photonContext.d.ts +3 -0
  161. package/dist/primitives/photonContext.d.ts.map +1 -0
  162. package/dist/primitives/photonContext.js +3 -0
  163. package/dist/primitives/photonContext.js.map +1 -0
  164. package/dist/primitives/types.d.ts +15 -3
  165. package/dist/primitives/types.d.ts.map +1 -1
  166. package/dist/primitives/types.js.map +1 -1
  167. package/dist/primitives/useProvidedPhoton.d.ts +8 -0
  168. package/dist/primitives/useProvidedPhoton.d.ts.map +1 -0
  169. package/dist/primitives/useProvidedPhoton.js +8 -0
  170. package/dist/primitives/useProvidedPhoton.js.map +1 -0
  171. package/dist/styles.css +45 -3
  172. package/dist/test-utils/contract.d.ts.map +1 -1
  173. package/dist/test-utils/contract.js +23 -2
  174. package/dist/test-utils/contract.js.map +1 -1
  175. package/package.json +8 -3
  176. package/src/adapters/tailwind.tsx +52 -24
  177. package/src/analytics/PhotonAnalyticsProvider.test.tsx +190 -0
  178. package/src/analytics/PhotonAnalyticsProvider.tsx +151 -0
  179. package/src/analytics/cardLifecycle.test.tsx +272 -0
  180. package/src/analytics/cardLifecycle.ts +44 -0
  181. package/src/analytics/fieldInteraction.test.tsx +186 -0
  182. package/src/analytics/fieldInteraction.tsx +148 -0
  183. package/src/analytics/lifecycleOutcomes.test.ts +78 -0
  184. package/src/analytics/lifecycleOutcomes.ts +29 -0
  185. package/src/analytics/tracker.test.ts +52 -0
  186. package/src/analytics/tracker.ts +49 -0
  187. package/src/analytics/usePhotonAnalytics.test.tsx +106 -0
  188. package/src/analytics/viewed.ts +27 -0
  189. package/src/components/PhotonOrder.test.tsx +394 -0
  190. package/src/components/PhotonOrder.tsx +79 -53
  191. package/src/components/PhotonPatient.tsx +15 -166
  192. package/src/components/PhotonPrescription.test.tsx +120 -0
  193. package/src/components/PhotonPrescription.tsx +23 -583
  194. package/src/components/ScreeningAlerts.test.tsx +81 -0
  195. package/src/components/format.ts +2 -2
  196. package/src/components/index.ts +1 -1
  197. package/src/components/open.ts +13 -16
  198. package/src/components/order/DraftOrderPrompt.test.tsx +77 -0
  199. package/src/components/order/DraftOrderPrompt.tsx +81 -0
  200. package/src/components/patient/PatientCard/PatientCard.test.tsx +255 -0
  201. package/src/components/patient/PatientCard/PatientCard.tsx +177 -0
  202. package/src/components/patient/PatientCard/usePatientToShow.ts +44 -0
  203. package/src/components/patient/PatientIntakeForm.test.tsx +262 -0
  204. package/src/components/patient/PatientIntakeForm.tsx +222 -0
  205. package/src/components/patient/PatientSearch.test.tsx +204 -0
  206. package/src/components/patient/PatientSearch.tsx +221 -0
  207. package/src/components/patient/PatientSummary/BenefitsInputForm.test.tsx +164 -0
  208. package/src/components/patient/{Benefits.tsx → PatientSummary/BenefitsInputForm.tsx} +24 -16
  209. package/src/components/patient/PatientSummary/ClinicalInputForm.test.tsx +247 -0
  210. package/src/components/patient/{Clinical.tsx → PatientSummary/ClinicalInputForm.tsx} +53 -48
  211. package/src/components/patient/PatientSummary/CollapsedSummary.test.tsx +90 -0
  212. package/src/components/patient/PatientSummary/CollapsedSummary.tsx +46 -0
  213. package/src/components/patient/PatientSummary/PatientDetailsForm.test.tsx +201 -0
  214. package/src/components/patient/PatientSummary/PatientDetailsForm.tsx +320 -0
  215. package/src/components/patient/PatientSummary/PatientDetailsSchema.test.ts +296 -0
  216. package/src/components/patient/PatientSummary/PatientDetailsSchema.ts +95 -0
  217. package/src/components/patient/PatientSummary/PatientSummary.test.tsx +194 -0
  218. package/src/components/patient/PatientSummary/PatientSummary.tsx +225 -0
  219. package/src/components/patient/PatientSummary/SummaryHeader.test.tsx +183 -0
  220. package/src/components/patient/PatientSummary/SummaryHeader.tsx +85 -0
  221. package/src/components/patient/fields.tsx +12 -124
  222. package/src/components/patient/parsePatientSearchQuery.test.ts +107 -0
  223. package/src/components/patient/{query.ts → parsePatientSearchQuery.ts} +4 -15
  224. package/src/components/prescription/PrescriptionCard.test.tsx +247 -0
  225. package/src/components/prescription/PrescriptionCard.tsx +361 -0
  226. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.test.tsx +82 -0
  227. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.tsx +41 -0
  228. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.test.tsx +206 -0
  229. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.tsx +99 -0
  230. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.test.tsx +453 -0
  231. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.tsx +238 -0
  232. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.test.ts +63 -0
  233. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.ts +15 -0
  234. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.test.tsx +72 -0
  235. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.tsx +41 -0
  236. package/src/components/prescription/TreatmentSearch/TreatmentPicker.tsx +375 -0
  237. package/src/components/prescription/TreatmentSearch/TreatmentSearch.test.tsx +695 -0
  238. package/src/components/prescription/TreatmentSearch/TreatmentSearch.tsx +220 -0
  239. package/src/components/schemaErrors.test.ts +49 -0
  240. package/src/components/schemaErrors.ts +34 -0
  241. package/src/components/search.tsx +1 -1
  242. package/src/index.ts +2 -0
  243. package/src/meta/version.test.ts +11 -0
  244. package/src/meta/version.ts +4 -0
  245. package/src/primitives/PhotonProvider.tsx +30 -15
  246. package/src/primitives/defaults.tsx +44 -21
  247. package/src/primitives/photonContext.ts +4 -0
  248. package/src/primitives/types.ts +15 -3
  249. package/src/primitives/useProvidedPhoton.ts +21 -0
  250. package/src/styles.css +45 -3
  251. package/src/test-utils/contract.tsx +45 -2
  252. package/dist/components/TreatmentPicker.d.ts +0 -45
  253. package/dist/components/TreatmentPicker.d.ts.map +0 -1
  254. package/dist/components/TreatmentPicker.js.map +0 -1
  255. package/dist/components/patient/Benefits.d.ts.map +0 -1
  256. package/dist/components/patient/Benefits.js.map +0 -1
  257. package/dist/components/patient/Clinical.d.ts.map +0 -1
  258. package/dist/components/patient/Clinical.js.map +0 -1
  259. package/dist/components/patient/IntakeForm.d.ts +0 -10
  260. package/dist/components/patient/IntakeForm.d.ts.map +0 -1
  261. package/dist/components/patient/IntakeForm.js +0 -324
  262. package/dist/components/patient/IntakeForm.js.map +0 -1
  263. package/dist/components/patient/Summary.d.ts +0 -48
  264. package/dist/components/patient/Summary.d.ts.map +0 -1
  265. package/dist/components/patient/Summary.js +0 -80
  266. package/dist/components/patient/Summary.js.map +0 -1
  267. package/dist/components/patient/query.d.ts +0 -23
  268. package/dist/components/patient/query.d.ts.map +0 -1
  269. package/dist/components/patient/query.js.map +0 -1
  270. package/src/components/TreatmentPicker.tsx +0 -350
  271. package/src/components/patient/IntakeForm.tsx +0 -532
  272. package/src/components/patient/Summary.tsx +0 -310
@@ -0,0 +1,206 @@
1
+ import { act, fireEvent, render, screen, within } from "@testing-library/react";
2
+ import userEvent from "@testing-library/user-event";
3
+ import { afterEach, describe, expect, it, vi } from "vitest";
4
+ import { fakeNetworkApiClient } from "@photonhealth/client/testing";
5
+ import { DispenseInputs, type DispenseInputsProps, type DispenseValues } from "./DispenseInputs.tsx";
6
+ import {
7
+ buildEditedEventsFor,
8
+ leaveField,
9
+ meAsPrescriber,
10
+ recordAnalytics,
11
+ unmountAndRestoreFetch,
12
+ withAnalytics,
13
+ } from "../../../../test/analytics.fixtures.tsx";
14
+
15
+ const dispense: DispenseValues = {
16
+ quantity: "30",
17
+ unit: "Capsule",
18
+ daysSupply: "10",
19
+ refills: "0",
20
+ dispenseAsWritten: false,
21
+ };
22
+
23
+ const renderInputs = (props: Partial<DispenseInputsProps> = {}, values: Partial<DispenseValues> = {}) => {
24
+ const all: DispenseInputsProps = {
25
+ values: { ...dispense, ...values },
26
+ units: [],
27
+ edited: new Set(),
28
+ spinning: null,
29
+ onEdit: vi.fn(),
30
+ save: vi.fn(),
31
+ ...props,
32
+ };
33
+ render(<DispenseInputs {...all} />);
34
+ return all;
35
+ };
36
+
37
+ const input = (label: string) => screen.getByLabelText<HTMLInputElement>(label);
38
+
39
+ const unitOptions = () =>
40
+ within(screen.getByRole("combobox", { name: "Unit" }))
41
+ .getAllByRole<HTMLOptionElement>("option")
42
+ .filter((option) => !option.disabled)
43
+ .map((option) => option.textContent);
44
+
45
+ describe("DispenseInputs", () => {
46
+ it("shows the dispense", () => {
47
+ renderInputs();
48
+ expect(input("Quantity").value).toBe("30");
49
+ expect(input("Unit").value).toBe("Capsule");
50
+ expect(input("Days supply").value).toBe("10");
51
+ expect(input("Refills").value).toBe("0");
52
+ expect(screen.getByRole<HTMLInputElement>("checkbox", { name: "Dispense as written" }).checked).toBe(false);
53
+ });
54
+
55
+ describe("when I leave a field", () => {
56
+ it.each([
57
+ { edit: "Quantity set to 60", label: "Quantity", field: "quantity", typed: "60", patch: { quantity: 60 } },
58
+ { edit: "Quantity set to 2.5", label: "Quantity", field: "quantity", typed: "2.5", patch: { quantity: 2.5 } },
59
+ { edit: "Quantity cleared", label: "Quantity", field: "quantity", typed: "", patch: { quantity: undefined } },
60
+ {
61
+ edit: "Days supply set to 14",
62
+ label: "Days supply",
63
+ field: "daysSupply",
64
+ typed: "14",
65
+ patch: { daysSupply: 14 },
66
+ },
67
+ { edit: "Refills set to 2", label: "Refills", field: "refills", typed: "2", patch: { refillsAllowed: 2 } },
68
+ { edit: "Unit set to Bottle", label: "Unit", field: "unit", typed: "Bottle", patch: { unit: "Bottle" } },
69
+ { edit: "Unit cleared", label: "Unit", field: "unit", typed: "", patch: { unit: undefined } },
70
+ ] as const)(
71
+ "saves $edit on its own, so it can't overwrite newer values in other fields",
72
+ ({ label, field, typed, patch }) => {
73
+ const { save } = renderInputs({ edited: new Set([field]) }, { [field]: typed });
74
+ fireEvent.blur(input(label));
75
+ expect(save).toHaveBeenCalledExactlyOnceWith(field, patch);
76
+ expect(Object.keys(vi.mocked(save).mock.calls[0]![1])).toEqual(Object.keys(patch));
77
+ },
78
+ );
79
+
80
+ it("doesn't save a field I didn't change", () => {
81
+ const { save } = renderInputs();
82
+ fireEvent.blur(input("Quantity"));
83
+ expect(save).not.toHaveBeenCalled();
84
+ });
85
+ });
86
+
87
+ describe("when I focus a field", () => {
88
+ it("selects an unedited value, so typing replaces it", () => {
89
+ renderInputs();
90
+ act(() => input("Quantity").focus());
91
+ expect([input("Quantity").selectionStart, input("Quantity").selectionEnd]).toEqual([0, 2]);
92
+ });
93
+
94
+ it("doesn't select an edited value", () => {
95
+ renderInputs({ edited: new Set(["quantity"]) }, { quantity: "60" });
96
+ act(() => input("Quantity").focus());
97
+ expect(input("Quantity").selectionStart).toBe(input("Quantity").selectionEnd);
98
+ });
99
+ });
100
+
101
+ describe("error messages", () => {
102
+ const labels = ["Quantity", "Unit", "Days supply", "Refills"];
103
+ const noteOf = (label: string) => {
104
+ const note = input(label).getAttribute("aria-describedby");
105
+ return note ? document.getElementById(note)?.textContent : null;
106
+ };
107
+ const isInvalid = (label: string) => input(label).getAttribute("aria-invalid") === "true";
108
+
109
+ it.each([
110
+ { field: "quantity", label: "Quantity" },
111
+ { field: "unit", label: "Unit" },
112
+ { field: "daysSupply", label: "Days supply" },
113
+ { field: "refills", label: "Refills" },
114
+ ] as const)(
115
+ "shows the error message it's given under $label, and marks only that field invalid",
116
+ ({ field, label }) => {
117
+ renderInputs({ errors: { [field]: "Must be greater than 0" } });
118
+ expect(isInvalid(label)).toBe(true);
119
+ expect(noteOf(label)).toBe("Must be greater than 0");
120
+ for (const other of labels.filter((l) => l !== label)) expect(isInvalid(other)).toBe(false);
121
+ },
122
+ );
123
+
124
+ it("shows each field's own error message", () => {
125
+ renderInputs({ errors: { quantity: "Required", daysSupply: "Must be a number" } });
126
+ expect(noteOf("Quantity")).toBe("Required");
127
+ expect(noteOf("Days supply")).toBe("Must be a number");
128
+ });
129
+
130
+ it("shows no errors when none are specified", () => {
131
+ renderInputs();
132
+ for (const label of labels) expect(isInvalid(label)).toBe(false);
133
+ });
134
+ });
135
+
136
+ it("shows the field whose save is in flight as busy", () => {
137
+ renderInputs({ spinning: "quantity" });
138
+ expect(input("Quantity").getAttribute("aria-busy")).toBe("true");
139
+ expect(input("Days supply").getAttribute("aria-busy")).toBeNull();
140
+ });
141
+
142
+ it('saves "Dispense as written" as soon as it\'s checked', () => {
143
+ const { save } = renderInputs();
144
+ fireEvent.click(screen.getByRole("checkbox", { name: "Dispense as written" }));
145
+ expect(save).toHaveBeenCalledExactlyOnceWith("daw", { dispenseAsWritten: true });
146
+ });
147
+
148
+ describe("unit", () => {
149
+ it("is typed when the drug has no dispense units", () => {
150
+ renderInputs();
151
+ expect(screen.getByRole("textbox", { name: "Unit" })).toBeTruthy();
152
+ });
153
+
154
+ it("is chosen from the drug's dispense units", () => {
155
+ renderInputs({ units: ["Capsule", "Bottle"] });
156
+ expect(unitOptions()).toEqual(["Capsule", "Bottle"]);
157
+ });
158
+
159
+ it("still offers a unit the drug isn't dispensed in", () => {
160
+ renderInputs({ units: ["Tablet", "Bottle"] });
161
+ expect(unitOptions()).toEqual(["Tablet", "Bottle", "Capsule"]);
162
+ });
163
+
164
+ it("matches a dispense unit whatever its case", () => {
165
+ renderInputs({ units: ["Capsule", "Bottle"] }, { unit: "capsule" });
166
+ expect(unitOptions()).toEqual(["Capsule", "Bottle"]);
167
+ expect(input("Unit").value).toBe("Capsule");
168
+ });
169
+
170
+ it("shows and saves a chosen unit right away", () => {
171
+ const { onEdit, save } = renderInputs({ units: ["Capsule", "Bottle"] });
172
+ fireEvent.change(screen.getByRole("combobox", { name: "Unit" }), { target: { value: "Bottle" } });
173
+ expect(onEdit).toHaveBeenCalledWith("unit", "Bottle");
174
+ expect(save).toHaveBeenCalledExactlyOnceWith("unit", { unit: "Bottle" });
175
+ });
176
+ });
177
+
178
+ describe("Field Interaction", () => {
179
+ afterEach(unmountAndRestoreFetch);
180
+
181
+ it("the unit picked from the drug's units is dispenseUnit: focus, change on the pick, blur", async () => {
182
+ const analytics = recordAnalytics();
183
+ const all: DispenseInputsProps = {
184
+ values: { ...dispense, unit: "" },
185
+ units: ["Capsule", "Tablet"],
186
+ edited: new Set(),
187
+ spinning: null,
188
+ onEdit: vi.fn(),
189
+ save: vi.fn(),
190
+ };
191
+ render(<DispenseInputs {...all} />, { wrapper: withAnalytics(fakeNetworkApiClient(meAsPrescriber).client) });
192
+
193
+ await userEvent.selectOptions(screen.getByRole("combobox", { name: "Unit" }), "Tablet");
194
+ leaveField();
195
+
196
+ await analytics.expectEventLog([...buildEditedEventsFor("dispenseUnit")]);
197
+ expect(analytics.sentFor("dispenseUnit")).toEqual([
198
+ expect.objectContaining({ hasValue: false, isOptional: false }),
199
+ expect.objectContaining({ hasValue: true }),
200
+ expect.objectContaining({ changed: true }),
201
+ ]);
202
+ expect(all.save).toHaveBeenCalledWith("unit", { unit: "Tablet" });
203
+ for (const body of analytics.bodies()) expect(body).not.toContain("Tablet");
204
+ });
205
+ });
206
+ });
@@ -0,0 +1,99 @@
1
+ import type { PrescriptionEdits } from "@photonhealth/client";
2
+ import { useComponents } from "../../../primitives/index.ts";
3
+ import { TrackedField, useFieldInteractions } from "../../../analytics/fieldInteraction.tsx";
4
+
5
+ export type DispenseField = "quantity" | "unit" | "daysSupply" | "refills";
6
+ export type DispenseValues = Record<DispenseField, string> & { dispenseAsWritten: boolean };
7
+ export type DispensePatch = NonNullable<PrescriptionEdits["dispense"]>;
8
+
9
+ /** Quantity / unit / days / refills, then DAW. Their values are kept by PrescriptionDetailsForm, which saves them. */
10
+ export interface DispenseInputsProps {
11
+ values: DispenseValues;
12
+ /** What's wrong with a field, shown under it. */
13
+ errors?: Partial<Record<DispenseField, string>>;
14
+ /** The units the picked drug is dispensed in; without them the unit is typed. */
15
+ units: readonly string[];
16
+ /** Fields typed in since they were last saved. */
17
+ edited: ReadonlySet<DispenseField>;
18
+ /** The field whose save is in flight. */
19
+ spinning: string | null;
20
+ onEdit: (field: DispenseField, value: string) => void;
21
+ /** Only the field that changed: network-api merges a partial dispense. */
22
+ save: (field: DispenseField | "daw", patch: DispensePatch) => void;
23
+ }
24
+
25
+ const num = (value: string) => (value.trim() === "" ? undefined : Number(value));
26
+
27
+ export function DispenseInputs({ values, errors = {}, units, edited, spinning, onEdit, save }: DispenseInputsProps) {
28
+ const { Checkbox, Grid, Input, Select } = useComponents();
29
+ const fieldInteraction = useFieldInteractions("add_prescription_form");
30
+ const unitPicked = fieldInteraction("dispenseUnit", { isOptional: false, hasValue: Boolean(values.unit.trim()) });
31
+ const dispenseAsWritten = fieldInteraction("dispenseAsWritten", {
32
+ isOptional: true,
33
+ hasValue: values.dispenseAsWritten,
34
+ });
35
+
36
+ const typed = (field: DispenseField, toPatch: (value: string) => DispensePatch) => ({
37
+ value: values[field],
38
+ invalid: Boolean(errors[field]),
39
+ errorMessage: errors[field],
40
+ // A value the server filled in (e.g. quantity 30) is selected, so typing replaces it.
41
+ onFocus: (e: { currentTarget: HTMLInputElement }) => {
42
+ if (!edited.has(field)) e.currentTarget.select();
43
+ },
44
+ onChange: (e: { target: HTMLInputElement }) => onEdit(field, e.target.value),
45
+ onBlur: () => {
46
+ if (edited.has(field)) save(field, toPatch(values[field]));
47
+ },
48
+ loading: spinning === field,
49
+ });
50
+
51
+ // The prescription's own unit stays listed, even when it isn't one the drug is dispensed in.
52
+ const unit = values.unit.trim();
53
+ const listedUnit = units.find((u) => u.toLowerCase() === unit.toLowerCase());
54
+ const unitOptions = [...units, ...(unit && !listedUnit ? [values.unit] : [])].map((u) => ({ value: u, label: u }));
55
+
56
+ return (
57
+ <>
58
+ <Grid min="64px" gap="2">
59
+ <TrackedField formName="add_prescription_form" fieldName="dispenseQuantity" isOptional={false}>
60
+ <Input label="Quantity" inputMode="decimal" {...typed("quantity", (v) => ({ quantity: num(v) }))} />
61
+ </TrackedField>
62
+ {units.length > 0 ? (
63
+ <div style={{ display: "contents" }} onFocus={unitPicked.onFocus} onBlur={unitPicked.onBlur}>
64
+ <Select
65
+ label="Unit"
66
+ options={unitOptions}
67
+ value={listedUnit ?? (values.unit || null)}
68
+ onChange={(value) => {
69
+ unitPicked.onChange(true);
70
+ onEdit("unit", value);
71
+ save("unit", { unit: value });
72
+ }}
73
+ />
74
+ </div>
75
+ ) : (
76
+ <TrackedField formName="add_prescription_form" fieldName="dispenseUnit" isOptional={false}>
77
+ <Input label="Unit" {...typed("unit", (v) => ({ unit: v || undefined }))} />
78
+ </TrackedField>
79
+ )}
80
+ <TrackedField formName="add_prescription_form" fieldName="daysSupply" isOptional>
81
+ <Input label="Days supply" inputMode="numeric" {...typed("daysSupply", (v) => ({ daysSupply: num(v) }))} />
82
+ </TrackedField>
83
+ <TrackedField formName="add_prescription_form" fieldName="refills" isOptional>
84
+ <Input label="Refills" inputMode="numeric" {...typed("refills", (v) => ({ refillsAllowed: num(v) }))} />
85
+ </TrackedField>
86
+ </Grid>
87
+ <div onFocus={dispenseAsWritten.onFocus} onBlur={dispenseAsWritten.onBlur}>
88
+ <Checkbox
89
+ label="Dispense as written"
90
+ checked={values.dispenseAsWritten}
91
+ onChange={(checked) => {
92
+ dispenseAsWritten.onChange(checked);
93
+ save("daw", { dispenseAsWritten: checked });
94
+ }}
95
+ />
96
+ </div>
97
+ </>
98
+ );
99
+ }