@terreno/ui 57.9.0 → 57.11.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.
- package/LICENSE +1 -0
- package/README.md +2 -0
- package/dist/Button.d.ts.map +1 -1
- package/dist/Button.js +20 -5
- package/dist/Button.js.map +1 -1
- package/dist/Common.d.ts +74 -1
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/DataTable.js +1 -1
- package/dist/DataTable.js.map +1 -1
- package/dist/DateTimeActionSheet.js +1 -1
- package/dist/DateTimeActionSheet.js.map +1 -1
- package/dist/DateTimeField.d.ts.map +1 -1
- package/dist/DateTimeField.js +91 -18
- package/dist/DateTimeField.js.map +1 -1
- package/dist/FilePickerButton.d.ts +15 -1
- package/dist/FilePickerButton.d.ts.map +1 -1
- package/dist/FilePickerButton.js +26 -9
- package/dist/FilePickerButton.js.map +1 -1
- package/dist/GPTChat.d.ts +57 -1
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js +199 -7
- package/dist/GPTChat.js.map +1 -1
- package/dist/HtmlFrame.d.ts +11 -0
- package/dist/HtmlFrame.d.ts.map +1 -0
- package/dist/HtmlFrame.js +29 -0
- package/dist/HtmlFrame.js.map +1 -0
- package/dist/SplitPage.d.ts +2 -2
- package/dist/SplitPage.d.ts.map +1 -1
- package/dist/SplitPage.js +17 -5
- package/dist/SplitPage.js.map +1 -1
- package/dist/Text.d.ts +1 -1
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +2 -2
- package/dist/Text.js.map +1 -1
- package/dist/asks/AskCard.d.ts +34 -0
- package/dist/asks/AskCard.d.ts.map +1 -0
- package/dist/asks/AskCard.js +91 -0
- package/dist/asks/AskCard.js.map +1 -0
- package/dist/asks/AskChoice.d.ts +11 -0
- package/dist/asks/AskChoice.d.ts.map +1 -0
- package/dist/asks/AskChoice.js +146 -0
- package/dist/asks/AskChoice.js.map +1 -0
- package/dist/asks/AskConfirm.d.ts +15 -0
- package/dist/asks/AskConfirm.d.ts.map +1 -0
- package/dist/asks/AskConfirm.js +17 -0
- package/dist/asks/AskConfirm.js.map +1 -0
- package/dist/asks/AskFiles.d.ts +18 -0
- package/dist/asks/AskFiles.d.ts.map +1 -0
- package/dist/asks/AskFiles.js +149 -0
- package/dist/asks/AskFiles.js.map +1 -0
- package/dist/asks/AskForm.d.ts +27 -0
- package/dist/asks/AskForm.d.ts.map +1 -0
- package/dist/asks/AskForm.js +146 -0
- package/dist/asks/AskForm.js.map +1 -0
- package/dist/asks/AskMarkdown.d.ts +25 -0
- package/dist/asks/AskMarkdown.d.ts.map +1 -0
- package/dist/asks/AskMarkdown.js +73 -0
- package/dist/asks/AskMarkdown.js.map +1 -0
- package/dist/asks/SimpleAskCard.d.ts +23 -0
- package/dist/asks/SimpleAskCard.d.ts.map +1 -0
- package/dist/asks/SimpleAskCard.js +32 -0
- package/dist/asks/SimpleAskCard.js.map +1 -0
- package/dist/asks/askControls.d.ts +33 -0
- package/dist/asks/askControls.d.ts.map +1 -0
- package/dist/asks/askControls.js +30 -0
- package/dist/asks/askControls.js.map +1 -0
- package/dist/asks/askFileRefs.d.ts +26 -0
- package/dist/asks/askFileRefs.d.ts.map +1 -0
- package/dist/asks/askFileRefs.js +75 -0
- package/dist/asks/askFileRefs.js.map +1 -0
- package/dist/asks/askFormDraft.d.ts +60 -0
- package/dist/asks/askFormDraft.d.ts.map +1 -0
- package/dist/asks/askFormDraft.js +224 -0
- package/dist/asks/askFormDraft.js.map +1 -0
- package/dist/asks/askSummary.d.ts +10 -0
- package/dist/asks/askSummary.d.ts.map +1 -0
- package/dist/asks/askSummary.js +138 -0
- package/dist/asks/askSummary.js.map +1 -0
- package/dist/asks/askTypes.d.ts +21 -0
- package/dist/asks/askTypes.d.ts.map +1 -0
- package/dist/asks/askTypes.js +2 -0
- package/dist/asks/askTypes.js.map +1 -0
- package/dist/asks/simpleCardButtonVariants.d.ts +5 -0
- package/dist/asks/simpleCardButtonVariants.d.ts.map +1 -0
- package/dist/asks/simpleCardButtonVariants.js +8 -0
- package/dist/asks/simpleCardButtonVariants.js.map +1 -0
- package/dist/blocks/BlocksError.d.ts +8 -0
- package/dist/blocks/BlocksError.d.ts.map +1 -0
- package/dist/blocks/BlocksError.js +14 -0
- package/dist/blocks/BlocksError.js.map +1 -0
- package/dist/blocks/BlocksView.d.ts +10 -0
- package/dist/blocks/BlocksView.d.ts.map +1 -0
- package/dist/blocks/BlocksView.js +90 -0
- package/dist/blocks/BlocksView.js.map +1 -0
- package/dist/blocks/blockRenderers.d.ts +25 -0
- package/dist/blocks/blockRenderers.d.ts.map +1 -0
- package/dist/blocks/blockRenderers.js +136 -0
- package/dist/blocks/blockRenderers.js.map +1 -0
- package/dist/blocks/datasetToPoints.d.ts +22 -0
- package/dist/blocks/datasetToPoints.d.ts.map +1 -0
- package/dist/blocks/datasetToPoints.js +28 -0
- package/dist/blocks/datasetToPoints.js.map +1 -0
- package/dist/blocks/useBlockSelections.d.ts +6 -0
- package/dist/blocks/useBlockSelections.d.ts.map +1 -0
- package/dist/blocks/useBlockSelections.js +10 -0
- package/dist/blocks/useBlockSelections.js.map +1 -0
- package/dist/blocks/useResolvedDatasets.d.ts +18 -0
- package/dist/blocks/useResolvedDatasets.d.ts.map +1 -0
- package/dist/blocks/useResolvedDatasets.js +59 -0
- package/dist/blocks/useResolvedDatasets.js.map +1 -0
- package/dist/blocks/useResolvedImages.d.ts +15 -0
- package/dist/blocks/useResolvedImages.d.ts.map +1 -0
- package/dist/blocks/useResolvedImages.js +66 -0
- package/dist/blocks/useResolvedImages.js.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts +6 -0
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.js +4 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
- package/dist/table/TableHeaderCell.js +1 -1
- package/dist/table/TableHeaderCell.js.map +1 -1
- package/package.json +2 -1
- package/src/Button.test.tsx +101 -0
- package/src/Button.tsx +35 -5
- package/src/Common.ts +73 -0
- package/src/DataTable.tsx +1 -1
- package/src/DateTimeActionSheet.tsx +1 -1
- package/src/DateTimeField.test.tsx +185 -2
- package/src/DateTimeField.tsx +107 -17
- package/src/DonutChart.test.tsx +5 -10
- package/src/FilePickerButton.test.tsx +56 -3
- package/src/FilePickerButton.tsx +83 -17
- package/src/GPTChat.test.tsx +758 -2
- package/src/GPTChat.tsx +517 -7
- package/src/HtmlFrame.tsx +60 -0
- package/src/LineChart.test.tsx +5 -10
- package/src/SplitPage.test.tsx +55 -0
- package/src/SplitPage.tsx +13 -7
- package/src/Text.test.tsx +22 -0
- package/src/Text.tsx +10 -1
- package/src/__snapshots__/Button.test.tsx.snap +1 -1
- package/src/__snapshots__/SplitPage.test.tsx.snap +14 -0
- package/src/asks/AskCard.test.tsx +1428 -0
- package/src/asks/AskCard.tsx +240 -0
- package/src/asks/AskChoice.tsx +368 -0
- package/src/asks/AskConfirm.tsx +42 -0
- package/src/asks/AskFiles.test.tsx +347 -0
- package/src/asks/AskFiles.tsx +246 -0
- package/src/asks/AskForm.tsx +334 -0
- package/src/asks/AskMarkdown.tsx +176 -0
- package/src/asks/SimpleAskCard.test.tsx +253 -0
- package/src/asks/SimpleAskCard.tsx +82 -0
- package/src/asks/askControls.tsx +98 -0
- package/src/asks/askFileRefs.test.ts +155 -0
- package/src/asks/askFileRefs.ts +98 -0
- package/src/asks/askFormDraft.test.ts +347 -0
- package/src/asks/askFormDraft.ts +309 -0
- package/src/asks/askSummary.test.ts +255 -0
- package/src/asks/askSummary.ts +160 -0
- package/src/asks/askTypes.ts +25 -0
- package/src/asks/asksUseTerrenoComponents.test.ts +55 -0
- package/src/asks/simpleCardButtonVariants.ts +14 -0
- package/src/blocks/BlocksError.tsx +28 -0
- package/src/blocks/BlocksView.test.tsx +607 -0
- package/src/blocks/BlocksView.tsx +125 -0
- package/src/blocks/blockRenderers.tsx +288 -0
- package/src/blocks/datasetToPoints.test.ts +31 -0
- package/src/blocks/datasetToPoints.ts +48 -0
- package/src/blocks/useBlockSelections.ts +13 -0
- package/src/blocks/useResolvedDatasets.ts +78 -0
- package/src/blocks/useResolvedImages.ts +81 -0
- package/src/bunSetup.ts +0 -6
- package/src/index.tsx +7 -0
- package/src/isolated/HtmlFrame.isolated.tsx +54 -0
- package/src/lazyBoundaries/heavyOptionalExports.test.tsx +2 -0
- package/src/lazyBoundaries/heavyOptionalExports.tsx +14 -0
- package/src/table/TableHeaderCell.tsx +1 -1
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import type {AskValidationError, FormAskInput, FormField} from "@terreno/blocks";
|
|
3
|
+
import {assert} from "chai";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
formatFormValue,
|
|
7
|
+
formDraftValues,
|
|
8
|
+
formFieldErrorText,
|
|
9
|
+
fromPickerValue,
|
|
10
|
+
initialFormDraft,
|
|
11
|
+
toPickerValue,
|
|
12
|
+
} from "./askFormDraft";
|
|
13
|
+
|
|
14
|
+
const EVERY_TYPE_INPUT: FormAskInput = {
|
|
15
|
+
fields: [
|
|
16
|
+
{default: "Acme", id: "company", label: "Company", type: "text"},
|
|
17
|
+
{id: "notes", label: "Notes", type: "textarea"},
|
|
18
|
+
{id: "email", label: "Email", type: "email"},
|
|
19
|
+
{id: "site", label: "Website", type: "url"},
|
|
20
|
+
{id: "phone", label: "Phone", type: "phone"},
|
|
21
|
+
{default: 12, id: "seats", label: "Seats", type: "number"},
|
|
22
|
+
{default: "2026-10-01", id: "start", label: "Start", type: "date"},
|
|
23
|
+
{id: "remind", label: "Remind at", type: "time"},
|
|
24
|
+
],
|
|
25
|
+
prompt: "Every type.",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const CHOICES_INPUT: FormAskInput = {
|
|
29
|
+
fields: [
|
|
30
|
+
{id: "notify", label: "Notify me", type: "boolean"},
|
|
31
|
+
{default: true, id: "terms", label: "Accept terms", type: "boolean"},
|
|
32
|
+
{
|
|
33
|
+
id: "region",
|
|
34
|
+
label: "Region",
|
|
35
|
+
options: [
|
|
36
|
+
{id: "us", label: "US"},
|
|
37
|
+
{id: "eu", label: "EU"},
|
|
38
|
+
],
|
|
39
|
+
type: "select",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
default: ["email"],
|
|
43
|
+
id: "channels",
|
|
44
|
+
label: "Channels",
|
|
45
|
+
options: [
|
|
46
|
+
{id: "email", label: "Email"},
|
|
47
|
+
{id: "sms", label: "Text message"},
|
|
48
|
+
],
|
|
49
|
+
type: "multiselect",
|
|
50
|
+
},
|
|
51
|
+
{default: "2026-10-01T09:30:00-07:00", id: "meeting", label: "Meeting", type: "datetime"},
|
|
52
|
+
],
|
|
53
|
+
prompt: "Choices.",
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const field = (input: FormAskInput, id: string): FormField => {
|
|
57
|
+
const found = input.fields.find((candidate) => candidate.id === id);
|
|
58
|
+
if (!found) {
|
|
59
|
+
throw new Error(`No field ${id}`);
|
|
60
|
+
}
|
|
61
|
+
return found;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const error = (code: AskValidationError["code"], path: string): AskValidationError => ({
|
|
65
|
+
code,
|
|
66
|
+
fix: "Server fix.",
|
|
67
|
+
message: `Server message for ${path}.`,
|
|
68
|
+
path,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe("initialFormDraft", () => {
|
|
72
|
+
it("starts each field from its default, as the text its control edits", () => {
|
|
73
|
+
assert.deepEqual(initialFormDraft(EVERY_TYPE_INPUT), {
|
|
74
|
+
company: "Acme",
|
|
75
|
+
email: "",
|
|
76
|
+
notes: "",
|
|
77
|
+
phone: "",
|
|
78
|
+
remind: "",
|
|
79
|
+
seats: "12",
|
|
80
|
+
site: "",
|
|
81
|
+
start: "2026-10-01",
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("starts checkboxes unchecked, selects empty, and multiselects from their defaults", () => {
|
|
86
|
+
assert.deepEqual(initialFormDraft(CHOICES_INPUT), {
|
|
87
|
+
channels: ["email"],
|
|
88
|
+
meeting: "2026-10-01T09:30:00-07:00",
|
|
89
|
+
notify: false,
|
|
90
|
+
region: "",
|
|
91
|
+
terms: true,
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe("formDraftValues", () => {
|
|
97
|
+
it("sends the defaults of an untouched form, leaving blank fields out", () => {
|
|
98
|
+
assert.deepEqual(
|
|
99
|
+
formDraftValues({draft: initialFormDraft(EVERY_TYPE_INPUT), input: EVERY_TYPE_INPUT}),
|
|
100
|
+
{company: "Acme", seats: 12, start: "2026-10-01"}
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("always sends checkboxes, since unchecked is an answer", () => {
|
|
105
|
+
assert.deepEqual(
|
|
106
|
+
formDraftValues({draft: initialFormDraft(CHOICES_INPUT), input: CHOICES_INPUT}),
|
|
107
|
+
{channels: ["email"], meeting: "2026-10-01T09:30:00-07:00", notify: false, terms: true}
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("trims text, parses numbers, and keeps text that is not a number for validation to reject", () => {
|
|
112
|
+
const draft = {
|
|
113
|
+
...initialFormDraft(EVERY_TYPE_INPUT),
|
|
114
|
+
company: " Acme Corp ",
|
|
115
|
+
email: " ada@example.com ",
|
|
116
|
+
notes: " ",
|
|
117
|
+
seats: " 1.5 ",
|
|
118
|
+
};
|
|
119
|
+
assert.deepEqual(formDraftValues({draft, input: EVERY_TYPE_INPUT}), {
|
|
120
|
+
company: "Acme Corp",
|
|
121
|
+
email: "ada@example.com",
|
|
122
|
+
seats: 1.5,
|
|
123
|
+
start: "2026-10-01",
|
|
124
|
+
});
|
|
125
|
+
assert.deepEqual(
|
|
126
|
+
formDraftValues({draft: {...draft, seats: "twelve"}, input: EVERY_TYPE_INPUT}).seats,
|
|
127
|
+
"twelve"
|
|
128
|
+
);
|
|
129
|
+
assert.equal(
|
|
130
|
+
formDraftValues({draft: {...draft, seats: "0x10"}, input: EVERY_TYPE_INPUT}).seats,
|
|
131
|
+
"0x10"
|
|
132
|
+
);
|
|
133
|
+
assert.equal(
|
|
134
|
+
formDraftValues({draft: {...draft, seats: "-2"}, input: EVERY_TYPE_INPUT}).seats,
|
|
135
|
+
-2
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("sends a valid phone number in E.164 and anything else as typed", () => {
|
|
140
|
+
const draft = initialFormDraft(EVERY_TYPE_INPUT);
|
|
141
|
+
assert.equal(
|
|
142
|
+
formDraftValues({draft: {...draft, phone: "(415) 555-2671"}, input: EVERY_TYPE_INPUT}).phone,
|
|
143
|
+
"+14155552671"
|
|
144
|
+
);
|
|
145
|
+
assert.equal(
|
|
146
|
+
formDraftValues({draft: {...draft, phone: "+44 20 7946 0958"}, input: EVERY_TYPE_INPUT})
|
|
147
|
+
.phone,
|
|
148
|
+
"+442079460958"
|
|
149
|
+
);
|
|
150
|
+
assert.equal(
|
|
151
|
+
formDraftValues({draft: {...draft, phone: "555"}, input: EVERY_TYPE_INPUT}).phone,
|
|
152
|
+
"555"
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("date pickers", () => {
|
|
158
|
+
it("round-trips a date through the picker's UTC midnight ISO value", () => {
|
|
159
|
+
assert.equal(
|
|
160
|
+
toPickerValue({timezone: "America/Los_Angeles", type: "date", value: "2026-10-01"}),
|
|
161
|
+
"2026-10-01T00:00:00.000Z"
|
|
162
|
+
);
|
|
163
|
+
assert.equal(
|
|
164
|
+
fromPickerValue({
|
|
165
|
+
iso: "2026-10-01T00:00:00.000Z",
|
|
166
|
+
timezone: "America/Los_Angeles",
|
|
167
|
+
type: "date",
|
|
168
|
+
}),
|
|
169
|
+
"2026-10-01"
|
|
170
|
+
);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("reads a time in the picker's timezone as 24-hour HH:mm", () => {
|
|
174
|
+
const iso = toPickerValue({timezone: "America/Los_Angeles", type: "time", value: "21:05"});
|
|
175
|
+
assert.isString(iso);
|
|
176
|
+
assert.equal(
|
|
177
|
+
fromPickerValue({iso: iso as string, timezone: "America/Los_Angeles", type: "time"}),
|
|
178
|
+
"21:05"
|
|
179
|
+
);
|
|
180
|
+
assert.equal(
|
|
181
|
+
fromPickerValue({iso: "2026-10-01T16:30:00.000Z", timezone: "UTC", type: "time"}),
|
|
182
|
+
"16:30"
|
|
183
|
+
);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("sends a datetime with the picker timezone's offset", () => {
|
|
187
|
+
assert.equal(
|
|
188
|
+
fromPickerValue({
|
|
189
|
+
iso: "2026-10-01T16:30:00.000Z",
|
|
190
|
+
timezone: "America/Los_Angeles",
|
|
191
|
+
type: "datetime",
|
|
192
|
+
}),
|
|
193
|
+
"2026-10-01T09:30:00-07:00"
|
|
194
|
+
);
|
|
195
|
+
assert.equal(
|
|
196
|
+
toPickerValue({timezone: "UTC", type: "datetime", value: "2026-10-01T09:30:00-07:00"}),
|
|
197
|
+
"2026-10-01T16:30:00.000Z"
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("treats an empty or unreadable value as no value", () => {
|
|
202
|
+
assert.isUndefined(toPickerValue({timezone: "UTC", type: "date", value: ""}));
|
|
203
|
+
assert.isUndefined(toPickerValue({timezone: "UTC", type: "date", value: "2026-02-30"}));
|
|
204
|
+
assert.equal(fromPickerValue({iso: "", timezone: "UTC", type: "time"}), "");
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
describe("formFieldErrorText", () => {
|
|
209
|
+
const seats: FormField = {
|
|
210
|
+
id: "seats",
|
|
211
|
+
integer: true,
|
|
212
|
+
label: "Seats",
|
|
213
|
+
max: 500,
|
|
214
|
+
min: 1,
|
|
215
|
+
type: "number",
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
it("finds the field's error by its path and says how to fix it in plain words", () => {
|
|
219
|
+
assert.equal(
|
|
220
|
+
formFieldErrorText({errors: [error("OUT_OF_RANGE", "content.values.seats")], field: seats}),
|
|
221
|
+
"Enter a number from 1 to 500."
|
|
222
|
+
);
|
|
223
|
+
assert.equal(
|
|
224
|
+
formFieldErrorText({
|
|
225
|
+
errors: [error("FIELD_TYPE_MISMATCH", "content.values.seats")],
|
|
226
|
+
field: seats,
|
|
227
|
+
}),
|
|
228
|
+
"Enter a whole number."
|
|
229
|
+
);
|
|
230
|
+
assert.equal(
|
|
231
|
+
formFieldErrorText({errors: [error("REQUIRED_FIELD", "content.values.seats")], field: seats}),
|
|
232
|
+
"This field is required."
|
|
233
|
+
);
|
|
234
|
+
assert.isUndefined(
|
|
235
|
+
formFieldErrorText({errors: [error("OUT_OF_RANGE", "content.values.seat")], field: seats})
|
|
236
|
+
);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("says which bound a one-sided range breaks", () => {
|
|
240
|
+
assert.equal(
|
|
241
|
+
formFieldErrorText({
|
|
242
|
+
errors: [error("OUT_OF_RANGE", "content.values.seats")],
|
|
243
|
+
field: {...seats, max: undefined},
|
|
244
|
+
}),
|
|
245
|
+
"Enter a number of at least 1."
|
|
246
|
+
);
|
|
247
|
+
assert.equal(
|
|
248
|
+
formFieldErrorText({
|
|
249
|
+
errors: [error("OUT_OF_RANGE", "content.values.seats")],
|
|
250
|
+
field: {...seats, min: undefined},
|
|
251
|
+
}),
|
|
252
|
+
"Enter a number of at most 500."
|
|
253
|
+
);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("explains format, date, and length errors by field type", () => {
|
|
257
|
+
const cases: [FormField, AskValidationError["code"], string][] = [
|
|
258
|
+
[{id: "a", label: "A", type: "email"}, "FIELD_TYPE_MISMATCH", "Enter a valid email address."],
|
|
259
|
+
[
|
|
260
|
+
{id: "a", label: "A", type: "url"},
|
|
261
|
+
"FIELD_TYPE_MISMATCH",
|
|
262
|
+
"Enter a web address that starts with http:// or https://.",
|
|
263
|
+
],
|
|
264
|
+
[{id: "a", label: "A", type: "phone"}, "FIELD_TYPE_MISMATCH", "Enter a valid phone number."],
|
|
265
|
+
[{id: "a", label: "A", type: "date"}, "INVALID_DATE", "Enter a real date."],
|
|
266
|
+
[{id: "a", label: "A", type: "time"}, "INVALID_DATE", "Enter a real time."],
|
|
267
|
+
[{id: "a", label: "A", type: "datetime"}, "INVALID_DATE", "Enter a real date and time."],
|
|
268
|
+
[
|
|
269
|
+
{id: "a", label: "A", maxLength: 60, type: "text"},
|
|
270
|
+
"TOO_LONG",
|
|
271
|
+
"Keep this to 60 characters or fewer.",
|
|
272
|
+
],
|
|
273
|
+
[
|
|
274
|
+
{id: "a", label: "A", minLength: 3, type: "text"},
|
|
275
|
+
"TOO_SHORT",
|
|
276
|
+
"Write at least 3 characters.",
|
|
277
|
+
],
|
|
278
|
+
[
|
|
279
|
+
{id: "a", label: "A", options: [{id: "x", label: "X"}], type: "select"},
|
|
280
|
+
"OPTION_NOT_OFFERED",
|
|
281
|
+
"Choose one of the options.",
|
|
282
|
+
],
|
|
283
|
+
];
|
|
284
|
+
for (const [caseField, code, text] of cases) {
|
|
285
|
+
assert.equal(
|
|
286
|
+
formFieldErrorText({
|
|
287
|
+
errors: [error(code, `content.values.${caseField.id}`)],
|
|
288
|
+
field: caseField,
|
|
289
|
+
}),
|
|
290
|
+
text,
|
|
291
|
+
`${caseField.type} ${code}`
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("matches an error on one item of a multiselect, and falls back to the server's message", () => {
|
|
297
|
+
const channels = field(CHOICES_INPUT, "channels");
|
|
298
|
+
assert.equal(
|
|
299
|
+
formFieldErrorText({
|
|
300
|
+
errors: [error("DUPLICATE_ID", "content.values.channels[1]")],
|
|
301
|
+
field: channels,
|
|
302
|
+
}),
|
|
303
|
+
"Server message for content.values.channels[1]."
|
|
304
|
+
);
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
describe("formatFormValue", () => {
|
|
309
|
+
it("shows each value the way the user entered it", () => {
|
|
310
|
+
assert.equal(formatFormValue({field: field(CHOICES_INPUT, "notify"), value: false}), "No");
|
|
311
|
+
assert.equal(formatFormValue({field: field(CHOICES_INPUT, "terms"), value: true}), "Yes");
|
|
312
|
+
assert.equal(formatFormValue({field: field(CHOICES_INPUT, "region"), value: "eu"}), "EU");
|
|
313
|
+
assert.equal(
|
|
314
|
+
formatFormValue({field: field(CHOICES_INPUT, "channels"), value: ["sms", "email"]}),
|
|
315
|
+
"Text message, Email"
|
|
316
|
+
);
|
|
317
|
+
assert.equal(
|
|
318
|
+
formatFormValue({field: field(EVERY_TYPE_INPUT, "start"), value: "2026-10-01"}),
|
|
319
|
+
"Oct 1, 2026"
|
|
320
|
+
);
|
|
321
|
+
assert.equal(
|
|
322
|
+
formatFormValue({field: field(EVERY_TYPE_INPUT, "remind"), value: "21:05"}),
|
|
323
|
+
"9:05 PM"
|
|
324
|
+
);
|
|
325
|
+
assert.equal(
|
|
326
|
+
formatFormValue({
|
|
327
|
+
field: field(CHOICES_INPUT, "meeting"),
|
|
328
|
+
value: "2026-10-01T09:30:00-07:00",
|
|
329
|
+
}),
|
|
330
|
+
"Oct 1, 2026, 9:30 AM UTC-7"
|
|
331
|
+
);
|
|
332
|
+
assert.equal(formatFormValue({field: field(EVERY_TYPE_INPUT, "seats"), value: 1200}), "1,200");
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("shortens long text and shows values it cannot read as sent", () => {
|
|
336
|
+
const notes = field(EVERY_TYPE_INPUT, "notes");
|
|
337
|
+
const shown = formatFormValue({field: notes, value: `${"word ".repeat(40)}end`});
|
|
338
|
+
assert.isAtMost(shown.length, 81);
|
|
339
|
+
assert.isTrue(shown.endsWith("…"));
|
|
340
|
+
assert.equal(formatFormValue({field: notes, value: "Line one\nLine two"}), "Line one Line two");
|
|
341
|
+
assert.equal(
|
|
342
|
+
formatFormValue({field: field(EVERY_TYPE_INPUT, "start"), value: "someday"}),
|
|
343
|
+
"someday"
|
|
344
|
+
);
|
|
345
|
+
assert.equal(formatFormValue({field: field(CHOICES_INPUT, "region"), value: "mars"}), "mars");
|
|
346
|
+
});
|
|
347
|
+
});
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AskValidationError,
|
|
3
|
+
type FormAskInput,
|
|
4
|
+
type FormField,
|
|
5
|
+
type FormValue,
|
|
6
|
+
formTextMaxLength,
|
|
7
|
+
} from "@terreno/blocks";
|
|
8
|
+
import {parsePhoneNumberFromString} from "libphonenumber-js";
|
|
9
|
+
import {DateTime} from "luxon";
|
|
10
|
+
|
|
11
|
+
import {formatCount} from "./askSummary";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* What each form control edits: text for text-like, number, and date fields (dates in the answer's
|
|
15
|
+
* ISO format), a boolean for checkboxes, and option ids for selects.
|
|
16
|
+
*/
|
|
17
|
+
export type FormDraftValue = boolean | string | string[];
|
|
18
|
+
|
|
19
|
+
export type FormDraft = Record<string, FormDraftValue>;
|
|
20
|
+
|
|
21
|
+
type DateFieldType = "date" | "datetime" | "time";
|
|
22
|
+
|
|
23
|
+
const ANSWER_PREVIEW_LENGTH = 80;
|
|
24
|
+
/** Plain decimal notation only, so "0x10" or "1e3" is not read as a different number than typed. */
|
|
25
|
+
const DECIMAL_TEXT = /^-?(?:\d+\.?\d*|\.\d+)$/;
|
|
26
|
+
const TIME_FORMAT = "HH:mm";
|
|
27
|
+
const WHITESPACE = /\s+/g;
|
|
28
|
+
|
|
29
|
+
const UNFINISHED_ENTRY_TEXT: Record<DateFieldType, string> = {
|
|
30
|
+
date: "Enter a complete date, or clear it.",
|
|
31
|
+
datetime: "Enter a complete date and time, or clear it.",
|
|
32
|
+
time: "Enter a complete time, or clear it.",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Field ids whose date control holds an unfinished or invalid entry, such as "0 / 5 / 026". The
|
|
37
|
+
* draft cannot hold such an entry, so these block Submit until the user finishes or clears them.
|
|
38
|
+
*/
|
|
39
|
+
export type UnfinishedEntries = Record<string, true>;
|
|
40
|
+
|
|
41
|
+
/** What a date field says while its control holds an unfinished entry; undefined for other types. */
|
|
42
|
+
export const unfinishedEntryText = (field: FormField): string | undefined =>
|
|
43
|
+
field.type === "date" || field.type === "time" || field.type === "datetime"
|
|
44
|
+
? UNFINISHED_ENTRY_TEXT[field.type]
|
|
45
|
+
: undefined;
|
|
46
|
+
|
|
47
|
+
/** The draft a form starts from: each field's default, or empty. Checkboxes start unchecked. */
|
|
48
|
+
export const initialFormDraft = (input: FormAskInput): FormDraft =>
|
|
49
|
+
Object.fromEntries(
|
|
50
|
+
input.fields.map((field): [string, FormDraftValue] => {
|
|
51
|
+
switch (field.type) {
|
|
52
|
+
case "boolean":
|
|
53
|
+
return [field.id, field.default ?? false];
|
|
54
|
+
case "multiselect":
|
|
55
|
+
return [field.id, field.default ?? []];
|
|
56
|
+
case "number":
|
|
57
|
+
return [field.id, field.default === undefined ? "" : String(field.default)];
|
|
58
|
+
default:
|
|
59
|
+
return [field.id, field.default ?? ""];
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
/** A valid phone number in E.164, read as a US number when it has no country code. */
|
|
65
|
+
const phoneAnswer = (text: string): string => {
|
|
66
|
+
const parsed = parsePhoneNumberFromString(text, "US");
|
|
67
|
+
return parsed?.isValid() ? parsed.number : text;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const draftAnswer = (field: FormField, draft: FormDraftValue): FormValue | undefined => {
|
|
71
|
+
if (typeof draft === "boolean" || Array.isArray(draft)) {
|
|
72
|
+
return Array.isArray(draft) && draft.length === 0 ? undefined : draft;
|
|
73
|
+
}
|
|
74
|
+
const text = draft.trim();
|
|
75
|
+
if (text === "") {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
if (field.type === "number") {
|
|
79
|
+
return DECIMAL_TEXT.test(text) ? Number(text) : text;
|
|
80
|
+
}
|
|
81
|
+
if (field.type === "phone") {
|
|
82
|
+
return phoneAnswer(text);
|
|
83
|
+
}
|
|
84
|
+
return text;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The answer's `values` for a draft. Blank fields are left out; checkboxes are always sent, since
|
|
89
|
+
* unchecked is an answer. Text that does not parse stays text so validation can say why.
|
|
90
|
+
*/
|
|
91
|
+
export const formDraftValues = ({
|
|
92
|
+
draft,
|
|
93
|
+
input,
|
|
94
|
+
}: {
|
|
95
|
+
draft: FormDraft;
|
|
96
|
+
input: FormAskInput;
|
|
97
|
+
}): Record<string, FormValue> =>
|
|
98
|
+
Object.fromEntries(
|
|
99
|
+
input.fields.flatMap((field) => {
|
|
100
|
+
const value = draftAnswer(field, draft[field.id] ?? "");
|
|
101
|
+
return value === undefined ? [] : [[field.id, value]];
|
|
102
|
+
})
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
/** The UTC ISO value `DateTimeField` shows for an answer-format date, time, or datetime. */
|
|
106
|
+
export const toPickerValue = ({
|
|
107
|
+
timezone,
|
|
108
|
+
type,
|
|
109
|
+
value,
|
|
110
|
+
}: {
|
|
111
|
+
timezone: string;
|
|
112
|
+
type: DateFieldType;
|
|
113
|
+
value: string;
|
|
114
|
+
}): string | undefined => {
|
|
115
|
+
if (value === "") {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
let parsed: DateTime;
|
|
119
|
+
if (type === "date") {
|
|
120
|
+
parsed = DateTime.fromISO(value, {zone: "UTC"});
|
|
121
|
+
} else if (type === "time") {
|
|
122
|
+
parsed = DateTime.fromFormat(value, TIME_FORMAT, {zone: timezone});
|
|
123
|
+
} else {
|
|
124
|
+
parsed = DateTime.fromISO(value, {setZone: true});
|
|
125
|
+
}
|
|
126
|
+
return parsed.isValid ? (parsed.toUTC().toISO() ?? undefined) : undefined;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The answer-format value for what `DateTimeField` emitted: YYYY-MM-DD, 24-hour HH:mm in the
|
|
131
|
+
* picker's timezone, or an ISO datetime with the picker timezone's offset.
|
|
132
|
+
*/
|
|
133
|
+
export const fromPickerValue = ({
|
|
134
|
+
iso,
|
|
135
|
+
timezone,
|
|
136
|
+
type,
|
|
137
|
+
}: {
|
|
138
|
+
iso: string;
|
|
139
|
+
timezone: string;
|
|
140
|
+
type: DateFieldType;
|
|
141
|
+
}): string => {
|
|
142
|
+
if (iso === "") {
|
|
143
|
+
return "";
|
|
144
|
+
}
|
|
145
|
+
if (type === "date") {
|
|
146
|
+
return DateTime.fromISO(iso, {zone: "UTC"}).toISODate() ?? "";
|
|
147
|
+
}
|
|
148
|
+
const zoned = DateTime.fromISO(iso).setZone(timezone);
|
|
149
|
+
if (type === "time") {
|
|
150
|
+
return zoned.toFormat(TIME_FORMAT);
|
|
151
|
+
}
|
|
152
|
+
return zoned.toISO({suppressMilliseconds: true}) ?? "";
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const characters = (count: number): string =>
|
|
156
|
+
`${formatCount(count)} ${count === 1 ? "character" : "characters"}`;
|
|
157
|
+
|
|
158
|
+
const rangeText = (field: FormField): string | undefined => {
|
|
159
|
+
if (field.type !== "number") {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
if (field.min !== undefined && field.max !== undefined) {
|
|
163
|
+
return `Enter a number from ${field.min} to ${field.max}.`;
|
|
164
|
+
}
|
|
165
|
+
if (field.min !== undefined) {
|
|
166
|
+
return `Enter a number of at least ${field.min}.`;
|
|
167
|
+
}
|
|
168
|
+
if (field.max !== undefined) {
|
|
169
|
+
return `Enter a number of at most ${field.max}.`;
|
|
170
|
+
}
|
|
171
|
+
return undefined;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const MISMATCH_TEXT: Partial<Record<FormField["type"], string>> = {
|
|
175
|
+
email: "Enter a valid email address.",
|
|
176
|
+
phone: "Enter a valid phone number.",
|
|
177
|
+
url: "Enter a web address that starts with http:// or https://.",
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const INVALID_DATE_TEXT: Record<DateFieldType, string> = {
|
|
181
|
+
date: "Enter a real date.",
|
|
182
|
+
datetime: "Enter a real date and time.",
|
|
183
|
+
time: "Enter a real time.",
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const mismatchText = (field: FormField): string | undefined => {
|
|
187
|
+
if (field.type === "number") {
|
|
188
|
+
return field.integer === true ? "Enter a whole number." : "Enter a number.";
|
|
189
|
+
}
|
|
190
|
+
return MISMATCH_TEXT[field.type];
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const friendlyText = (field: FormField, error: AskValidationError): string | undefined => {
|
|
194
|
+
switch (error.code) {
|
|
195
|
+
case "REQUIRED_FIELD":
|
|
196
|
+
return "This field is required.";
|
|
197
|
+
case "OUT_OF_RANGE":
|
|
198
|
+
return rangeText(field);
|
|
199
|
+
case "FIELD_TYPE_MISMATCH":
|
|
200
|
+
return mismatchText(field);
|
|
201
|
+
case "INVALID_DATE":
|
|
202
|
+
return field.type === "date" || field.type === "time" || field.type === "datetime"
|
|
203
|
+
? INVALID_DATE_TEXT[field.type]
|
|
204
|
+
: undefined;
|
|
205
|
+
case "TOO_LONG":
|
|
206
|
+
return `Keep this to ${characters(formTextMaxLength(field))} or fewer.`;
|
|
207
|
+
case "TOO_SHORT": {
|
|
208
|
+
const min = field.type === "text" || field.type === "textarea" ? field.minLength : undefined;
|
|
209
|
+
return min ? `Write at least ${characters(min)}.` : undefined;
|
|
210
|
+
}
|
|
211
|
+
case "OPTION_NOT_OFFERED":
|
|
212
|
+
return field.type === "select" ? "Choose one of the options." : undefined;
|
|
213
|
+
default:
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/** Whether an error's path points at a field's value or one of its items. */
|
|
219
|
+
export const isFormFieldError = (error: AskValidationError, field: FormField): boolean => {
|
|
220
|
+
const path = `content.values.${field.id}`;
|
|
221
|
+
return (
|
|
222
|
+
error.path === path || error.path.startsWith(`${path}[`) || error.path.startsWith(`${path}.`)
|
|
223
|
+
);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Plain words for the first error on a field, such as "Enter a number from 1 to 500.", or the
|
|
228
|
+
* error's own message when no plainer text fits.
|
|
229
|
+
*/
|
|
230
|
+
export const formFieldErrorText = ({
|
|
231
|
+
errors,
|
|
232
|
+
field,
|
|
233
|
+
}: {
|
|
234
|
+
errors: AskValidationError[];
|
|
235
|
+
field: FormField;
|
|
236
|
+
}): string | undefined => {
|
|
237
|
+
const error = errors.find((candidate) => isFormFieldError(candidate, field));
|
|
238
|
+
if (!error) {
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
return friendlyText(field, error) ?? error.message;
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const optionLabel = (
|
|
245
|
+
field: Extract<FormField, {type: "multiselect" | "select"}>,
|
|
246
|
+
id: string
|
|
247
|
+
): string => field.options.find((option) => option.id === id)?.label ?? id;
|
|
248
|
+
|
|
249
|
+
const formatNumber = (value: number): string => {
|
|
250
|
+
const [whole = "", fraction] = String(value).split(".");
|
|
251
|
+
return fraction === undefined
|
|
252
|
+
? formatCount(Number(whole))
|
|
253
|
+
: `${formatCount(Number(whole))}.${fraction}`;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const formatDate = (type: DateFieldType, value: string): string => {
|
|
257
|
+
if (type === "time") {
|
|
258
|
+
const time = DateTime.fromFormat(value, TIME_FORMAT);
|
|
259
|
+
return time.isValid ? time.toFormat("h:mm a") : value;
|
|
260
|
+
}
|
|
261
|
+
const parsed = DateTime.fromISO(value, {setZone: true});
|
|
262
|
+
if (!parsed.isValid) {
|
|
263
|
+
return value;
|
|
264
|
+
}
|
|
265
|
+
return parsed.toFormat(type === "date" ? "MMM d, yyyy" : "MMM d, yyyy, h:mm a ZZZZ");
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const previewText = (value: string): string => {
|
|
269
|
+
const text = value.replace(WHITESPACE, " ").trim();
|
|
270
|
+
return text.length > ANSWER_PREVIEW_LENGTH
|
|
271
|
+
? `${text.slice(0, ANSWER_PREVIEW_LENGTH).trimEnd()}…`
|
|
272
|
+
: text;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* One answered value as the user would read it: Yes or No, option labels, a readable date or
|
|
277
|
+
* time, or text shortened to one line.
|
|
278
|
+
*/
|
|
279
|
+
export const formatFormValue = ({field, value}: {field: FormField; value: unknown}): string => {
|
|
280
|
+
if (typeof value === "boolean") {
|
|
281
|
+
return value ? "Yes" : "No";
|
|
282
|
+
}
|
|
283
|
+
if (typeof value === "number") {
|
|
284
|
+
return formatNumber(value);
|
|
285
|
+
}
|
|
286
|
+
if (Array.isArray(value)) {
|
|
287
|
+
return value
|
|
288
|
+
.map((id) =>
|
|
289
|
+
field.type === "multiselect" || field.type === "select"
|
|
290
|
+
? optionLabel(field, String(id))
|
|
291
|
+
: String(id)
|
|
292
|
+
)
|
|
293
|
+
.join(", ");
|
|
294
|
+
}
|
|
295
|
+
if (typeof value !== "string") {
|
|
296
|
+
return JSON.stringify(value) ?? "";
|
|
297
|
+
}
|
|
298
|
+
switch (field.type) {
|
|
299
|
+
case "select":
|
|
300
|
+
case "multiselect":
|
|
301
|
+
return optionLabel(field, value);
|
|
302
|
+
case "date":
|
|
303
|
+
case "time":
|
|
304
|
+
case "datetime":
|
|
305
|
+
return formatDate(field.type, value);
|
|
306
|
+
default:
|
|
307
|
+
return previewText(value);
|
|
308
|
+
}
|
|
309
|
+
};
|