@photonhealth/react 0.0.0-stage → 2.0.0-rc.2

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 (277) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +111 -2
  3. package/dist/adapters/index.d.ts +2 -0
  4. package/dist/adapters/index.d.ts.map +1 -0
  5. package/dist/adapters/index.js +2 -0
  6. package/dist/adapters/index.js.map +1 -0
  7. package/dist/adapters/tailwind.d.ts +3 -0
  8. package/dist/adapters/tailwind.d.ts.map +1 -0
  9. package/dist/adapters/tailwind.js +126 -0
  10. package/dist/adapters/tailwind.js.map +1 -0
  11. package/dist/analytics/PhotonAnalyticsProvider.d.ts +16 -0
  12. package/dist/analytics/PhotonAnalyticsProvider.d.ts.map +1 -0
  13. package/dist/analytics/PhotonAnalyticsProvider.js +57 -0
  14. package/dist/analytics/PhotonAnalyticsProvider.js.map +1 -0
  15. package/dist/analytics/fieldInteraction.d.ts +18 -0
  16. package/dist/analytics/fieldInteraction.d.ts.map +1 -0
  17. package/dist/analytics/fieldInteraction.js +102 -0
  18. package/dist/analytics/fieldInteraction.js.map +1 -0
  19. package/dist/analytics/tracker.d.ts +17 -0
  20. package/dist/analytics/tracker.d.ts.map +1 -0
  21. package/dist/analytics/tracker.js +36 -0
  22. package/dist/analytics/tracker.js.map +1 -0
  23. package/dist/analytics/viewed.d.ts +11 -0
  24. package/dist/analytics/viewed.d.ts.map +1 -0
  25. package/dist/analytics/viewed.js +16 -0
  26. package/dist/analytics/viewed.js.map +1 -0
  27. package/dist/components/ChangeList.d.ts +21 -0
  28. package/dist/components/ChangeList.d.ts.map +1 -0
  29. package/dist/components/ChangeList.js +40 -0
  30. package/dist/components/ChangeList.js.map +1 -0
  31. package/dist/components/PhotonOrder.d.ts +46 -0
  32. package/dist/components/PhotonOrder.d.ts.map +1 -0
  33. package/dist/components/PhotonOrder.js +274 -0
  34. package/dist/components/PhotonOrder.js.map +1 -0
  35. package/dist/components/PhotonPatient.d.ts +48 -0
  36. package/dist/components/PhotonPatient.d.ts.map +1 -0
  37. package/dist/components/PhotonPatient.js +85 -0
  38. package/dist/components/PhotonPatient.js.map +1 -0
  39. package/dist/components/PhotonPrescription.d.ts +32 -0
  40. package/dist/components/PhotonPrescription.d.ts.map +1 -0
  41. package/dist/components/PhotonPrescription.js +11 -0
  42. package/dist/components/PhotonPrescription.js.map +1 -0
  43. package/dist/components/ScreeningAlerts.d.ts +6 -0
  44. package/dist/components/ScreeningAlerts.d.ts.map +1 -0
  45. package/dist/components/ScreeningAlerts.js +11 -0
  46. package/dist/components/ScreeningAlerts.js.map +1 -0
  47. package/dist/components/SignedIn.d.ts +12 -0
  48. package/dist/components/SignedIn.d.ts.map +1 -0
  49. package/dist/components/SignedIn.js +84 -0
  50. package/dist/components/SignedIn.js.map +1 -0
  51. package/dist/components/activity.d.ts +7 -0
  52. package/dist/components/activity.d.ts.map +1 -0
  53. package/dist/components/activity.js +24 -0
  54. package/dist/components/activity.js.map +1 -0
  55. package/dist/components/display.d.ts +90 -0
  56. package/dist/components/display.d.ts.map +1 -0
  57. package/dist/components/display.js +223 -0
  58. package/dist/components/display.js.map +1 -0
  59. package/dist/components/flow.d.ts +35 -0
  60. package/dist/components/flow.d.ts.map +1 -0
  61. package/dist/components/flow.js +53 -0
  62. package/dist/components/flow.js.map +1 -0
  63. package/dist/components/format.d.ts +26 -0
  64. package/dist/components/format.d.ts.map +1 -0
  65. package/dist/components/format.js +61 -0
  66. package/dist/components/format.js.map +1 -0
  67. package/dist/components/index.d.ts +6 -0
  68. package/dist/components/index.d.ts.map +1 -0
  69. package/dist/components/index.js +6 -0
  70. package/dist/components/index.js.map +1 -0
  71. package/dist/components/open.d.ts +47 -0
  72. package/dist/components/open.d.ts.map +1 -0
  73. package/dist/components/open.js +73 -0
  74. package/dist/components/open.js.map +1 -0
  75. package/dist/components/order/DraftOrderPrompt.d.ts +18 -0
  76. package/dist/components/order/DraftOrderPrompt.d.ts.map +1 -0
  77. package/dist/components/order/DraftOrderPrompt.js +26 -0
  78. package/dist/components/order/DraftOrderPrompt.js.map +1 -0
  79. package/dist/components/patient/Benefits.d.ts +25 -0
  80. package/dist/components/patient/Benefits.d.ts.map +1 -0
  81. package/dist/components/patient/Benefits.js +48 -0
  82. package/dist/components/patient/Benefits.js.map +1 -0
  83. package/dist/components/patient/Clinical.d.ts +56 -0
  84. package/dist/components/patient/Clinical.d.ts.map +1 -0
  85. package/dist/components/patient/Clinical.js +118 -0
  86. package/dist/components/patient/Clinical.js.map +1 -0
  87. package/dist/components/patient/IntakeForm.d.ts +10 -0
  88. package/dist/components/patient/IntakeForm.d.ts.map +1 -0
  89. package/dist/components/patient/IntakeForm.js +333 -0
  90. package/dist/components/patient/IntakeForm.js.map +1 -0
  91. package/dist/components/patient/Summary.d.ts +48 -0
  92. package/dist/components/patient/Summary.d.ts.map +1 -0
  93. package/dist/components/patient/Summary.js +90 -0
  94. package/dist/components/patient/Summary.js.map +1 -0
  95. package/dist/components/patient/fields.d.ts +42 -0
  96. package/dist/components/patient/fields.d.ts.map +1 -0
  97. package/dist/components/patient/fields.js +72 -0
  98. package/dist/components/patient/fields.js.map +1 -0
  99. package/dist/components/patient/query.d.ts +37 -0
  100. package/dist/components/patient/query.d.ts.map +1 -0
  101. package/dist/components/patient/query.js +70 -0
  102. package/dist/components/patient/query.js.map +1 -0
  103. package/dist/components/prescription/PrescriptionCard.d.ts +45 -0
  104. package/dist/components/prescription/PrescriptionCard.d.ts.map +1 -0
  105. package/dist/components/prescription/PrescriptionCard.js +179 -0
  106. package/dist/components/prescription/PrescriptionCard.js.map +1 -0
  107. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts +15 -0
  108. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.d.ts.map +1 -0
  109. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js +18 -0
  110. package/dist/components/prescription/PrescriptionDetailsForm/DirectionsInput.js.map +1 -0
  111. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts +23 -0
  112. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.d.ts.map +1 -0
  113. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js +42 -0
  114. package/dist/components/prescription/PrescriptionDetailsForm/DispenseInputs.js.map +1 -0
  115. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts +27 -0
  116. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.d.ts.map +1 -0
  117. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js +169 -0
  118. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.js.map +1 -0
  119. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts +8 -0
  120. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.d.ts.map +1 -0
  121. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js +12 -0
  122. package/dist/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.js.map +1 -0
  123. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts +11 -0
  124. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.d.ts.map +1 -0
  125. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js +25 -0
  126. package/dist/components/prescription/PrescriptionDetailsForm/TemplatePicker.js.map +1 -0
  127. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts +50 -0
  128. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.d.ts.map +1 -0
  129. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js +287 -0
  130. package/dist/components/prescription/TreatmentSearch/TreatmentPicker.js.map +1 -0
  131. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts +28 -0
  132. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.d.ts.map +1 -0
  133. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js +146 -0
  134. package/dist/components/prescription/TreatmentSearch/TreatmentSearch.js.map +1 -0
  135. package/dist/components/schemaErrors.d.ts +12 -0
  136. package/dist/components/schemaErrors.d.ts.map +1 -0
  137. package/dist/components/schemaErrors.js +24 -0
  138. package/dist/components/schemaErrors.js.map +1 -0
  139. package/dist/components/search.d.ts +154 -0
  140. package/dist/components/search.d.ts.map +1 -0
  141. package/dist/components/search.js +295 -0
  142. package/dist/components/search.js.map +1 -0
  143. package/dist/hooks/index.d.ts +5 -0
  144. package/dist/hooks/index.d.ts.map +1 -0
  145. package/dist/hooks/index.js +5 -0
  146. package/dist/hooks/index.js.map +1 -0
  147. package/dist/hooks/useLatestCallback.d.ts +7 -0
  148. package/dist/hooks/useLatestCallback.d.ts.map +1 -0
  149. package/dist/hooks/useLatestCallback.js +14 -0
  150. package/dist/hooks/useLatestCallback.js.map +1 -0
  151. package/dist/hooks/useLive.d.ts +15 -0
  152. package/dist/hooks/useLive.d.ts.map +1 -0
  153. package/dist/hooks/useLive.js +6 -0
  154. package/dist/hooks/useLive.js.map +1 -0
  155. package/dist/hooks/useOnChange.d.ts +11 -0
  156. package/dist/hooks/useOnChange.d.ts.map +1 -0
  157. package/dist/hooks/useOnChange.js +28 -0
  158. package/dist/hooks/useOnChange.js.map +1 -0
  159. package/dist/hooks/useSession.d.ts +7 -0
  160. package/dist/hooks/useSession.d.ts.map +1 -0
  161. package/dist/hooks/useSession.js +10 -0
  162. package/dist/hooks/useSession.js.map +1 -0
  163. package/dist/index.d.ts +22 -0
  164. package/dist/index.d.ts.map +1 -0
  165. package/dist/index.js +20 -0
  166. package/dist/index.js.map +1 -0
  167. package/dist/meta/version.d.ts +5 -0
  168. package/dist/meta/version.d.ts.map +1 -0
  169. package/dist/meta/version.js +5 -0
  170. package/dist/meta/version.js.map +1 -0
  171. package/dist/primitives/PhotonProvider.d.ts +60 -0
  172. package/dist/primitives/PhotonProvider.d.ts.map +1 -0
  173. package/dist/primitives/PhotonProvider.js +103 -0
  174. package/dist/primitives/PhotonProvider.js.map +1 -0
  175. package/dist/primitives/defaults.d.ts +3 -0
  176. package/dist/primitives/defaults.d.ts.map +1 -0
  177. package/dist/primitives/defaults.js +70 -0
  178. package/dist/primitives/defaults.js.map +1 -0
  179. package/dist/primitives/icons.d.ts +6 -0
  180. package/dist/primitives/icons.d.ts.map +1 -0
  181. package/dist/primitives/icons.js +6 -0
  182. package/dist/primitives/icons.js.map +1 -0
  183. package/dist/primitives/index.d.ts +5 -0
  184. package/dist/primitives/index.d.ts.map +1 -0
  185. package/dist/primitives/index.js +5 -0
  186. package/dist/primitives/index.js.map +1 -0
  187. package/dist/primitives/photonContext.d.ts +3 -0
  188. package/dist/primitives/photonContext.d.ts.map +1 -0
  189. package/dist/primitives/photonContext.js +3 -0
  190. package/dist/primitives/photonContext.js.map +1 -0
  191. package/dist/primitives/select.d.ts +7 -0
  192. package/dist/primitives/select.d.ts.map +1 -0
  193. package/dist/primitives/select.js +16 -0
  194. package/dist/primitives/select.js.map +1 -0
  195. package/dist/primitives/types.d.ts +288 -0
  196. package/dist/primitives/types.d.ts.map +1 -0
  197. package/dist/primitives/types.js +3 -0
  198. package/dist/primitives/types.js.map +1 -0
  199. package/dist/primitives/useProvidedPhoton.d.ts +8 -0
  200. package/dist/primitives/useProvidedPhoton.d.ts.map +1 -0
  201. package/dist/primitives/useProvidedPhoton.js +8 -0
  202. package/dist/primitives/useProvidedPhoton.js.map +1 -0
  203. package/dist/styles.css +653 -0
  204. package/dist/test-utils/contract.d.ts +17 -0
  205. package/dist/test-utils/contract.d.ts.map +1 -0
  206. package/dist/test-utils/contract.js +282 -0
  207. package/dist/test-utils/contract.js.map +1 -0
  208. package/package.json +76 -4
  209. package/src/adapters/index.ts +1 -0
  210. package/src/adapters/tailwind.tsx +572 -0
  211. package/src/analytics/PhotonAnalyticsProvider.test.tsx +191 -0
  212. package/src/analytics/PhotonAnalyticsProvider.tsx +96 -0
  213. package/src/analytics/fieldInteraction.test.tsx +184 -0
  214. package/src/analytics/fieldInteraction.tsx +148 -0
  215. package/src/analytics/tracker.test.ts +52 -0
  216. package/src/analytics/tracker.ts +42 -0
  217. package/src/analytics/viewed.ts +27 -0
  218. package/src/components/ChangeList.tsx +85 -0
  219. package/src/components/PhotonOrder.test.tsx +256 -0
  220. package/src/components/PhotonOrder.tsx +508 -0
  221. package/src/components/PhotonPatient.tsx +188 -0
  222. package/src/components/PhotonPrescription.test.tsx +116 -0
  223. package/src/components/PhotonPrescription.tsx +45 -0
  224. package/src/components/ScreeningAlerts.tsx +28 -0
  225. package/src/components/SignedIn.tsx +144 -0
  226. package/src/components/activity.ts +23 -0
  227. package/src/components/display.ts +278 -0
  228. package/src/components/flow.ts +67 -0
  229. package/src/components/format.ts +73 -0
  230. package/src/components/index.ts +5 -0
  231. package/src/components/open.ts +100 -0
  232. package/src/components/order/DraftOrderPrompt.test.tsx +80 -0
  233. package/src/components/order/DraftOrderPrompt.tsx +81 -0
  234. package/src/components/patient/Benefits.tsx +133 -0
  235. package/src/components/patient/Clinical.tsx +215 -0
  236. package/src/components/patient/IntakeForm.test.tsx +89 -0
  237. package/src/components/patient/IntakeForm.tsx +564 -0
  238. package/src/components/patient/Summary.test.tsx +106 -0
  239. package/src/components/patient/Summary.tsx +330 -0
  240. package/src/components/patient/fields.tsx +158 -0
  241. package/src/components/patient/query.ts +83 -0
  242. package/src/components/prescription/PrescriptionCard.test.tsx +85 -0
  243. package/src/components/prescription/PrescriptionCard.tsx +347 -0
  244. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.test.tsx +85 -0
  245. package/src/components/prescription/PrescriptionDetailsForm/DirectionsInput.tsx +41 -0
  246. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.test.tsx +209 -0
  247. package/src/components/prescription/PrescriptionDetailsForm/DispenseInputs.tsx +99 -0
  248. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.test.tsx +456 -0
  249. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsForm.tsx +238 -0
  250. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.test.ts +63 -0
  251. package/src/components/prescription/PrescriptionDetailsForm/PrescriptionDetailsSchema.ts +15 -0
  252. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.test.tsx +75 -0
  253. package/src/components/prescription/PrescriptionDetailsForm/TemplatePicker.tsx +41 -0
  254. package/src/components/prescription/TreatmentSearch/TreatmentPicker.tsx +375 -0
  255. package/src/components/prescription/TreatmentSearch/TreatmentSearch.test.tsx +699 -0
  256. package/src/components/prescription/TreatmentSearch/TreatmentSearch.tsx +220 -0
  257. package/src/components/schemaErrors.test.ts +49 -0
  258. package/src/components/schemaErrors.ts +33 -0
  259. package/src/components/search.tsx +377 -0
  260. package/src/hooks/index.ts +4 -0
  261. package/src/hooks/useLatestCallback.ts +16 -0
  262. package/src/hooks/useLive.ts +21 -0
  263. package/src/hooks/useOnChange.ts +34 -0
  264. package/src/hooks/useSession.ts +11 -0
  265. package/src/index.ts +21 -0
  266. package/src/meta/version.test.ts +10 -0
  267. package/src/meta/version.ts +4 -0
  268. package/src/primitives/PhotonProvider.tsx +168 -0
  269. package/src/primitives/defaults.tsx +432 -0
  270. package/src/primitives/icons.tsx +37 -0
  271. package/src/primitives/index.ts +4 -0
  272. package/src/primitives/photonContext.ts +4 -0
  273. package/src/primitives/select.ts +15 -0
  274. package/src/primitives/types.ts +288 -0
  275. package/src/primitives/useProvidedPhoton.ts +21 -0
  276. package/src/styles.css +653 -0
  277. package/src/test-utils/contract.tsx +453 -0
