@assistant-ui/react-generative-ui 0.0.24 → 0.1.0

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 (338) hide show
  1. package/README.md +2 -0
  2. package/dist/a2ui.d.ts +1 -4
  3. package/dist/a2ui.d.ts.map +1 -1
  4. package/dist/a2ui.js +1 -4
  5. package/dist/index.d.ts +1 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -10
  8. package/dist/index.server.d.ts +1 -2
  9. package/dist/index.server.d.ts.map +1 -1
  10. package/dist/index.server.js +1 -10
  11. package/dist/ir.d.ts +1 -111
  12. package/dist/ir.d.ts.map +1 -1
  13. package/dist/ir.js +1 -175
  14. package/dist/slack.d.ts +1 -4
  15. package/dist/slack.d.ts.map +1 -1
  16. package/dist/slack.js +1 -4
  17. package/dist/teams.d.ts +1 -4
  18. package/dist/teams.d.ts.map +1 -1
  19. package/dist/teams.js +1 -4
  20. package/package.json +4 -5
  21. package/src/a2ui.ts +1 -22
  22. package/src/index.server.ts +1 -2
  23. package/src/index.test.ts +75 -0
  24. package/src/index.ts +1 -2
  25. package/src/ir.ts +1 -279
  26. package/src/slack.ts +1 -39
  27. package/src/teams.ts +1 -35
  28. package/dist/JSONGenerativeUI.client.d.ts +0 -28
  29. package/dist/JSONGenerativeUI.client.d.ts.map +0 -1
  30. package/dist/JSONGenerativeUI.client.js +0 -101
  31. package/dist/JSONGenerativeUI.client.js.map +0 -1
  32. package/dist/JSONGenerativeUI.server.d.ts +0 -21
  33. package/dist/JSONGenerativeUI.server.d.ts.map +0 -1
  34. package/dist/JSONGenerativeUI.server.js +0 -32
  35. package/dist/JSONGenerativeUI.server.js.map +0 -1
  36. package/dist/JSONGenerativeUI.shared.d.ts +0 -63
  37. package/dist/JSONGenerativeUI.shared.d.ts.map +0 -1
  38. package/dist/JSONGenerativeUI.shared.js +0 -31
  39. package/dist/JSONGenerativeUI.shared.js.map +0 -1
  40. package/dist/RadioGroupScope.d.ts +0 -6
  41. package/dist/RadioGroupScope.d.ts.map +0 -1
  42. package/dist/RadioGroupScope.js +0 -25
  43. package/dist/RadioGroupScope.js.map +0 -1
  44. package/dist/a2ui/BindingContext.d.ts +0 -11
  45. package/dist/a2ui/BindingContext.d.ts.map +0 -1
  46. package/dist/a2ui/BindingContext.js +0 -12
  47. package/dist/a2ui/BindingContext.js.map +0 -1
  48. package/dist/a2ui/PresentRenderer.d.ts +0 -11
  49. package/dist/a2ui/PresentRenderer.d.ts.map +0 -1
  50. package/dist/a2ui/PresentRenderer.js +0 -39
  51. package/dist/a2ui/PresentRenderer.js.map +0 -1
  52. package/dist/a2ui/convert.d.ts +0 -15
  53. package/dist/a2ui/convert.d.ts.map +0 -1
  54. package/dist/a2ui/convert.js +0 -728
  55. package/dist/a2ui/convert.js.map +0 -1
  56. package/dist/a2ui/dataModel.d.ts +0 -9
  57. package/dist/a2ui/dataModel.d.ts.map +0 -1
  58. package/dist/a2ui/dataModel.js +0 -44
  59. package/dist/a2ui/dataModel.js.map +0 -1
  60. package/dist/a2ui/reducer.d.ts +0 -8
  61. package/dist/a2ui/reducer.d.ts.map +0 -1
  62. package/dist/a2ui/reducer.js +0 -242
  63. package/dist/a2ui/reducer.js.map +0 -1
  64. package/dist/a2ui/snapshot.d.ts +0 -3
  65. package/dist/a2ui/snapshot.d.ts.map +0 -1
  66. package/dist/a2ui/snapshot.js +0 -33
  67. package/dist/a2ui/snapshot.js.map +0 -1
  68. package/dist/a2ui/types.d.ts +0 -88
  69. package/dist/a2ui/types.d.ts.map +0 -1
  70. package/dist/a2ui/types.js +0 -6
  71. package/dist/a2ui/types.js.map +0 -1
  72. package/dist/a2ui/valueFunctions.d.ts +0 -16
  73. package/dist/a2ui/valueFunctions.d.ts.map +0 -1
  74. package/dist/a2ui/valueFunctions.js +0 -332
  75. package/dist/a2ui/valueFunctions.js.map +0 -1
  76. package/dist/actionRegistry.d.ts +0 -29
  77. package/dist/actionRegistry.d.ts.map +0 -1
  78. package/dist/actionRegistry.js +0 -41
  79. package/dist/actionRegistry.js.map +0 -1
  80. package/dist/answeredValues.d.ts +0 -16
  81. package/dist/answeredValues.d.ts.map +0 -1
  82. package/dist/answeredValues.js +0 -37
  83. package/dist/answeredValues.js.map +0 -1
  84. package/dist/buildPresentParameters.d.ts +0 -21
  85. package/dist/buildPresentParameters.d.ts.map +0 -1
  86. package/dist/buildPresentParameters.js +0 -95
  87. package/dist/buildPresentParameters.js.map +0 -1
  88. package/dist/constants.d.ts +0 -15
  89. package/dist/constants.d.ts.map +0 -1
  90. package/dist/constants.js +0 -19
  91. package/dist/constants.js.map +0 -1
  92. package/dist/convert/boundSpec.d.ts +0 -48
  93. package/dist/convert/boundSpec.d.ts.map +0 -1
  94. package/dist/convert/boundSpec.js +0 -108
  95. package/dist/convert/boundSpec.js.map +0 -1
  96. package/dist/convert/copyBounded.d.ts +0 -25
  97. package/dist/convert/copyBounded.d.ts.map +0 -1
  98. package/dist/convert/copyBounded.js +0 -38
  99. package/dist/convert/copyBounded.js.map +0 -1
  100. package/dist/convert/isElement.d.ts +0 -3
  101. package/dist/convert/isElement.d.ts.map +0 -1
  102. package/dist/convert/isElement.js +0 -6
  103. package/dist/convert/isElement.js.map +0 -1
  104. package/dist/convert/takeRun.d.ts +0 -6
  105. package/dist/convert/takeRun.d.ts.map +0 -1
  106. package/dist/convert/takeRun.js +0 -20
  107. package/dist/convert/takeRun.js.map +0 -1
  108. package/dist/defineGenerativeComponents.d.ts +0 -29
  109. package/dist/defineGenerativeComponents.d.ts.map +0 -1
  110. package/dist/defineGenerativeComponents.js +0 -34
  111. package/dist/defineGenerativeComponents.js.map +0 -1
  112. package/dist/fieldReferences.d.ts +0 -9
  113. package/dist/fieldReferences.d.ts.map +0 -1
  114. package/dist/fieldReferences.js +0 -53
  115. package/dist/fieldReferences.js.map +0 -1
  116. package/dist/generativeUIToJSX.d.ts +0 -18
  117. package/dist/generativeUIToJSX.d.ts.map +0 -1
  118. package/dist/generativeUIToJSX.js +0 -76
  119. package/dist/generativeUIToJSX.js.map +0 -1
  120. package/dist/index.shared.d.ts +0 -15
  121. package/dist/index.shared.d.ts.map +0 -1
  122. package/dist/index.shared.js +0 -9
  123. package/dist/ir.js.map +0 -1
  124. package/dist/renderGenerativeUI.d.ts +0 -14
  125. package/dist/renderGenerativeUI.d.ts.map +0 -1
  126. package/dist/renderGenerativeUI.js +0 -83
  127. package/dist/renderGenerativeUI.js.map +0 -1
  128. package/dist/scopeSchema.d.ts +0 -6
  129. package/dist/scopeSchema.d.ts.map +0 -1
  130. package/dist/scopeSchema.js +0 -56
  131. package/dist/scopeSchema.js.map +0 -1
  132. package/dist/slack/constants.d.ts +0 -93
  133. package/dist/slack/constants.d.ts.map +0 -1
  134. package/dist/slack/constants.js +0 -146
  135. package/dist/slack/constants.js.map +0 -1
  136. package/dist/slack/decodeBlockAction.d.ts +0 -10
  137. package/dist/slack/decodeBlockAction.d.ts.map +0 -1
  138. package/dist/slack/decodeBlockAction.js +0 -129
  139. package/dist/slack/decodeBlockAction.js.map +0 -1
  140. package/dist/slack/fromSlackBlocks.d.ts +0 -27
  141. package/dist/slack/fromSlackBlocks.d.ts.map +0 -1
  142. package/dist/slack/fromSlackBlocks.js +0 -483
  143. package/dist/slack/fromSlackBlocks.js.map +0 -1
  144. package/dist/slack/toSlackBlocks.d.ts +0 -4
  145. package/dist/slack/toSlackBlocks.d.ts.map +0 -1
  146. package/dist/slack/toSlackBlocks.js +0 -901
  147. package/dist/slack/toSlackBlocks.js.map +0 -1
  148. package/dist/slack/types.d.ts +0 -204
  149. package/dist/slack/types.d.ts.map +0 -1
  150. package/dist/slack/types.js +0 -0
  151. package/dist/teams/constants.d.ts +0 -34
  152. package/dist/teams/constants.d.ts.map +0 -1
  153. package/dist/teams/constants.js +0 -68
  154. package/dist/teams/constants.js.map +0 -1
  155. package/dist/teams/decodeSubmitData.d.ts +0 -26
  156. package/dist/teams/decodeSubmitData.d.ts.map +0 -1
  157. package/dist/teams/decodeSubmitData.js +0 -70
  158. package/dist/teams/decodeSubmitData.js.map +0 -1
  159. package/dist/teams/temporalId.d.ts +0 -5
  160. package/dist/teams/temporalId.d.ts.map +0 -1
  161. package/dist/teams/temporalId.js +0 -28
  162. package/dist/teams/temporalId.js.map +0 -1
  163. package/dist/teams/toAdaptiveCard.d.ts +0 -45
  164. package/dist/teams/toAdaptiveCard.d.ts.map +0 -1
  165. package/dist/teams/toAdaptiveCard.js +0 -642
  166. package/dist/teams/toAdaptiveCard.js.map +0 -1
  167. package/dist/teams/toTeamsAttachments.d.ts +0 -10
  168. package/dist/teams/toTeamsAttachments.d.ts.map +0 -1
  169. package/dist/teams/toTeamsAttachments.js +0 -99
  170. package/dist/teams/toTeamsAttachments.js.map +0 -1
  171. package/dist/teams/types.d.ts +0 -224
  172. package/dist/teams/types.d.ts.map +0 -1
  173. package/dist/teams/types.js +0 -0
  174. package/dist/temporal.d.ts +0 -33
  175. package/dist/temporal.d.ts.map +0 -1
  176. package/dist/temporal.js +0 -153
  177. package/dist/temporal.js.map +0 -1
  178. package/dist/types.d.ts +0 -99
  179. package/dist/types.d.ts.map +0 -1
  180. package/dist/types.js +0 -0
  181. package/dist/vocabulary/alert.d.ts +0 -32
  182. package/dist/vocabulary/alert.d.ts.map +0 -1
  183. package/dist/vocabulary/alert.js +0 -160
  184. package/dist/vocabulary/alert.js.map +0 -1
  185. package/dist/vocabulary/collectFormValues.d.ts +0 -23
  186. package/dist/vocabulary/collectFormValues.d.ts.map +0 -1
  187. package/dist/vocabulary/collectFormValues.js +0 -44
  188. package/dist/vocabulary/collectFormValues.js.map +0 -1
  189. package/dist/vocabulary/data.d.ts +0 -66
  190. package/dist/vocabulary/data.d.ts.map +0 -1
  191. package/dist/vocabulary/data.js +0 -503
  192. package/dist/vocabulary/data.js.map +0 -1
  193. package/dist/vocabulary/dispatch.d.ts +0 -9
  194. package/dist/vocabulary/dispatch.d.ts.map +0 -1
  195. package/dist/vocabulary/dispatch.js +0 -50
  196. package/dist/vocabulary/dispatch.js.map +0 -1
  197. package/dist/vocabulary/fact.d.ts +0 -19
  198. package/dist/vocabulary/fact.d.ts.map +0 -1
  199. package/dist/vocabulary/fact.js +0 -51
  200. package/dist/vocabulary/fact.js.map +0 -1
  201. package/dist/vocabulary/form.d.ts +0 -11
  202. package/dist/vocabulary/form.d.ts.map +0 -1
  203. package/dist/vocabulary/form.js +0 -23
  204. package/dist/vocabulary/form.js.map +0 -1
  205. package/dist/vocabulary/formatValue.d.ts +0 -7
  206. package/dist/vocabulary/formatValue.d.ts.map +0 -1
  207. package/dist/vocabulary/formatValue.js +0 -76
  208. package/dist/vocabulary/formatValue.js.map +0 -1
  209. package/dist/vocabulary/icon.d.ts +0 -41
  210. package/dist/vocabulary/icon.d.ts.map +0 -1
  211. package/dist/vocabulary/icon.js +0 -272
  212. package/dist/vocabulary/icon.js.map +0 -1
  213. package/dist/vocabulary/index.d.ts +0 -12
  214. package/dist/vocabulary/index.d.ts.map +0 -1
  215. package/dist/vocabulary/index.js +0 -36
  216. package/dist/vocabulary/index.js.map +0 -1
  217. package/dist/vocabulary/interactive.d.ts +0 -230
  218. package/dist/vocabulary/interactive.d.ts.map +0 -1
  219. package/dist/vocabulary/interactive.js +0 -584
  220. package/dist/vocabulary/interactive.js.map +0 -1
  221. package/dist/vocabulary/layout.d.ts +0 -79
  222. package/dist/vocabulary/layout.d.ts.map +0 -1
  223. package/dist/vocabulary/layout.js +0 -139
  224. package/dist/vocabulary/layout.js.map +0 -1
  225. package/dist/vocabulary/list.d.ts +0 -14
  226. package/dist/vocabulary/list.d.ts.map +0 -1
  227. package/dist/vocabulary/list.js +0 -50
  228. package/dist/vocabulary/list.js.map +0 -1
  229. package/dist/vocabulary/media.d.ts +0 -25
  230. package/dist/vocabulary/media.d.ts.map +0 -1
  231. package/dist/vocabulary/media.js +0 -42
  232. package/dist/vocabulary/media.js.map +0 -1
  233. package/dist/vocabulary/text.d.ts +0 -57
  234. package/dist/vocabulary/text.d.ts.map +0 -1
  235. package/dist/vocabulary/text.js +0 -49
  236. package/dist/vocabulary/text.js.map +0 -1
  237. package/dist/vocabulary/toTextContent.d.ts +0 -2
  238. package/dist/vocabulary/toTextContent.d.ts.map +0 -1
  239. package/dist/vocabulary/toTextContent.js +0 -6
  240. package/dist/vocabulary/toTextContent.js.map +0 -1
  241. package/src/JSONGenerativeUI.client.tsx +0 -168
  242. package/src/JSONGenerativeUI.server.tsx +0 -39
  243. package/src/JSONGenerativeUI.shared.ts +0 -90
  244. package/src/JSONGenerativeUI.test.tsx +0 -1298
  245. package/src/RadioGroupScope.server.test.tsx +0 -24
  246. package/src/RadioGroupScope.tsx +0 -34
  247. package/src/a2ui/BindingContext.ts +0 -26
  248. package/src/a2ui/PresentRenderer.tsx +0 -83
  249. package/src/a2ui/convert.test.ts +0 -2923
  250. package/src/a2ui/convert.ts +0 -1207
  251. package/src/a2ui/dataModel.ts +0 -69
  252. package/src/a2ui/reducer.test.ts +0 -422
  253. package/src/a2ui/reducer.ts +0 -363
  254. package/src/a2ui/snapshot.test.ts +0 -52
  255. package/src/a2ui/snapshot.ts +0 -52
  256. package/src/a2ui/types.ts +0 -114
  257. package/src/a2ui/valueFunctions.test.ts +0 -271
  258. package/src/a2ui/valueFunctions.ts +0 -523
  259. package/src/actionRegistry.test.ts +0 -90
  260. package/src/actionRegistry.ts +0 -88
  261. package/src/answeredValues.test.ts +0 -33
  262. package/src/answeredValues.tsx +0 -70
  263. package/src/buildPresentParameters.test.ts +0 -246
  264. package/src/buildPresentParameters.ts +0 -130
  265. package/src/constants.ts +0 -17
  266. package/src/convert/boundSpec.test.ts +0 -192
  267. package/src/convert/boundSpec.ts +0 -134
  268. package/src/convert/copyBounded.test.ts +0 -131
  269. package/src/convert/copyBounded.ts +0 -38
  270. package/src/convert/isElement.ts +0 -6
  271. package/src/convert/takeRun.test.ts +0 -64
  272. package/src/convert/takeRun.ts +0 -19
  273. package/src/defineGenerativeComponents.ts +0 -38
  274. package/src/fieldReferences.test.ts +0 -66
  275. package/src/fieldReferences.ts +0 -83
  276. package/src/generativeUIToJSX.test.ts +0 -268
  277. package/src/generativeUIToJSX.ts +0 -152
  278. package/src/index.server.test.ts +0 -106
  279. package/src/index.shared.ts +0 -61
  280. package/src/ir.test.ts +0 -203
  281. package/src/renderGenerativeUI.test.tsx +0 -373
  282. package/src/renderGenerativeUI.tsx +0 -165
  283. package/src/scopeSchema.test.ts +0 -72
  284. package/src/scopeSchema.ts +0 -71
  285. package/src/slack/a2uiSlack.test.ts +0 -198
  286. package/src/slack/constants.ts +0 -194
  287. package/src/slack/datePicker.test.ts +0 -612
  288. package/src/slack/decodeBlockAction.test.ts +0 -233
  289. package/src/slack/decodeBlockAction.ts +0 -232
  290. package/src/slack/fromSlackBlocks.test.ts +0 -1465
  291. package/src/slack/fromSlackBlocks.ts +0 -789
  292. package/src/slack/toSlackBlocks.test.ts +0 -3065
  293. package/src/slack/toSlackBlocks.ts +0 -1818
  294. package/src/slack/types.ts +0 -264
  295. package/src/teams/constants.ts +0 -93
  296. package/src/teams/decodeSubmitData.test.ts +0 -397
  297. package/src/teams/decodeSubmitData.ts +0 -110
  298. package/src/teams/temporal.test.ts +0 -766
  299. package/src/teams/temporalId.ts +0 -56
  300. package/src/teams/toAdaptiveCard.test.ts +0 -1897
  301. package/src/teams/toAdaptiveCard.ts +0 -1057
  302. package/src/teams/toTeamsAttachments.test.ts +0 -74
  303. package/src/teams/toTeamsAttachments.ts +0 -133
  304. package/src/teams/types.ts +0 -266
  305. package/src/temporal.test.ts +0 -337
  306. package/src/temporal.ts +0 -263
  307. package/src/types.ts +0 -119
  308. package/src/vocabulary/alert.test.tsx +0 -427
  309. package/src/vocabulary/alert.tsx +0 -217
  310. package/src/vocabulary/collectFormValues.test.ts +0 -389
  311. package/src/vocabulary/collectFormValues.ts +0 -116
  312. package/src/vocabulary/data.test.tsx +0 -858
  313. package/src/vocabulary/data.tsx +0 -819
  314. package/src/vocabulary/datepicker.dom.test.tsx +0 -1457
  315. package/src/vocabulary/dispatch.ts +0 -83
  316. package/src/vocabulary/fact.test.tsx +0 -94
  317. package/src/vocabulary/fact.tsx +0 -54
  318. package/src/vocabulary/form.test.tsx +0 -113
  319. package/src/vocabulary/form.tsx +0 -39
  320. package/src/vocabulary/formatValue.test.ts +0 -72
  321. package/src/vocabulary/formatValue.ts +0 -127
  322. package/src/vocabulary/icon.test.tsx +0 -64
  323. package/src/vocabulary/icon.tsx +0 -174
  324. package/src/vocabulary/index.test.ts +0 -65
  325. package/src/vocabulary/index.ts +0 -33
  326. package/src/vocabulary/interactive.dispatch.test.tsx +0 -454
  327. package/src/vocabulary/interactive.dom.test.tsx +0 -1747
  328. package/src/vocabulary/interactive.test.tsx +0 -377
  329. package/src/vocabulary/interactive.tsx +0 -1109
  330. package/src/vocabulary/layout.test.tsx +0 -323
  331. package/src/vocabulary/layout.tsx +0 -247
  332. package/src/vocabulary/list.test.tsx +0 -222
  333. package/src/vocabulary/list.tsx +0 -50
  334. package/src/vocabulary/media.test.tsx +0 -85
  335. package/src/vocabulary/media.tsx +0 -56
  336. package/src/vocabulary/text.test.tsx +0 -102
  337. package/src/vocabulary/text.tsx +0 -60
  338. package/src/vocabulary/toTextContent.ts +0 -2