@@ -0,0 +1,220 @@
1
+ import type { Change, ChangeOption, Prescription, PrescriptionEdits, PrescriptionRecord } from "@photonhealth/client";
2
+ import { useEffect, useRef, useState } from "react";
3
+ import { useLatestCallback } from "../../../hooks/index.ts";
4
+ import { useComponents } from "../../../primitives/index.ts";
5
+ import { focusProps, searchProps } from "../../flow.ts";
6
+ import { FEW, searchKeys, useTypeAhead } from "../../search.tsx";
7
+ import { TreatmentPicker, type TreatmentPickerHandle } from "./TreatmentPicker.tsx";
8
+ import { useFieldInteractions } from "../../../analytics/fieldInteraction.tsx";
9
+
10
+ /** A drug chosen from one of the server's choices: the choice, and the option taken. */
11
+ export interface TreatmentChoice {
12
+ change: Change;
13
+ option: ChangeOption;
14
+ }
15
+
16
+ export interface TreatmentSearchProps {
17
+ live: Prescription | null;
18
+ rx: PrescriptionRecord | null;
19
+ /** The drug picked before the line has a record (the server drafts it once the patient is known). */
20
+ pickId: string | null;
21
+ /** The server's choices for the drug typed. */
22
+ drugChoices: readonly Change[];
23
+ /** A product chosen, saving: nothing to pick until the server answers. */
24
+ choosing: TreatmentChoice | null;
25
+ /** Shown until a product is set. Kept mounted while closed. */
26
+ open: boolean;
27
+ busy: boolean;
28
+ /** The field whose save is in flight. */
29
+ spinning: string | null;
30
+ autoFocus?: boolean;
31
+ /** Put the cursor in Treatment each time this changes. */
32
+ focusSignal?: number;
33
+ save: (edits: PrescriptionEdits, from?: string) => void;
34
+ onChoose: (choice: TreatmentChoice) => void;
35
+ }
36
+
37
+ export function TreatmentSearch({
38
+ live,
39
+ rx,
40
+ pickId,
41
+ drugChoices,
42
+ choosing,
43
+ open,
44
+ busy,
45
+ spinning,
46
+ autoFocus,
47
+ focusSignal,
48
+ save,
49
+ onChoose,
50
+ }: TreatmentSearchProps) {
51
+ const { Input } = useComponents();
52
+ const treatmentField = useFieldInteractions("add_prescription_form")("treatment", { isOptional: false });
53
+ const [treatment, setTreatment] = useState(rx?.treatment.name ?? "");
54
+ // Only send free-text medication when it actually changed (see submitTreatment).
55
+ const [sentTreatment, setSentTreatment] = useState(rx?.treatment.name ?? "");
56
+
57
+ // When the server's copy changes (a hydrate, a chosen option, signing), show its drug.
58
+ // Adjusted during render, keyed on id + contentHash — React's pattern for deriving state from props.
59
+ const serverKey = rx ? `${rx.id}:${rx.signing.contentHash}:${rx.signing.state}` : null;
60
+ const [syncedKey, setSyncedKey] = useState(serverKey);
61
+ if (rx && serverKey !== syncedKey) {
62
+ setSyncedKey(serverKey);
63
+ const shownName = live?.name ?? rx.treatment.name;
64
+ setTreatment(shownName);
65
+ setSentTreatment(shownName); // what's shown was the pick — leaving the field mustn't re-send it as text
66
+ }
67
+
68
+ // A line without a record shows the drug entered on it — e.g. given as props: once per object.
69
+ const [filledFrom, setFilledFrom] = useState<Prescription | null>(null);
70
+ if (live && live !== filledFrom) {
71
+ setFilledFrom(live);
72
+ const text = live.name ?? (rx ? undefined : live.entered.treatment?.name);
73
+ if (!rx && text) {
74
+ setTreatment(text);
75
+ setSentTreatment(text); // already sent: leaving the field mustn't send it again
76
+ }
77
+ }
78
+
79
+ // The pick's name — never its id (a drug given by id alone is named once it's drafted).
80
+ const [shownPick, setShownPick] = useState(pickId);
81
+ if (pickId !== shownPick) {
82
+ setShownPick(pickId);
83
+ const name = pickId && live?.name;
84
+ if (name) {
85
+ setTreatment(name);
86
+ setSentTreatment(name);
87
+ }
88
+ }
89
+
90
+ const picker = useRef<TreatmentPickerHandle | null>(null);
91
+ // Treatment works like a select: the drug picked from the list (its name is the field's value), and whether the
92
+ // list of drugs is open — else the picked drug's form and strength show under the field.
93
+ const [drug, setDrug] = useState<string | null>(null);
94
+ const [listOpen, setListOpen] = useState(true);
95
+ // The Input building block takes no ref, so focus reaches its field through a wrapper.
96
+ const searchBox = useRef<HTMLDivElement>(null);
97
+ const focusField = () => searchBox.current?.querySelector("input")?.focus();
98
+ useEffect(() => {
99
+ if (focusSignal) focusField();
100
+ }, [focusSignal]);
101
+
102
+ // Only send free-text medication when it actually changed. Otherwise clicking one of the server's
103
+ // choices blurs this field first, re-sends the same text, and the "saving" state swallows the click.
104
+ const submitTreatment = () => {
105
+ const text = treatment.trim();
106
+ if (!text || text === sentTreatment || text === rx?.treatment.name) return;
107
+ setSentTreatment(text);
108
+ save({ treatment: { name: text } }, "treatment");
109
+ };
110
+ // Type-ahead: once there are 3+ characters, the medication text is sent when typing pauses — before the
111
+ // record it's a lookup (the object aborts the one for older text), and the server's choices render live
112
+ // below. Enter / blur send at once; unchanged text is never re-sent (so a wait that ends after them is a no-op).
113
+ // Not two, as a patient's name: two letters of a drug match thousands of products.
114
+ const typeAhead = useTypeAhead(submitTreatment, { minChars: 3 });
115
+
116
+ // A drug picked: its name in the field (already looked up — leaving the field mustn't send it), the list closed.
117
+ const showDrug = (name: string | null) => {
118
+ typeAhead.cancel();
119
+ setDrug(name);
120
+ setListOpen(false);
121
+ setTreatment(name ?? "");
122
+ setSentTreatment(name ?? "");
123
+ };
124
+
125
+ // The drugs to pick from: none while a product chosen saves (it heads the card at once).
126
+ const shownChoices = choosing ? [] : drugChoices;
127
+ const pickerUp = shownChoices.length > 0;
128
+
129
+ // A few drugs for what's typed, and the prescriber is still in Treatment: focus goes to the first, ready to
130
+ // pick — typing carries on in Treatment (search.tsx). Once per answer.
131
+ const choicesFor = drugChoices[0]
132
+ ? `${drugChoices[0].inputText}|${drugChoices[0].options.map((o) => o.displayName).join("|")}`
133
+ : null;
134
+ // A new answer opens the list on its drugs, nothing picked.
135
+ const [listedFor, setListedFor] = useState(choicesFor);
136
+ if (choicesFor !== listedFor) {
137
+ setListedFor(choicesFor);
138
+ if (choicesFor) {
139
+ setDrug(null);
140
+ setListOpen(true);
141
+ }
142
+ }
143
+ const focusedFor = useRef<string | null>(null);
144
+ const focusFew = useLatestCallback(() => {
145
+ const field = searchBox.current?.querySelector("input");
146
+ const answered = treatment.trim() === sentTreatment && !busy && !typeAhead.waiting();
147
+ if (!choicesFor || !answered || !field || document.activeElement !== field) return;
148
+ if (focusedFor.current === choicesFor || (picker.current?.total ?? FEW) >= FEW) return;
149
+ focusedFor.current = choicesFor;
150
+ picker.current!.focus(true);
151
+ });
152
+ useEffect(() => focusFew(), [choicesFor, busy, focusFew]);
153
+
154
+ if (!open) return null;
155
+ return (
156
+ <div style={{ display: "contents" }} {...treatmentField}>
157
+ {/* Where the card flow (← / →) lands: the drug list while choosing, else Treatment. */}
158
+ <div ref={searchBox} {...searchProps} {...(pickerUp ? {} : focusProps)}>
159
+ <Input
160
+ label="Treatment"
161
+ autoFocus={autoFocus}
162
+ value={treatment}
163
+ onChange={(e) => {
164
+ setTreatment(e.target.value);
165
+ setDrug(null); // typing searches anew: the list, with no drug picked
166
+ setListOpen(true);
167
+ typeAhead.typed("treatment", e.target.value);
168
+ }}
169
+ // The picked drug's field, clicked: the last search's drugs again, without searching.
170
+ onClick={() => drug && !listOpen && setListOpen(true)}
171
+ onBlur={submitTreatment}
172
+ loading={spinning === "treatment"}
173
+ clearLabel="Clear treatment"
174
+ onClear={() => {
175
+ showDrug(null);
176
+ focusField();
177
+ }}
178
+ onKeyDown={(e) => {
179
+ // Esc with the list open over a picked drug: closed again, back to its form and strength.
180
+ if (e.key === "Escape" && drug && listOpen) {
181
+ setListOpen(false);
182
+ return e.preventDefault();
183
+ }
184
+ const choices = pickerUp ? picker.current : null;
185
+ // A search field over the drugs (searchKeys); and Enter is on the open drug when the
186
+ // choices are for what's typed (chosen if it's one product, else on to its chips).
187
+ const answered = treatment.trim() === sentTreatment && !busy;
188
+ if (searchKeys(e, choices, answered)) return;
189
+ if (e.key === "Enter" && choices && treatment.trim() === sentTreatment) choices.enter();
190
+ else if (e.key === "Enter") submitTreatment();
191
+ else return;
192
+ e.preventDefault();
193
+ }}
194
+ />
195
+ </div>
196
+ {/* Which drug? — grouped by drug / form / strength. */}
197
+ {pickerUp && (
198
+ <div {...focusProps}>
199
+ {shownChoices.map((change) => (
200
+ <TreatmentPicker
201
+ // Each search's own: what's picked in one doesn't carry over to the next.
202
+ key={`${change.key}:${change.inputText ?? ""}`}
203
+ change={change}
204
+ drug={drug}
205
+ listOpen={listOpen}
206
+ onDrug={(name) => {
207
+ treatmentField.onChange(true);
208
+ showDrug(name);
209
+ }}
210
+ onChoose={(option) => onChoose({ change, option })}
211
+ busy={busy}
212
+ handleRef={picker}
213
+ onBack={focusField}
214
+ />
215
+ ))}
216
+ </div>
217
+ )}
218
+ </div>
219
+ );
220
+ }
@@ -0,0 +1,49 @@
1
+ import { z } from "zod";
2
+ import { describe, expect, it } from "vitest";
3
+ import { schemaErrors } from "./schemaErrors.ts";
4
+
5
+ const name = z.string().min(1, "Required");
6
+
7
+ describe("schemaErrors", () => {
8
+ it("returns no errors when the input is valid", () => {
9
+ expect(schemaErrors(z.object({ name }), { name: "Ada" })).toEqual({});
10
+ });
11
+
12
+ it("returns error messages in the same shape as the input", () => {
13
+ const schema = z.object({ first: name, last: name, age: z.number().gt(0, "Must be greater than 0") });
14
+ expect(schemaErrors(schema, { first: "", last: "Lovelace", age: 0 })).toEqual({
15
+ first: "Required",
16
+ age: "Must be greater than 0",
17
+ });
18
+ });
19
+
20
+ it("returns nested error message when the input is also nested", () => {
21
+ const schema = z.object({ patient: z.object({ name: z.object({ first: name, last: name }) }) });
22
+ expect(schemaErrors(schema, { patient: { name: { first: "", last: "Lovelace" } } })).toEqual({
23
+ patient: { name: { first: "Required" } },
24
+ });
25
+ });
26
+
27
+ it("returns messages that are for a parent and its nested children", () => {
28
+ const schema = z.object({
29
+ range: z
30
+ .object({ from: z.number(), to: z.number().gt(0, "Must be greater than 0") })
31
+ .refine((r) => r.from <= r.to, { message: "From must come first", path: [] }),
32
+ });
33
+ // zod checks the whole range only once its parts parse.
34
+ expect(schemaErrors(schema, { range: { from: 1, to: 0 } })).toEqual({ range: { to: "Must be greater than 0" } });
35
+ expect(schemaErrors(schema, { range: { from: 5, to: 2 } })).toEqual({ range: "From must come first" });
36
+ });
37
+
38
+ it("returns an array item's error messages at its index", () => {
39
+ const schema = z.object({ refills: z.array(z.object({ date: name })) });
40
+ expect(schemaErrors(schema, { refills: [{ date: "2026-11-06" }, { date: "" }] })).toEqual({
41
+ refills: { 1: { date: "Required" } },
42
+ });
43
+ });
44
+
45
+ it("returns only a field's first message when it has several", () => {
46
+ const schema = z.object({ code: z.string().min(3, "Too short").regex(/^\d+$/, "Digits only") });
47
+ expect(schemaErrors(schema, { code: "a" })).toEqual({ code: "Too short" });
48
+ });
49
+ });
@@ -0,0 +1,33 @@
1
+ import type { z } from "zod";
2
+
3
+ /** Simplified error object shape that mirros the Zod schema shape */
4
+ export type SchemaErrors<T> = T extends readonly (infer Item)[]
5
+ ? { [index: number]: SchemaErrors<Item> | undefined }
6
+ : T extends object
7
+ ? { [K in keyof T]?: SchemaErrors<T[K]> }
8
+ : string;
9
+
10
+ type Node = Record<PropertyKey, unknown>;
11
+
12
+ /**
13
+ * Return errors from Zod schema.parse in a friendlier shape
14
+ */
15
+ export function schemaErrors<S extends z.ZodType<unknown, object>>(
16
+ schema: S,
17
+ input: z.input<S>,
18
+ ): SchemaErrors<z.input<S>> {
19
+ const errors: Node = {};
20
+ const result = schema.safeParse(input);
21
+ if (result.success) return errors as SchemaErrors<z.input<S>>;
22
+ for (const issue of result.error.issues) {
23
+ if (issue.path.length === 0) continue;
24
+ let node: Node | undefined = errors;
25
+ for (const key of issue.path.slice(0, -1)) {
26
+ const next: unknown = (node[key] ??= {});
27
+ node = typeof next === "object" ? (next as Node) : undefined;
28
+ if (!node) break;
29
+ }
30
+ if (node) node[issue.path.at(-1)!] ??= issue.message;
31
+ }
32
+ return errors as SchemaErrors<z.input<S>>;
33
+ }
@@ -0,0 +1,377 @@
1
+ /**
2
+ * Every search on the cards works one way — patients, drugs, allergies, medications, diagnoses, and the
3
+ * server's other choices:
4
+ *
5
+ * - Type-ahead (`useTypeAhead`): once a field has the caller's `minChars` (each search says its own: the
6
+ * patient search 2, drugs and clinical entries 3) and typing pauses, it searches; each
7
+ * keystroke cancels the search waiting, and the live object aborts one in flight. Leaving the field (or
8
+ * Enter) sends at once. Patients and drugs search without writing; for allergies, meds and diagnoses a
9
+ * search is an add — a unique match is the entry (see ClinicalList).
10
+ * - Results (`SearchResults`): the first five, then "Show N more". In the list, 1–9 and 0 pick one (of those shown),
11
+ * ↑ ↓ (or K J) move, Home / End (or g / G) go to the first / last, and Esc goes back to the field that searched
12
+ * (← → and h l are the card flow's, / its search box: flow.ts).
13
+ * The one picked reads as selected while its call runs. Pressing one isn't leaving the field: the field
14
+ * doesn't send or open more of the form under the pointer (`pointing`).
15
+ * - A few results (fewer than five) for what's typed, for a patient or a drug: focus goes to the first, ready to
16
+ * pick (`focus(true)`). Typing carries on: until a key acts on the list, one that types (a character,
17
+ * Backspace) goes back to the field and types there (`typesBack`).
18
+ * - From the field (`searchKeys`): Option/Alt + 1–9 / 0 picks one (plain digits stay text: "amoxicillin 500"),
19
+ * ↓ moves into the list, and Tab does when there are only a few results and they answer what's typed.
20
+ * - The field whose call is in flight shows the spinner — nowhere else.
21
+ */
22
+ import { useEffect, useId, useRef, useState, type KeyboardEvent } from "react";
23
+ import { useLatestCallback } from "../hooks/index.ts";
24
+ import { useComponents } from "../primitives/index.ts";
25
+ import { CHOICE_ACTION, type ChoiceListProps } from "../primitives/types.ts";
26
+
27
+ /** A search waits until typing has paused this long. */
28
+ const PAUSE_MS = 300;
29
+ /** Results shown before "Show N more". */
30
+ export const SHOWN = 5;
31
+ /** Fewer results than this are few enough that focus goes to them (more: keep narrowing). */
32
+ export const FEW = 5;
33
+
34
+ export interface TypeAhead<F extends string> {
35
+ /** `field` changed to `text`: the search waiting is dropped, and (`minChars`+) a new one waits for a pause. */
36
+ typed: (field: F, text: string) => void;
37
+ /** Too little to search on — under `minChars` once trimmed. */
38
+ tooShort: (text: string) => boolean;
39
+ /** Drop the search waiting — e.g. leaving the field sends now instead. */
40
+ cancel: () => void;
41
+ /** A search is waiting for typing to pause. */
42
+ waiting: () => boolean;
43
+ }
44
+
45
+ /** Type-ahead for a card's search fields: `search(field)` runs once `minChars` are typed and typing pauses. */
46
+ export function useTypeAhead<F extends string>(
47
+ search: (field: F) => void,
48
+ { minChars }: { minChars: number },
49
+ ): TypeAhead<F> {
50
+ const latest = useLatestCallback(search);
51
+ const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
52
+ const cancel = () => {
53
+ if (timer.current) clearTimeout(timer.current);
54
+ timer.current = null;
55
+ };
56
+ useEffect(() => cancel, []);
57
+ const tooShort = (text: string) => text.trim().length < minChars;
58
+ return {
59
+ typed: (field, text) => {
60
+ cancel();
61
+ if (tooShort(text)) return;
62
+ timer.current = setTimeout(() => {
63
+ timer.current = null;
64
+ latest(field);
65
+ }, PAUSE_MS);
66
+ },
67
+ tooShort,
68
+ cancel,
69
+ waiting: () => timer.current !== null,
70
+ };
71
+ }
72
+
73
+ // Whether a pointer is pressed anywhere on the page (one listener for every card).
74
+ let pressing = false;
75
+ let pressEnded: (() => void)[] = [];
76
+ if (typeof document !== "undefined") {
77
+ document.addEventListener("pointerdown", () => (pressing = true), true);
78
+ const end = () => {
79
+ pressing = false;
80
+ const queued = pressEnded;
81
+ pressEnded = [];
82
+ setTimeout(() => queued.forEach((run) => run())); // after the press's click
83
+ };
84
+ document.addEventListener("pointerup", end, true);
85
+ document.addEventListener("pointercancel", end, true);
86
+ }
87
+
88
+ /**
89
+ * Run `fn` now — or, if a press took focus from a field, once that press (and its click) is done. Leaving a
90
+ * field can close it or open more of the form; doing that mid-press moves whatever was pressed out from
91
+ * under the pointer, and the click lands somewhere else.
92
+ */
93
+ export function afterPress(fn: () => void) {
94
+ if (pressing) pressEnded.push(fn);
95
+ else fn();
96
+ }
97
+
98
+ /**
99
+ * 1–9 then 0 as a 0-based index (0 is the tenth), or -1. By physical key first: Option + 1 on a Mac reports
100
+ * key "¡". Plain digits also match by key, for keyboards whose number row isn't Digit1…
101
+ */
102
+ export function digitKey(e: { key: string; code: string; altKey: boolean }): number {
103
+ const code = /^(?:Digit|Numpad)([0-9])$/.exec(e.code)?.[1];
104
+ const key = !e.altKey && /^[0-9]$/.test(e.key) ? e.key : undefined;
105
+ const digit = code ?? key;
106
+ return digit ? (Number(digit) + 9) % 10 : -1;
107
+ }
108
+
109
+ /**
110
+ * A key a card acts on stops there: handled, it's not also a host page's shortcut (a host's own `j` or `/`).
111
+ * Keys the cards don't use pass through untouched.
112
+ */
113
+ export function took(e: { preventDefault(): void; stopPropagation(): void }) {
114
+ e.preventDefault();
115
+ e.stopPropagation();
116
+ }
117
+
118
+ /** In a list: Home or g is the first item, End or G the last; null for any other key. */
119
+ export function edge(e: { key: string; altKey: boolean; metaKey: boolean; ctrlKey: boolean }): "first" | "last" | null {
120
+ if (e.altKey || e.metaKey || e.ctrlKey) return null;
121
+ return e.key === "Home" || e.key === "g" ? "first" : e.key === "End" || e.key === "G" ? "last" : null;
122
+ }
123
+
124
+ /**
125
+ * A key that types — a character, Backspace, Delete. In a list focus was put in for the prescriber (a few results
126
+ * came back), it goes back to the field instead: the list sends focus there and lets the key through, so the
127
+ * browser types it in the field.
128
+ */
129
+ export function typesBack(e: { key: string; altKey: boolean; metaKey: boolean; ctrlKey: boolean }): boolean {
130
+ if (e.altKey || e.metaKey || e.ctrlKey) return false;
131
+ return e.key.length === 1 || e.key === "Backspace" || e.key === "Delete";
132
+ }
133
+
134
+ /** In a list: ↓ or J is the next item (1), ↑ or K the previous (-1); 0 for any other key. */
135
+ export function vertical(e: { key: string; altKey: boolean; metaKey: boolean; ctrlKey: boolean }): number {
136
+ if (e.altKey || e.metaKey || e.ctrlKey) return 0;
137
+ return e.key === "ArrowDown" || e.key === "j" ? 1 : e.key === "ArrowUp" || e.key === "k" ? -1 : 0;
138
+ }
139
+
140
+ /** How a search field reaches its results (`SearchResults`, or the drug picker). */
141
+ export interface SearchHandle {
142
+ /** How many results there are, shown or not. */
143
+ total: number;
144
+ /**
145
+ * Put focus on the first result; false if there's none. `typing`: the field put it there (a few results came
146
+ * back) — keys that type go back to it until one acts on the list.
147
+ */
148
+ focus: (typing?: boolean) => boolean;
149
+ /** Pick the result at this index (0-based) — the drug picker opens it; false if there's none shown there. */
150
+ pick: (index: number) => boolean;
151
+ /** A result is being pressed: the field's blur is the press, not the prescriber leaving it. */
152
+ pointing?: () => boolean;
153
+ /**
154
+ * Set when the field keeps the cursor (`inField`). The keys then move a highlight rather than focus, so
155
+ * the prescriber can carry on typing at any point — which is what a search box that takes a name, a date
156
+ * or a phone has to allow. `activeId` is the highlighted option's DOM id, for `aria-activedescendant`.
157
+ */
158
+ inField?: true;
159
+ /** Move the highlight by `step`, wrapping at the ends; false when there is nothing to move. */
160
+ move?: (step: number) => boolean;
161
+ /** Take the highlighted row; false when nothing is highlighted. */
162
+ accept?: () => boolean;
163
+ /** Drop the highlight; false when there was none. */
164
+ clear?: () => boolean;
165
+ activeId?: string | null;
166
+ }
167
+
168
+ /**
169
+ * A search field's keys (its onKeyDown): Option/Alt + 1–9 / 0 picks a result, ↓ moves into them, and Tab does
170
+ * when they're few and `answered` (they're for what's in the field now). True when it took the key.
171
+ *
172
+ * A field that keeps the cursor (`inField`) takes only ↑ ↓, Return and Esc, and never gives up focus — no
173
+ * j / k, no digits, no Tab into the list: every one of those is a character someone may be typing, and a
174
+ * field that takes a name, a date or a phone can't quietly swallow them.
175
+ */
176
+ export function searchKeys(e: KeyboardEvent, results: SearchHandle | null | undefined, answered: boolean): boolean {
177
+ if (!results?.total) return false;
178
+ if (results.inField) {
179
+ if (e.altKey || e.metaKey || e.ctrlKey) return false;
180
+ const step = e.key === "ArrowDown" ? 1 : e.key === "ArrowUp" ? -1 : 0;
181
+ const took_ =
182
+ step !== 0
183
+ ? results.move?.(step)
184
+ : e.key === "Enter"
185
+ ? results.accept?.()
186
+ : e.key === "Escape"
187
+ ? results.clear?.()
188
+ : false;
189
+ if (took_) took(e);
190
+ return Boolean(took_);
191
+ }
192
+ const digit = e.altKey ? digitKey(e) : -1;
193
+ const handled =
194
+ digit >= 0
195
+ ? results.pick(digit)
196
+ : e.key === "ArrowDown"
197
+ ? results.focus()
198
+ : e.key === "Tab" && !e.shiftKey && answered && results.total < FEW && results.focus();
199
+ if (handled) took(e);
200
+ return handled;
201
+ }
202
+
203
+ /**
204
+ * A search's results: the first five, then "Show N more". In the list, 1–9 / 0 picks one and ↑ ↓ (or K J) move. `busy`: the
205
+ * object is working — the result picked reads as selected until it's done, and another pick waits for it.
206
+ */
207
+ export function SearchResults({
208
+ label,
209
+ results,
210
+ onPick,
211
+ busy,
212
+ disabled,
213
+ handleRef,
214
+ onBack,
215
+ action,
216
+ inField,
217
+ onActive,
218
+ }: {
219
+ label: string;
220
+ results: ChoiceListProps["choices"];
221
+ onPick: (key: string) => void;
222
+ busy?: boolean;
223
+ disabled?: boolean;
224
+ handleRef?: { current: SearchHandle | null };
225
+ /** Esc in the list: back to the field that searched. */
226
+ onBack?: () => void;
227
+ /**
228
+ * The way out of the list, after the results — the patient card's "Add new patient". It renders as the
229
+ * last row, so ↓ carries on into it from the last result and ↑ comes back.
230
+ */
231
+ action?: ChoiceListProps["action"];
232
+ /**
233
+ * The field below keeps the cursor: the rows become options it highlights, so typing, Backspace and
234
+ * Delete always reach the field — the prescriber can narrow or start over without first coming back.
235
+ */
236
+ inField?: boolean;
237
+ /** With `inField`: the highlighted option's DOM id, for the field's `aria-activedescendant`. */
238
+ onActive?: (id: string | null) => void;
239
+ }) {
240
+ const { Button, ChoiceList, Stack } = useComponents();
241
+ const listId = useId();
242
+ const [showAll, setShowAll] = useState(false);
243
+ /** Where the keys are, when the field keeps the cursor. Null until ↓ or an answer puts it somewhere. */
244
+ const [active, setActive] = useState<string | null>(null);
245
+ useEffect(() => {
246
+ onActive?.(active ? `${listId}-${active}` : null);
247
+ }, [active, listId, onActive]);
248
+ const shown = showAll ? results : results.slice(0, SHOWN);
249
+ const list = useRef<HTMLDivElement>(null);
250
+ const pointing = useRef(false);
251
+ // Focus was put here for the prescriber, mid-typing: keys that type go back to the field (typesBack).
252
+ const typing = useRef(false);
253
+ const buttons = () => [...(list.current?.querySelectorAll<HTMLElement>("button:not(:disabled)") ?? [])];
254
+ // The pick, while its call runs (it starts at once: the object goes busy as it's picked).
255
+ const [picked, setPicked] = useState<{ key: string; started: boolean } | null>(null);
256
+ if (picked && busy && !picked.started) setPicked({ ...picked, started: true });
257
+ if (picked?.started && !busy) setPicked(null);
258
+ const pickKey = (key: string) => {
259
+ if (disabled || (picked && busy)) return;
260
+ setPicked({ key, started: false });
261
+ onPick(key);
262
+ };
263
+ const pick = (index: number) => {
264
+ const result = !disabled ? shown[index] : undefined;
265
+ if (result) pickKey(result.key);
266
+ return Boolean(result);
267
+ };
268
+ /** Every row the keys run through, in order: the results shown, then the way out. */
269
+ const keys = [...shown.map((r) => r.key), ...(action ? [CHOICE_ACTION] : [])];
270
+ const take = (key: string | null) => {
271
+ if (!key) return false;
272
+ if (key === CHOICE_ACTION) action?.onChoose();
273
+ else pickKey(key);
274
+ return true;
275
+ };
276
+ useEffect(() => {
277
+ if (!handleRef) return;
278
+ handleRef.current = {
279
+ // The action counts: with nobody matching it's the only row, and ↓ from the field must still reach it.
280
+ total: results.length + (action ? 1 : 0),
281
+ focus: (fromTyping) => {
282
+ if (inField) {
283
+ // The cursor stays where it is; the first row is simply where the keys now are.
284
+ setActive(keys[0] ?? null);
285
+ return keys.length > 0;
286
+ }
287
+ const first = buttons()[0];
288
+ typing.current = Boolean(fromTyping && first && onBack);
289
+ first?.focus();
290
+ return Boolean(first);
291
+ },
292
+ pick,
293
+ pointing: () => pointing.current,
294
+ ...(inField
295
+ ? {
296
+ inField: true as const,
297
+ // The ends are ends: leaning on ↓ parks on the last row and stops, rather than looping round
298
+ // to the top where Return would take someone else entirely. ↓ is the way in.
299
+ move: (step: number) => {
300
+ if (!keys.length) return false;
301
+ const at = active ? keys.indexOf(active) : -1;
302
+ if (at < 0) {
303
+ if (step < 0) return false;
304
+ setActive(keys[0]!);
305
+ return true;
306
+ }
307
+ const next = Math.max(0, Math.min(keys.length - 1, at + step));
308
+ if (next === at) return true; // at the end: taken, so the page doesn't scroll instead
309
+ setActive(keys[next]!);
310
+ return true;
311
+ },
312
+ accept: () => (disabled ? false : take(active)),
313
+ clear: () => {
314
+ if (active === null) return false;
315
+ setActive(null);
316
+ return true;
317
+ },
318
+ activeId: active ? `${listId}-${active}` : null,
319
+ }
320
+ : {}),
321
+ };
322
+ return () => {
323
+ handleRef.current = null;
324
+ };
325
+ });
326
+
327
+ const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
328
+ if (typing.current && typesBack(e)) return onBack?.(); // not taken: the browser types it in the field
329
+ typing.current = false;
330
+ if (e.metaKey || e.ctrlKey) return;
331
+ const digit = digitKey(e);
332
+ const all = buttons();
333
+ const at = all.indexOf(e.target as HTMLElement);
334
+ if (digit >= 0) {
335
+ if (!pick(digit)) return;
336
+ } else if (vertical(e) && at >= 0) {
337
+ all[Math.max(0, Math.min(all.length - 1, at + vertical(e)))]?.focus();
338
+ } else if (edge(e) && at >= 0) {
339
+ all[edge(e) === "first" ? 0 : all.length - 1]?.focus();
340
+ } else if (e.key === "Escape" && onBack) {
341
+ onBack();
342
+ } else return;
343
+ took(e);
344
+ };
345
+
346
+ return (
347
+ <Stack gap="1">
348
+ <div
349
+ ref={list}
350
+ onKeyDown={onKeyDown}
351
+ onBlur={(e) => !list.current?.contains(e.relatedTarget) && (typing.current = false)}
352
+ onPointerDown={() => {
353
+ // Until the press ends: the field it leaves mustn't send, or open rows that move the list under it.
354
+ pointing.current = true;
355
+ window.addEventListener("pointerup", () => setTimeout(() => (pointing.current = false)), { once: true });
356
+ }}
357
+ >
358
+ <ChoiceList
359
+ label={label}
360
+ choices={shown}
361
+ onChoose={pickKey}
362
+ disabled={disabled}
363
+ pendingKey={picked && busy ? picked.key : null}
364
+ action={action}
365
+ {...(inField ? { activeKey: keys.includes(active ?? "") ? active : null, id: listId } : {})}
366
+ />
367
+ </div>
368
+ {results.length > shown.length && (
369
+ <div>
370
+ <Button variant="ghost" onClick={() => setShowAll(true)}>
371
+ Show {results.length - shown.length} more
372
+ </Button>
373
+ </div>
374
+ )}
375
+ </Stack>
376
+ );
377
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./useLatestCallback.ts";
2
+ export * from "./useLive.ts";
3
+ export * from "./useOnChange.ts";
4
+ export * from "./useSession.ts";