@@ -1,1109 +0,0 @@
1
- import * as React from "react";
2
- import { z } from "zod";
3
- import {
4
- CHECKBOX_GROUP_ATTR,
5
- FIELD_NAME_ATTR,
6
- FIELD_VALUE_ATTR,
7
- GENERATED_NAME_ATTR,
8
- } from "../constants";
9
- import type { Action } from "../ir";
10
- import { BUTTON_STYLES } from "../ir";
11
- import type {
12
- GenerativeUIDispatch,
13
- GenerativeUILibrary,
14
- GenerativeUIStatus,
15
- } from "../types";
16
- import { A2uiBindingContext, useA2uiBinding } from "../a2ui/BindingContext";
17
- import { useAnsweredValue } from "../answeredValues";
18
- import { actionAttr, fire } from "./dispatch";
19
- import { toTextContent } from "./toTextContent";
20
- import { useRadioGroupName } from "../RadioGroupScope";
21
- import {
22
- classifyTemporal,
23
- fromLocalDateTime,
24
- getTemporalInputStep,
25
- normalizeTemporalInputValue,
26
- toLocalDateTime,
27
- toPickerLocalDateTime,
28
- } from "../temporal";
29
-
30
- const optionSchema = z.object({
31
- label: z.string(),
32
- value: z.string(),
33
- });
34
-
35
- const describedOptionSchema = optionSchema.extend({
36
- description: z.string().optional(),
37
- });
38
-
39
- type Option = { label: string; description?: string; value: string };
40
-
41
- const isOption = (option: unknown): option is Option =>
42
- option !== null &&
43
- typeof option === "object" &&
44
- "label" in option &&
45
- typeof option.label === "string" &&
46
- (!("description" in option) || typeof option.description === "string") &&
47
- "value" in option &&
48
- typeof option.value === "string";
49
-
50
- const mapOptions = (
51
- options: unknown,
52
- render: (option: Option, key: string) => React.ReactNode,
53
- ) => {
54
- const occurrences = new Map<string, number>();
55
- return (Array.isArray(options) ? options : []).map((option) => {
56
- if (!isOption(option)) return null;
57
- const occurrence = occurrences.get(option.value) ?? 0;
58
- occurrences.set(option.value, occurrence + 1);
59
- return render(option, JSON.stringify([option.value, occurrence]));
60
- });
61
- };
62
-
63
- type RadioGroupRenderProps = {
64
- value?: string;
65
- options: Option[];
66
- name?: string;
67
- label?: string;
68
- defaultValue?: string;
69
- children?: React.ReactNode;
70
- $status: GenerativeUIStatus;
71
- $action?: Action;
72
- $dispatch?: GenerativeUIDispatch;
73
- };
74
-
75
- function RadioGroupRender({
76
- value,
77
- options,
78
- name,
79
- label,
80
- defaultValue,
81
- children,
82
- $action,
83
- $dispatch,
84
- }: RadioGroupRenderProps) {
85
- const groupName = useRadioGroupName(name);
86
- const answeredValue = useAnsweredValue(name);
87
- const updateBinding = useA2uiBinding(name);
88
- const isBound = updateBinding !== undefined;
89
- const initialValue =
90
- typeof answeredValue === "string" ? answeredValue : defaultValue;
91
- return (
92
- <fieldset
93
- key={initialValue}
94
- data-aui="radiogroup"
95
- data-aui-action={actionAttr($action)}
96
- aria-label={label}
97
- >
98
- {mapOptions(options, (option, key) => (
99
- <label key={key} data-aui="radiogroup-option">
100
- <input
101
- type="radio"
102
- name={groupName}
103
- {...{ [FIELD_NAME_ATTR]: name }}
104
- {...(name == null ? { [GENERATED_NAME_ATTR]: "" } : {})}
105
- value={option.value}
106
- {...(isBound
107
- ? { checked: (answeredValue ?? value) === option.value }
108
- : { defaultChecked: initialValue === option.value })}
109
- onChange={(e) => {
110
- updateBinding?.(option.value);
111
- fire($action, $dispatch, option.value, e.currentTarget);
112
- }}
113
- />
114
- {option.description ? (
115
- <span data-aui="option-content">
116
- <span data-aui="option-label">{option.label}</span>
117
- <span data-aui="option-description">{option.description}</span>
118
- </span>
119
- ) : (
120
- option.label
121
- )}
122
- </label>
123
- ))}
124
- {children}
125
- </fieldset>
126
- );
127
- }
128
-
129
- type CheckboxGroupRenderProps = {
130
- value?: string[];
131
- options: Option[];
132
- name?: string;
133
- label?: string;
134
- defaultValue?: string[];
135
- children?: React.ReactNode;
136
- $status: GenerativeUIStatus;
137
- $action?: Action;
138
- $dispatch?: GenerativeUIDispatch;
139
- };
140
-
141
- const checkedGroupValues = (input: HTMLInputElement): string[] =>
142
- Array.from(
143
- input
144
- .closest("fieldset")
145
- ?.querySelectorAll<HTMLInputElement>(
146
- `:scope > label > input[${CHECKBOX_GROUP_ATTR}]:checked`,
147
- ) ?? [],
148
- (checkbox) => checkbox.value,
149
- );
150
-
151
- function CheckboxGroupRender({
152
- value,
153
- options,
154
- name,
155
- label,
156
- defaultValue,
157
- children,
158
- $action,
159
- $dispatch,
160
- }: CheckboxGroupRenderProps) {
161
- const generatedName = React.useId();
162
- const fieldName = name ?? generatedName;
163
- const answeredValue = useAnsweredValue(name);
164
- const updateBinding = useA2uiBinding(name);
165
- const isBound = updateBinding !== undefined;
166
- const checkedValues =
167
- Array.isArray(answeredValue) &&
168
- answeredValue.every((value): value is string => typeof value === "string")
169
- ? answeredValue
170
- : Array.isArray(defaultValue)
171
- ? defaultValue
172
- : [];
173
- return (
174
- <fieldset
175
- key={JSON.stringify(checkedValues)}
176
- data-aui="checkboxgroup"
177
- data-aui-action={actionAttr($action)}
178
- aria-label={label}
179
- >
180
- {mapOptions(options, (option, key) => (
181
- <label key={key} data-aui="checkboxgroup-option">
182
- <input
183
- type="checkbox"
184
- name={fieldName}
185
- {...{ [CHECKBOX_GROUP_ATTR]: "" }}
186
- {...(name == null ? { [GENERATED_NAME_ATTR]: "" } : {})}
187
- value={option.value}
188
- {...(isBound
189
- ? {
190
- checked: (Array.isArray(answeredValue)
191
- ? checkedValues
192
- : (value ?? [])
193
- ).includes(option.value),
194
- }
195
- : { defaultChecked: checkedValues.includes(option.value) })}
196
- onChange={(e) => {
197
- const selected = checkedGroupValues(e.currentTarget);
198
- updateBinding?.(selected);
199
- fire($action, $dispatch, selected, e.currentTarget);
200
- }}
201
- />
202
- {option.description ? (
203
- <span data-aui="option-content">
204
- <span data-aui="option-label">{option.label}</span>
205
- <span data-aui="option-description">{option.description}</span>
206
- </span>
207
- ) : (
208
- option.label
209
- )}
210
- </label>
211
- ))}
212
- {children}
213
- </fieldset>
214
- );
215
- }
216
-
217
- type SelectRenderProps = {
218
- value?: string;
219
- options: Option[];
220
- placeholder?: string;
221
- label?: string;
222
- name?: string;
223
- defaultValue?: string;
224
- children?: React.ReactNode;
225
- $status: GenerativeUIStatus;
226
- $action?: Action;
227
- $dispatch?: GenerativeUIDispatch;
228
- };
229
-
230
- function SelectRender({
231
- value,
232
- options,
233
- placeholder,
234
- label,
235
- name,
236
- defaultValue,
237
- $action,
238
- $dispatch,
239
- children,
240
- }: SelectRenderProps) {
241
- const answeredValue = useAnsweredValue(name);
242
- const updateBinding = useA2uiBinding(name);
243
- const isBound = updateBinding !== undefined;
244
- const placeholderText = toTextContent(placeholder);
245
- const initialValue =
246
- typeof answeredValue === "string"
247
- ? answeredValue
248
- : typeof defaultValue === "string"
249
- ? defaultValue
250
- : "";
251
- return (
252
- <select
253
- key={initialValue}
254
- data-aui="select"
255
- data-aui-action={actionAttr($action)}
256
- name={name}
257
- aria-label={label}
258
- {...(isBound
259
- ? {
260
- value:
261
- typeof answeredValue === "string" ? answeredValue : (value ?? ""),
262
- }
263
- : { defaultValue: initialValue })}
264
- onChange={(e) => {
265
- updateBinding?.(e.currentTarget.value);
266
- fire($action, $dispatch, e.currentTarget.value, e.currentTarget);
267
- }}
268
- >
269
- {placeholderText ? (
270
- <option value="" disabled>
271
- {placeholderText}
272
- </option>
273
- ) : null}
274
- {mapOptions(options, (option, key) => (
275
- <option key={key} value={option.value}>
276
- {option.label}
277
- </option>
278
- ))}
279
- {children}
280
- </select>
281
- );
282
- }
283
-
284
- type InputRenderProps = {
285
- value?: string;
286
- inputType?: "text" | "password" | "number";
287
- placeholder?: string;
288
- multiline?: boolean;
289
- label?: string;
290
- name?: string;
291
- defaultValue?: string;
292
- $status: GenerativeUIStatus;
293
- $action?: Action;
294
- $dispatch?: GenerativeUIDispatch;
295
- };
296
-
297
- function InputRender({
298
- value,
299
- inputType,
300
- placeholder,
301
- multiline,
302
- label,
303
- name,
304
- defaultValue,
305
- $action,
306
- $dispatch,
307
- }: InputRenderProps) {
308
- const answeredValue = useAnsweredValue(name);
309
- const updateBinding = useA2uiBinding(name);
310
- const isBound = updateBinding !== undefined;
311
- const initialValue =
312
- typeof answeredValue === "string" ? answeredValue : defaultValue;
313
- const submit = (control: HTMLInputElement | HTMLTextAreaElement) =>
314
- fire($action, $dispatch, control.value, control);
315
- return multiline && (inputType === undefined || inputType === "text") ? (
316
- <textarea
317
- key={initialValue}
318
- data-aui="input"
319
- data-aui-multiline
320
- data-aui-action={actionAttr($action)}
321
- name={name}
322
- aria-label={label}
323
- placeholder={placeholder}
324
- {...(isBound
325
- ? {
326
- value:
327
- typeof answeredValue === "string" ? answeredValue : (value ?? ""),
328
- }
329
- : { defaultValue: initialValue })}
330
- onChange={
331
- updateBinding ? (e) => updateBinding(e.currentTarget.value) : undefined
332
- }
333
- onKeyDown={(e) => {
334
- if (
335
- e.key !== "Enter" ||
336
- !(e.ctrlKey || e.metaKey) ||
337
- e.nativeEvent.isComposing
338
- )
339
- return;
340
- if (e.currentTarget.form) {
341
- e.preventDefault();
342
- HTMLFormElement.prototype.requestSubmit.call(e.currentTarget.form);
343
- } else {
344
- submit(e.currentTarget);
345
- }
346
- }}
347
- />
348
- ) : (
349
- <input
350
- key={initialValue}
351
- type={inputType}
352
- step={inputType === "number" ? "any" : undefined}
353
- data-aui="input"
354
- data-aui-action={actionAttr($action)}
355
- name={name}
356
- aria-label={label}
357
- placeholder={placeholder}
358
- {...(isBound
359
- ? {
360
- value:
361
- typeof answeredValue === "string" ? answeredValue : (value ?? ""),
362
- }
363
- : { defaultValue: initialValue })}
364
- onChange={
365
- updateBinding ? (e) => updateBinding(e.currentTarget.value) : undefined
366
- }
367
- onKeyDown={(e) => {
368
- if (
369
- e.key === "Enter" &&
370
- !e.nativeEvent.isComposing &&
371
- !e.currentTarget.form
372
- )
373
- submit(e.currentTarget);
374
- }}
375
- />
376
- );
377
- }
378
-
379
- type DatePickerRenderProps = {
380
- inputType?: "date" | "datetime" | "time";
381
- value?: string;
382
- min?: string;
383
- max?: string;
384
- label?: string;
385
- name?: string;
386
- $status: GenerativeUIStatus;
387
- $action?: Action;
388
- $dispatch?: GenerativeUIDispatch;
389
- };
390
-
391
- const subscribeToHydration = () => () => {};
392
- const clientHydrationSnapshot = () => true;
393
- const serverHydrationSnapshot = () => false;
394
-
395
- function DatePickerRender({
396
- inputType = "date",
397
- value,
398
- min,
399
- max,
400
- label,
401
- name,
402
- $action,
403
- $dispatch,
404
- }: DatePickerRenderProps) {
405
- const answeredValue = useAnsweredValue(name);
406
- const updateBinding = useA2uiBinding(name);
407
- const isBound = updateBinding !== undefined;
408
- const initialValue =
409
- typeof answeredValue === "string" ? answeredValue : value;
410
- const [selection, setSelection] = React.useState({
411
- initialValue,
412
- value: initialValue,
413
- anchor: initialValue,
414
- committedValue: initialValue,
415
- edited: false,
416
- });
417
- if (selection.initialValue !== initialValue) {
418
- const isEcho = isBound && initialValue === selection.value;
419
- setSelection({
420
- initialValue,
421
- value: initialValue,
422
- anchor: isEcho ? selection.anchor : initialValue,
423
- committedValue: isEcho ? selection.committedValue : initialValue,
424
- edited: isEcho && selection.edited,
425
- });
426
- }
427
- const currentValue = isBound ? initialValue : selection.value;
428
- const temporal = classifyTemporal(currentValue ?? "");
429
- const anchor = classifyTemporal(selection.anchor ?? "");
430
- const hasInstantAnchor =
431
- inputType === "datetime" && anchor.kind === "instant";
432
- const canonicalFieldValue = (value: string | undefined) => {
433
- const current = value ?? "";
434
- const kind = classifyTemporal(current).kind;
435
- if (inputType === "time")
436
- return kind === "time" && normalizeTemporalInputValue(current) !== current
437
- ? value
438
- : undefined;
439
- if (inputType !== "datetime") return undefined;
440
- if (kind === "instant") return value;
441
- if (hasInstantAnchor) return selection.anchor;
442
- return kind === "floating" &&
443
- normalizeTemporalInputValue(current) !== current
444
- ? value
445
- : undefined;
446
- };
447
- const minimum = classifyTemporal(min ?? "");
448
- const maximum = classifyTemporal(max ?? "");
449
- const hydrated = React.useSyncExternalStore(
450
- subscribeToHydration,
451
- clientHydrationSnapshot,
452
- serverHydrationSnapshot,
453
- );
454
- const showRawValue =
455
- inputType === "datetime" && temporal.kind === "instant" && !hydrated;
456
- const displayValue =
457
- currentValue === undefined
458
- ? undefined
459
- : inputType === "datetime" && hydrated
460
- ? toPickerLocalDateTime(currentValue)
461
- : normalizeTemporalInputValue(currentValue);
462
- const keyboardActive = React.useRef(false);
463
- const valueFromInput = (input: HTMLInputElement) => {
464
- const inputValue = normalizeTemporalInputValue(input.value);
465
- return inputType === "datetime"
466
- ? fromLocalDateTime(inputValue, selection.anchor)
467
- : inputValue;
468
- };
469
- const commit = (input: HTMLInputElement) => {
470
- if (!selection.edited) return;
471
- const nextValue = valueFromInput(input);
472
- setSelection({ ...selection, committedValue: nextValue, edited: false });
473
- if (nextValue !== selection.committedValue)
474
- fire($action, $dispatch, nextValue, input);
475
- };
476
- return (
477
- <input
478
- key={isBound ? undefined : initialValue}
479
- type={
480
- showRawValue
481
- ? "text"
482
- : inputType === "datetime"
483
- ? "datetime-local"
484
- : inputType
485
- }
486
- readOnly={showRawValue || undefined}
487
- data-aui="datepicker"
488
- data-aui-action={actionAttr($action)}
489
- {...{
490
- [FIELD_VALUE_ATTR]: canonicalFieldValue(currentValue),
491
- }}
492
- name={name}
493
- aria-label={label}
494
- {...(isBound || inputType === "datetime"
495
- ? { value: displayValue ?? "" }
496
- : { defaultValue: displayValue })}
497
- min={
498
- inputType === "datetime" && hydrated && min !== undefined
499
- ? normalizeTemporalInputValue(
500
- toLocalDateTime(
501
- min,
502
- minimum.kind === "instant" ? minimum.precision : undefined,
503
- ),
504
- )
505
- : min === undefined
506
- ? undefined
507
- : normalizeTemporalInputValue(min)
508
- }
509
- max={
510
- inputType === "datetime" && hydrated && max !== undefined
511
- ? normalizeTemporalInputValue(
512
- toLocalDateTime(
513
- max,
514
- maximum.kind === "instant" ? maximum.precision : undefined,
515
- ),
516
- )
517
- : max === undefined
518
- ? undefined
519
- : normalizeTemporalInputValue(max)
520
- }
521
- step={
522
- inputType !== "date"
523
- ? getTemporalInputStep(
524
- selection.anchor,
525
- hasInstantAnchor ? displayValue : currentValue,
526
- min,
527
- max,
528
- )
529
- : undefined
530
- }
531
- onChange={(e) => {
532
- const nextValue = valueFromInput(e.currentTarget);
533
- const canonical = canonicalFieldValue(nextValue);
534
- if (canonical !== undefined) {
535
- e.currentTarget.setAttribute(FIELD_VALUE_ATTR, canonical);
536
- } else {
537
- e.currentTarget.removeAttribute(FIELD_VALUE_ATTR);
538
- }
539
- // A change without an editing key down is a pick from the native picker, which commits at once; a typed value waits for blur or Enter.
540
- const typed = keyboardActive.current;
541
- keyboardActive.current = false;
542
- setSelection({
543
- initialValue,
544
- value: nextValue,
545
- anchor: selection.anchor,
546
- committedValue: typed ? selection.committedValue : nextValue,
547
- edited: typed,
548
- });
549
- updateBinding?.(nextValue);
550
- if (!typed && nextValue !== selection.committedValue)
551
- fire($action, $dispatch, nextValue, e.currentTarget);
552
- }}
553
- onBlur={(e) => {
554
- keyboardActive.current = false;
555
- commit(e.currentTarget);
556
- }}
557
- onKeyDown={(e) => {
558
- if (
559
- e.key === "Enter" &&
560
- !e.nativeEvent.isComposing &&
561
- !e.currentTarget.form
562
- ) {
563
- commit(e.currentTarget);
564
- return;
565
- }
566
- keyboardActive.current =
567
- !e.altKey &&
568
- !e.ctrlKey &&
569
- !e.metaKey &&
570
- (e.key.length === 1 ||
571
- [
572
- "ArrowDown",
573
- "ArrowUp",
574
- "Backspace",
575
- "Delete",
576
- "End",
577
- "Home",
578
- "PageDown",
579
- "PageUp",
580
- ].includes(e.key));
581
- }}
582
- onKeyUp={() => {
583
- keyboardActive.current = false;
584
- }}
585
- onPaste={() => {
586
- keyboardActive.current = true;
587
- }}
588
- onCut={() => {
589
- keyboardActive.current = true;
590
- }}
591
- />
592
- );
593
- }
594
-
595
- type CheckboxRenderProps = {
596
- checked?: boolean;
597
- label: string;
598
- name?: string;
599
- defaultChecked?: boolean;
600
- variant?: "checkbox" | "switch";
601
- $status: GenerativeUIStatus;
602
- $action?: Action;
603
- $dispatch?: GenerativeUIDispatch;
604
- };
605
-
606
- function CheckboxRender({
607
- checked,
608
- label,
609
- name,
610
- defaultChecked,
611
- variant = "checkbox",
612
- $action,
613
- $dispatch,
614
- }: CheckboxRenderProps) {
615
- const answeredValue = useAnsweredValue(name);
616
- const updateBinding = useA2uiBinding(name);
617
- const isBound = updateBinding !== undefined;
618
- const initialChecked =
619
- typeof answeredValue === "boolean" ? answeredValue : defaultChecked;
620
- return (
621
- <label
622
- data-aui="checkbox"
623
- data-aui-variant={variant === "switch" ? variant : undefined}
624
- >
625
- <input
626
- key={String(initialChecked)}
627
- type="checkbox"
628
- role={variant === "switch" ? "switch" : undefined}
629
- data-aui-action={actionAttr($action)}
630
- name={name}
631
- {...(isBound
632
- ? {
633
- checked:
634
- typeof answeredValue === "boolean"
635
- ? answeredValue
636
- : checked === true,
637
- }
638
- : { defaultChecked: initialChecked })}
639
- onChange={(e) => {
640
- updateBinding?.(e.currentTarget.checked);
641
- fire($action, $dispatch, e.currentTarget.checked, e.currentTarget);
642
- }}
643
- />
644
- <span data-aui="checkbox-label">{toTextContent(label)}</span>
645
- </label>
646
- );
647
- }
648
-
649
- const finiteNumber = (value: unknown): value is number =>
650
- typeof value === "number" && Number.isFinite(value);
651
-
652
- const clamp = (value: number, min: number, max: number): number =>
653
- Math.min(Math.max(value, min), max);
654
-
655
- type SliderRenderProps = {
656
- value?: number;
657
- name?: string;
658
- label?: string;
659
- min: number;
660
- max: number;
661
- step?: number;
662
- defaultValue?: number;
663
- unit?: string;
664
- $status: GenerativeUIStatus;
665
- $action?: Action;
666
- $dispatch?: GenerativeUIDispatch;
667
- };
668
-
669
- type SliderControlProps = {
670
- controlledValue?: number;
671
- name?: string;
672
- label?: string;
673
- min: number;
674
- max: number;
675
- step: number;
676
- initialValue: number;
677
- unit?: string;
678
- $action?: Action;
679
- $dispatch?: GenerativeUIDispatch;
680
- };
681
-
682
- function SliderControl({
683
- controlledValue,
684
- name,
685
- label,
686
- min,
687
- max,
688
- step,
689
- initialValue,
690
- unit,
691
- $action,
692
- $dispatch,
693
- }: SliderControlProps) {
694
- const updateBinding = useA2uiBinding(name);
695
- const [localValue, setValue] = React.useState(initialValue);
696
- const value = controlledValue ?? localValue;
697
- const pointerActive = React.useRef(false);
698
- const keyboardActive = React.useRef(false);
699
- const lastCommitted = React.useRef(initialValue);
700
- const currentValue = (input: HTMLInputElement) =>
701
- clamp(Number(input.value), min, max);
702
- const commit = (input: HTMLInputElement) => {
703
- if (controlledValue !== undefined) return;
704
- const nextValue = currentValue(input);
705
- setValue(nextValue);
706
- if (lastCommitted.current === nextValue) return;
707
- lastCommitted.current = nextValue;
708
- fire($action, $dispatch, nextValue, input);
709
- };
710
-
711
- return (
712
- <label data-aui="slider-field">
713
- {label ? <span data-aui="slider-label">{label}</span> : null}
714
- <input
715
- type="range"
716
- data-aui="slider"
717
- data-aui-action={actionAttr($action)}
718
- name={name}
719
- aria-label={label}
720
- aria-valuetext={unit ? `${value} ${unit}` : String(value)}
721
- min={min}
722
- max={max}
723
- step={step}
724
- {...(controlledValue !== undefined
725
- ? { value }
726
- : { defaultValue: initialValue })}
727
- onInput={(e) => {
728
- const nextValue = currentValue(e.currentTarget);
729
- if (controlledValue !== undefined) updateBinding?.(nextValue);
730
- else setValue(nextValue);
731
- }}
732
- onPointerDown={() => {
733
- pointerActive.current = true;
734
- }}
735
- onPointerUp={(e) => {
736
- pointerActive.current = false;
737
- commit(e.currentTarget);
738
- }}
739
- onKeyDown={(e) => {
740
- keyboardActive.current = [
741
- "ArrowDown",
742
- "ArrowLeft",
743
- "ArrowRight",
744
- "ArrowUp",
745
- "End",
746
- "Home",
747
- "PageDown",
748
- "PageUp",
749
- ].includes(e.key);
750
- }}
751
- onKeyUp={(e) => {
752
- if (!keyboardActive.current) return;
753
- keyboardActive.current = false;
754
- commit(e.currentTarget);
755
- }}
756
- onChange={(e) => {
757
- if (!pointerActive.current && !keyboardActive.current)
758
- commit(e.currentTarget);
759
- }}
760
- />
761
- <output data-aui="slider-value">
762
- {value}
763
- {unit ? ` ${unit}` : ""}
764
- </output>
765
- </label>
766
- );
767
- }
768
-
769
- function SliderRender({
770
- value,
771
- name,
772
- label,
773
- min,
774
- max,
775
- step,
776
- defaultValue,
777
- unit,
778
- $action,
779
- $dispatch,
780
- }: SliderRenderProps) {
781
- const answeredValue = useAnsweredValue(name);
782
- const updateBinding = useA2uiBinding(name);
783
- const isBound = updateBinding !== undefined;
784
- const safeMin = finiteNumber(min) ? min : 0;
785
- const safeMax = finiteNumber(max) ? Math.max(max, safeMin) : safeMin + 100;
786
- const safeStep = finiteNumber(step) && step > 0 ? step : 1;
787
- const defaultNumber = finiteNumber(isBound ? value : defaultValue)
788
- ? isBound
789
- ? value!
790
- : defaultValue!
791
- : safeMin;
792
- const initialValue = clamp(
793
- finiteNumber(answeredValue) ? answeredValue : defaultNumber,
794
- safeMin,
795
- safeMax,
796
- );
797
- return (
798
- <SliderControl
799
- key={`${safeMin}:${safeMax}:${safeStep}:${isBound ? "bound" : initialValue}`}
800
- {...(isBound ? { controlledValue: initialValue } : {})}
801
- min={safeMin}
802
- max={safeMax}
803
- step={safeStep}
804
- initialValue={initialValue}
805
- {...(name !== undefined ? { name } : {})}
806
- {...(label !== undefined ? { label } : {})}
807
- {...(unit !== undefined ? { unit } : {})}
808
- {...($action !== undefined ? { $action } : {})}
809
- {...($dispatch !== undefined ? { $dispatch } : {})}
810
- />
811
- );
812
- }
813
-
814
- const UNDO_WINDOW_SECONDS = 5;
815
-
816
- type ButtonRenderProps = {
817
- label: string;
818
- buttonStyle?: (typeof BUTTON_STYLES)[number];
819
- block?: boolean;
820
- submit?: boolean;
821
- undoable?: boolean;
822
- children?: React.ReactNode;
823
- $status: GenerativeUIStatus;
824
- $action?: Action;
825
- $dispatch?: GenerativeUIDispatch;
826
- };
827
-
828
- function ButtonRender({
829
- label,
830
- buttonStyle,
831
- block,
832
- submit,
833
- undoable,
834
- children,
835
- $action,
836
- $dispatch,
837
- }: ButtonRenderProps) {
838
- const bindings = React.useContext(A2uiBindingContext!)?.fields;
839
- const bindingsRef = React.useRef(bindings);
840
- bindingsRef.current = bindings;
841
- const [remaining, setRemaining] = React.useState<number | undefined>(
842
- undefined,
843
- );
844
- const labelText = String(toTextContent(label) ?? "");
845
- const undoLabel = labelText.trim();
846
- const pendingAction = React.useRef<
847
- | {
848
- $action: Action;
849
- $dispatch: GenerativeUIDispatch;
850
- source: HTMLButtonElement;
851
- }
852
- | undefined
853
- >(undefined);
854
- const canUndo =
855
- undoable && !submit && $action !== undefined && $dispatch !== undefined;
856
-
857
- React.useEffect(() => {
858
- if (remaining === undefined) return;
859
- const timer = setTimeout(() => {
860
- if (remaining > 1) {
861
- setRemaining(remaining - 1);
862
- return;
863
- }
864
- const action = pendingAction.current;
865
- pendingAction.current = undefined;
866
- setRemaining(undefined);
867
- if (action)
868
- fire(
869
- action.$action,
870
- action.$dispatch,
871
- undefined,
872
- action.source,
873
- bindingsRef.current,
874
- );
875
- }, 1_000);
876
- return () => clearTimeout(timer);
877
- }, [remaining]);
878
-
879
- const cancelUndo = () => {
880
- pendingAction.current = undefined;
881
- setRemaining(undefined);
882
- };
883
-
884
- return (
885
- <button
886
- type={submit ? "submit" : "button"}
887
- data-aui="button"
888
- data-aui-style={buttonStyle}
889
- data-aui-block={block || undefined}
890
- data-aui-submit={submit || undefined}
891
- data-aui-state={remaining === undefined ? undefined : "pending"}
892
- data-aui-action={actionAttr($action)}
893
- aria-label={
894
- remaining === undefined
895
- ? undefined
896
- : undoLabel
897
- ? `Undo ${undoLabel}`
898
- : "Undo"
899
- }
900
- onClick={
901
- submit
902
- ? undefined
903
- : canUndo
904
- ? (e) => {
905
- if (remaining !== undefined) {
906
- cancelUndo();
907
- return;
908
- }
909
- pendingAction.current = {
910
- $action,
911
- $dispatch,
912
- source: e.currentTarget,
913
- };
914
- setRemaining(UNDO_WINDOW_SECONDS);
915
- }
916
- : (e) =>
917
- fire($action, $dispatch, undefined, e.currentTarget, bindings)
918
- }
919
- onKeyDown={
920
- canUndo
921
- ? (e) => {
922
- if (e.key !== "Escape" || remaining === undefined) return;
923
- e.preventDefault();
924
- cancelUndo();
925
- }
926
- : undefined
927
- }
928
- >
929
- {remaining === undefined ? (
930
- <>
931
- {labelText}
932
- {children}
933
- </>
934
- ) : (
935
- <>
936
- Undo <span data-aui="button-countdown">{remaining}</span>
937
- <span data-aui="button-undo-status" role="status">
938
- {labelText} in {UNDO_WINDOW_SECONDS} seconds
939
- </span>
940
- </>
941
- )}
942
- </button>
943
- );
944
- }
945
-
946
- export const interactiveVocabulary = {
947
- Button: {
948
- description:
949
- "A clickable button. Carries `$action` describing the side effect or resume value. Set `submit` to submit an ancestor Form/Card instead of firing `$action` on click.",
950
- properties: z.object({
951
- label: z.string().describe("Button label."),
952
- buttonStyle: z.enum(BUTTON_STYLES).optional().describe("Visual style."),
953
- block: z
954
- .boolean()
955
- .optional()
956
- .describe("Whether the button spans the full width."),
957
- submit: z
958
- .boolean()
959
- .optional()
960
- .describe(
961
- "Render as a submit button for an ancestor Form/Card instead of a click button.",
962
- ),
963
- undoable: z
964
- .boolean()
965
- .optional()
966
- .describe(
967
- "Delay the action with an undo control, for sends and other hard-to-reverse actions.",
968
- ),
969
- }),
970
- render: ButtonRender,
971
- },
972
- Select: {
973
- description:
974
- "A dropdown selector. Carries `$action` describing the on-select behavior.",
975
- properties: z.object({
976
- options: z.array(optionSchema).describe("Selectable options."),
977
- placeholder: z
978
- .string()
979
- .optional()
980
- .describe("Placeholder shown when nothing is selected."),
981
- label: z
982
- .string()
983
- .optional()
984
- .describe("Accessible label for the control."),
985
- name: z.string().optional().describe("Field name used inside a Form."),
986
- defaultValue: z.string().optional().describe("Initially selected value."),
987
- }),
988
- render: SelectRender,
989
- },
990
- Input: {
991
- description:
992
- "A text input. Carries `$action` describing the on-submit behavior.",
993
- properties: z.object({
994
- inputType: z
995
- .enum(["text", "password", "number"])
996
- .optional()
997
- .describe("Single-line input type. Values remain strings."),
998
- placeholder: z.string().optional().describe("Placeholder text."),
999
- multiline: z
1000
- .boolean()
1001
- .optional()
1002
- .describe(
1003
- 'Render a textarea instead of a single-line input; ignored when `inputType` is `"password"` or `"number"`.',
1004
- ),
1005
- label: z
1006
- .string()
1007
- .optional()
1008
- .describe("Accessible label for the control."),
1009
- name: z.string().optional().describe("Field name used inside a Form."),
1010
- defaultValue: z.string().optional().describe("Initial text."),
1011
- }),
1012
- render: InputRender,
1013
- },
1014
- DatePicker: {
1015
- description:
1016
- "A date, datetime, or time input. Carries `$action`, which runs once per committed value: a pick from the picker, or a typed value on blur or Enter.",
1017
- properties: z.object({
1018
- inputType: z
1019
- .enum(["date", "datetime", "time"])
1020
- .optional()
1021
- .describe(
1022
- 'Temporal input type. Defaults to "date". Values remain strings.',
1023
- ),
1024
- value: z
1025
- .string()
1026
- .optional()
1027
- .describe(
1028
- "Initial value: YYYY-MM-DD for date, HH:mm or HH:mm:ss with optional fraction of at most 9 digits for time, YYYY-MM-DDTHH:mm with optional :ss and fraction of at most 9 digits for datetime. A datetime with Z or ±HH:mm is an instant, displayed in the viewer's time zone and submitted with the same offset and precision. A datetime without an offset is local and submitted unchanged. An empty datetime submits with the viewer's offset and seconds.",
1029
- ),
1030
- min: z
1031
- .string()
1032
- .optional()
1033
- .describe(
1034
- "Minimum value: YYYY-MM-DD for date, HH:mm or HH:mm:ss with optional fraction of at most 9 digits for time, YYYY-MM-DDTHH:mm with optional :ss and fraction of at most 9 digits for datetime. Datetimes with Z or ±HH:mm are converted to the viewer's time zone.",
1035
- ),
1036
- max: z
1037
- .string()
1038
- .optional()
1039
- .describe(
1040
- "Maximum value: YYYY-MM-DD for date, HH:mm or HH:mm:ss with optional fraction of at most 9 digits for time, YYYY-MM-DDTHH:mm with optional :ss and fraction of at most 9 digits for datetime. Datetimes with Z or ±HH:mm are converted to the viewer's time zone.",
1041
- ),
1042
- label: z
1043
- .string()
1044
- .optional()
1045
- .describe("Accessible label for the control."),
1046
- name: z.string().optional().describe("Field name used inside a Form."),
1047
- }),
1048
- render: DatePickerRender,
1049
- },
1050
- Checkbox: {
1051
- description:
1052
- "A checkbox with a label. Carries `$action` describing the on-change behavior.",
1053
- properties: z.object({
1054
- label: z.string().describe("Label text next to the checkbox."),
1055
- name: z.string().optional().describe("Field name used inside a Form."),
1056
- defaultChecked: z
1057
- .boolean()
1058
- .optional()
1059
- .describe("Whether the checkbox starts checked."),
1060
- variant: z
1061
- .enum(["checkbox", "switch"])
1062
- .optional()
1063
- .describe("Whether to render a checkbox or switch."),
1064
- }),
1065
- render: CheckboxRender,
1066
- },
1067
- Slider: {
1068
- description:
1069
- "A numeric range control. Carries `$action` describing the committed value.",
1070
- properties: z.object({
1071
- name: z.string().optional().describe("Field name used inside a Form."),
1072
- label: z
1073
- .string()
1074
- .optional()
1075
- .describe("Accessible label for the control."),
1076
- min: z.number().describe("Minimum value."),
1077
- max: z.number().describe("Maximum value."),
1078
- step: z.number().optional().describe("Value increment. Defaults to 1."),
1079
- defaultValue: z.number().optional().describe("Initial value."),
1080
- unit: z.string().optional().describe("Unit shown with the value."),
1081
- }),
1082
- render: SliderRender,
1083
- },
1084
- RadioGroup: {
1085
- description:
1086
- "A group of mutually exclusive radio options. Carries `$action` describing the on-change behavior.",
1087
- properties: z.object({
1088
- options: z.array(describedOptionSchema).describe("Selectable options."),
1089
- name: z.string().optional().describe("Field name used inside a Form."),
1090
- label: z.string().optional().describe("Accessible name for the group."),
1091
- defaultValue: z.string().optional().describe("Initially selected value."),
1092
- }),
1093
- render: RadioGroupRender,
1094
- },
1095
- CheckboxGroup: {
1096
- description:
1097
- "A group of checkbox options where any number can be checked. Carries `$action` describing the on-change behavior.",
1098
- properties: z.object({
1099
- options: z.array(describedOptionSchema).describe("Selectable options."),
1100
- name: z.string().optional().describe("Field name used inside a Form."),
1101
- label: z.string().optional().describe("Accessible name for the group."),
1102
- defaultValue: z
1103
- .array(z.string())
1104
- .optional()
1105
- .describe("Initially checked values."),
1106
- }),
1107
- render: CheckboxGroupRender,
1108
- },
1109
- } satisfies GenerativeUILibrary;