foldkit-form 0.1.0 → 0.1.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.
- package/README.md +192 -15
- package/dist/index.d.mts +712 -53
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +395 -56
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/input.ts","../src/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/input.ts","../src/index.ts"],"mappings":";;;;;;;;;;;KAkBY,2BAA2B;;KAG3B;;UAGK,QAAQ;;WAEd;WACA,OAAO;;WAEP;;;;;WAKA;;WAEA,MAAM;;;;;WAKN,UAAU;;WAEV;;;;;WAKA,UAAU;;;UAIJ;WACN;WACA,UAAU;;;UAIJ,YAAY;WAClB;WACA,KAAK,MAAM,SAAS,QAAQ;WAC5B,KAAK,SAAS,YAAY,WAAW,QAAQ;;;;;;UAOvC;WACN,SAAS,UAAU,qBAAqB,gBAAgB;;;UAOlD,QAAQ;WACd;WACA,OAAO;;;UAID;WACN,UAAU;aACR;aACA,SAAS;aACT;aACA;aACA;;WAEF,QAAQ,cAAc;WACtB,OAAO,cAAc,eAAe,cAAc;WAClD,SAAS,cAAc;WACvB,cAAc,cAAc;WAC5B;aACE,UAAU;aACV,UAAU;aACV,WAAW;aACX,SAAS;aACT,WAAW;aACX,aAAa;;;;UA+BT;WACN,QAAQ;WACR;;;UAIM;WACN;;WAEA;WACA,MAAM;;qBA6DJ;;;;;;EAjGC,OAAA,OAAI,uBAAA,cACJ;aAED,OAAO;aACP;aACA,uBAAuB,MAAM;aAC7B,SAAS;aACT;QAEV,YAAY;;;;;;;aA6CyB,SAAA;;;;;EA8D5B,YAAA;;EAEE,cAAA;EACG,iBAAA;EACH,cAAA;EACA,cAAA;EACM,SAAA,SAAA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UAAA,QAAA;;;;;;;;;;;;IAiCF,UAAA,OAAA,UAAA,QAAA,aAAA,QAAO,OAAA,OAAA,KAAA,gBAAA,OAAA,OAAA,iBAAA,SAAA,OAAA,OAAA,gBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAlXG,eAAA,OAAA,UAAA,QAAA;;MAuYV,QAAA,OAAA,UAAA,QAAA,aAAA,OAAA,OAAA,KAAA,gBAAA,OAAA,OAAA,iBAAA,SAAA,OAAA,OAAA,gBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAnJM;eACpB,MAAM;eACN,GAAG;eACH,QAAQ;eACR,SAAS;eACT,GAAG;eACH,MAAM;;;;;;;;EAuJN,QAAA,eAAM,yBAAsB;;;;;EAOzB,UAAA;aAAW,UAAU"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,53 @@
|
|
|
1
|
-
import { Effect, Result, Schema } from "effect";
|
|
1
|
+
import { Effect, Pipeable, Result, Schema } from "effect";
|
|
2
2
|
import { Bundle } from "foldkit-bundle";
|
|
3
3
|
import { Metadata } from "foldkit-metadata";
|
|
4
4
|
import * as FieldValidation from "foldkit/fieldValidation";
|
|
5
5
|
import { defineMessageUnion } from "foldkit/message";
|
|
6
6
|
//#region src/input.ts
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const isControlChange = (value) => "change" in value;
|
|
8
|
+
/** A kind of control. The kinds below are made with it, and so is an application's. */
|
|
9
|
+
const kind = (name, spec) => ({
|
|
10
|
+
kind: name,
|
|
11
|
+
of: (data) => Object.freeze({
|
|
12
|
+
kind: name,
|
|
13
|
+
draft: spec.draft,
|
|
14
|
+
shown: spec.shown ?? true,
|
|
15
|
+
searches: typeof spec.searches === "function" ? spec.searches(data) : spec.searches ?? false,
|
|
16
|
+
data,
|
|
17
|
+
parse: spec.parse,
|
|
18
|
+
unparsed: spec.unparsed
|
|
19
|
+
}),
|
|
20
|
+
is: (control) => control.kind === name
|
|
21
|
+
});
|
|
22
|
+
const nothing = Object.freeze({});
|
|
23
|
+
const Text = kind("Text", { draft: "text" });
|
|
24
|
+
const Hidden = kind("Hidden", {
|
|
25
|
+
draft: "text",
|
|
26
|
+
shown: false
|
|
27
|
+
});
|
|
28
|
+
const Multiline = kind("Multiline", { draft: "text" });
|
|
29
|
+
const Number_ = kind("Number", {
|
|
30
|
+
draft: "text",
|
|
31
|
+
parse: (draft) => {
|
|
32
|
+
const value = globalThis.Number(draft);
|
|
33
|
+
return draft.trim() === "" || !globalThis.Number.isFinite(value) ? void 0 : value;
|
|
34
|
+
},
|
|
35
|
+
unparsed: "Enter a number"
|
|
36
|
+
});
|
|
37
|
+
const Toggle = kind("Toggle", { draft: "flag" });
|
|
38
|
+
const Select = kind("Select", { draft: "text" });
|
|
39
|
+
const RelationOne = kind("RelationOne", {
|
|
40
|
+
draft: "text",
|
|
41
|
+
searches: (data) => data.search
|
|
42
|
+
});
|
|
43
|
+
const RelationMany = kind("RelationMany", {
|
|
44
|
+
draft: "list",
|
|
45
|
+
searches: (data) => data.search
|
|
46
|
+
});
|
|
47
|
+
const Nested = kind("Nested", { draft: "rows" });
|
|
9
48
|
const key = Metadata.key("foldkit-form/input", {
|
|
10
49
|
merge: (controls) => controls.slice(-1),
|
|
11
|
-
summarize: (control) => control.
|
|
50
|
+
summarize: (control) => control.kind
|
|
12
51
|
});
|
|
13
52
|
/** The schema with `null` and `undefined` set aside: optional text is still text. */
|
|
14
53
|
const present = (ast) => ast._tag === "Union" ? (ast.types ?? []).filter((member) => member._tag !== "Null" && member._tag !== "Undefined") : [ast];
|
|
@@ -16,26 +55,69 @@ const fromSchema = (schema) => {
|
|
|
16
55
|
const members = present(schema.ast);
|
|
17
56
|
const [only] = members;
|
|
18
57
|
if (members.length === 1 && only !== void 0) {
|
|
19
|
-
if (only._tag === "String") return
|
|
20
|
-
if (only._tag === "Number") return
|
|
21
|
-
if (only._tag === "Boolean") return
|
|
58
|
+
if (only._tag === "String") return Text.of(nothing);
|
|
59
|
+
if (only._tag === "Number") return Number_.of(nothing);
|
|
60
|
+
if (only._tag === "Boolean") return Toggle.of(nothing);
|
|
22
61
|
}
|
|
23
62
|
const literals = members.map((member) => member._tag === "Literal" ? member.literal : void 0);
|
|
24
|
-
return literals.length > 0 && literals.every((literal) => typeof literal === "string") ? {
|
|
25
|
-
_tag: "Select",
|
|
26
|
-
options: literals
|
|
27
|
-
} : void 0;
|
|
63
|
+
return literals.length > 0 && literals.every((literal) => typeof literal === "string") ? Select.of({ options: literals }) : void 0;
|
|
28
64
|
};
|
|
29
65
|
const Input = {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
66
|
+
/**
|
|
67
|
+
* A kind of control. `Input.kind<{ min: string }>('Date', { draft: 'text' })`
|
|
68
|
+
* gives `.of(data)` to make one and `.is(control)` to tell one, exactly as the
|
|
69
|
+
* kinds below were made. A view draws it once it is given a renderer for `Date`.
|
|
70
|
+
*/
|
|
71
|
+
kind,
|
|
72
|
+
Text,
|
|
73
|
+
Hidden,
|
|
74
|
+
Multiline,
|
|
75
|
+
Number: Number_,
|
|
76
|
+
Toggle,
|
|
77
|
+
Select,
|
|
78
|
+
RelationOne,
|
|
79
|
+
RelationMany,
|
|
80
|
+
Nested,
|
|
81
|
+
text: () => Text.of(nothing),
|
|
82
|
+
/** Carried and submitted, not shown: the id of the thing being edited. Set it with `fill`. */
|
|
83
|
+
hidden: () => Hidden.of(nothing),
|
|
84
|
+
multiline: () => Multiline.of(nothing),
|
|
85
|
+
number: () => Number_.of(nothing),
|
|
86
|
+
toggle: () => Toggle.of(nothing),
|
|
87
|
+
select: (options) => Select.of({ options }),
|
|
88
|
+
/**
|
|
89
|
+
* For a relation key, under `inputs`: its picker, with a search. Too many to
|
|
90
|
+
* list is the usual case for a relation; the form holds the search text, and
|
|
91
|
+
* the query that lists the options takes it as input.
|
|
92
|
+
*/
|
|
93
|
+
search: () => ({ change: (resolved, inputKey) => {
|
|
94
|
+
if (resolved !== void 0 && RelationOne.is(resolved)) return RelationOne.of({
|
|
95
|
+
...resolved.data,
|
|
96
|
+
search: true
|
|
97
|
+
});
|
|
98
|
+
if (resolved !== void 0 && RelationMany.is(resolved)) return RelationMany.of({
|
|
99
|
+
...resolved.data,
|
|
100
|
+
search: true
|
|
101
|
+
});
|
|
102
|
+
throw new Error(`"${inputKey}" is not a relation, so it has no picker to search`);
|
|
103
|
+
} }),
|
|
104
|
+
/**
|
|
105
|
+
* Under `inputs`: the key's own control, following another key. Until the
|
|
106
|
+
* author writes this key, its draft is `through` of that key's text, rewritten
|
|
107
|
+
* as that key is edited; once they write it, it is theirs. Emptying it hands
|
|
108
|
+
* it back. A form filled with a value for it does not follow: an address that
|
|
109
|
+
* is already published must not move because its title did.
|
|
110
|
+
*/
|
|
111
|
+
following: (key, through = (text) => text) => ({ change: (resolved, inputKey) => {
|
|
112
|
+
if (resolved === void 0 || resolved.draft !== "text") throw new Error(`"${inputKey}" holds no text, so it cannot follow "${key}"`);
|
|
113
|
+
return Object.freeze({
|
|
114
|
+
...resolved,
|
|
115
|
+
follows: {
|
|
116
|
+
key,
|
|
117
|
+
through
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
} }),
|
|
39
121
|
/**
|
|
40
122
|
* Entity metadata: the control a member is edited with wherever it appears,
|
|
41
123
|
* e.g. `Entity.annotateMembers({ body: Input.of(Input.multiline()) })`.
|
|
@@ -54,14 +136,11 @@ const Input = {
|
|
|
54
136
|
const [explicit] = key.get(owner.metadata);
|
|
55
137
|
if (explicit !== void 0) return explicit;
|
|
56
138
|
if (member._tag === "Field") return fromSchema(schema);
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
target
|
|
61
|
-
} : {
|
|
62
|
-
_tag: "RelationOne",
|
|
63
|
-
target
|
|
139
|
+
const data = {
|
|
140
|
+
target: member.relation.target(),
|
|
141
|
+
search: false
|
|
64
142
|
};
|
|
143
|
+
return member.relation.cardinality === "many" ? RelationMany.of(data) : RelationOne.of(data);
|
|
65
144
|
}
|
|
66
145
|
};
|
|
67
146
|
//#endregion
|
|
@@ -76,6 +155,13 @@ const Input = {
|
|
|
76
155
|
* happens to it (a Remote mutation, a Sync operation, a plain `update`) is the
|
|
77
156
|
* parent's.
|
|
78
157
|
*/
|
|
158
|
+
/**
|
|
159
|
+
* Words with their blanks filled: `fillWords('{label} is required', { label: 'Title' })`.
|
|
160
|
+
* A blank with no value is left as it is written. Words as text, not functions,
|
|
161
|
+
* can be kept in one place, translated, and passed anywhere, a view's inputs
|
|
162
|
+
* included, where Foldkit admits no nested function.
|
|
163
|
+
*/
|
|
164
|
+
const fillWords = (template, values) => template.replace(/\{(\w+)\}/g, (blank, name) => Object.hasOwn(values, name) ? String(values[name]) : blank);
|
|
79
165
|
const labelKey = Metadata.key("foldkit-form/label", {
|
|
80
166
|
merge: (labels) => labels.slice(-1),
|
|
81
167
|
summarize: (label) => label.label
|
|
@@ -92,8 +178,8 @@ const isEdit = (message) => message._tag === "Nested" ? isEdit(message.message)
|
|
|
92
178
|
const kindOf = (draft) => typeof draft === "string" ? "text" : typeof draft === "boolean" ? "flag" : Array.isArray(draft) && draft.every((item) => typeof item === "string") ? "list" : void 0;
|
|
93
179
|
const sameDraft = (left, right) => Array.isArray(left) && Array.isArray(right) ? left.length === right.length && left.every((item, index) => item === right[index]) : left === right;
|
|
94
180
|
const isEmpty = (draft) => draft === "" || Array.isArray(draft) && draft.length === 0;
|
|
95
|
-
/**
|
|
96
|
-
const isBlank = (control, draft) => isEmpty(draft) || control.
|
|
181
|
+
/** Text that is read as something else is blank when it is only spaces: `Number(' ')` is 0, which nobody typed. */
|
|
182
|
+
const isBlank = (control, draft) => isEmpty(draft) || control.parse !== void 0 && typeof draft === "string" && draft.trim() === "";
|
|
97
183
|
const annotationsOf = (schema) => Schema.resolveAnnotations(schema) ?? {};
|
|
98
184
|
/** The schema's `title`, else `Form.label` metadata on the member, else the key. */
|
|
99
185
|
const wordsOf = (key, schema, member) => {
|
|
@@ -107,20 +193,20 @@ const wordsOf = (key, schema, member) => {
|
|
|
107
193
|
description: description ?? labelled?.description
|
|
108
194
|
};
|
|
109
195
|
};
|
|
110
|
-
const nestedPlanOf = (name, key, schema, member,
|
|
196
|
+
const nestedPlanOf = (name, key, schema, member, given, inherited) => {
|
|
111
197
|
const accepts = Schema.is(Schema.toType(schema));
|
|
112
198
|
const cardinality = member.relation.cardinality;
|
|
113
199
|
const optional = cardinality === "many" || accepts(void 0) || accepts(null);
|
|
114
|
-
|
|
200
|
+
if (given !== void 0 && given.input !== member.input) fail(name, `"${key}" is given a form of another input than the one it nests; make it from the input passed to Relation.nested`);
|
|
201
|
+
const form = given ?? makeNested(`${name}.${key}`, member.input, inherited);
|
|
115
202
|
return {
|
|
116
203
|
key,
|
|
117
204
|
...wordsOf(key, schema, member),
|
|
118
|
-
control: {
|
|
119
|
-
_tag: "Nested",
|
|
205
|
+
control: Input.Nested.of({
|
|
120
206
|
cardinality,
|
|
121
207
|
optional,
|
|
122
208
|
form
|
|
123
|
-
},
|
|
209
|
+
}),
|
|
124
210
|
required: !optional,
|
|
125
211
|
member,
|
|
126
212
|
form,
|
|
@@ -130,15 +216,16 @@ const nestedPlanOf = (name, key, schema, member, options) => {
|
|
|
130
216
|
};
|
|
131
217
|
};
|
|
132
218
|
const planOf = (name, key, schema, member, override, messages) => {
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
219
|
+
const resolved = Input.resolve(member, schema);
|
|
220
|
+
const control = (override === void 0 ? resolved : isControlChange(override) ? attempt(name, () => override.change(resolved, key)) : override) ?? fail(name, `no control for "${key}"; name one under "inputs"`);
|
|
221
|
+
if (control.draft === "rows") return fail(name, `"${key}" cannot be given a ${control.kind} control; map it with Relation.nested`);
|
|
222
|
+
const kind = control.draft;
|
|
136
223
|
const type = Schema.toType(schema);
|
|
137
224
|
const decode = Schema.decodeUnknownResult(type);
|
|
138
225
|
const accepts = Schema.is(type);
|
|
139
226
|
const empty = kind === "flag" ? false : kind === "list" ? [] : "";
|
|
140
227
|
const samples = kind === "flag" ? [true, false] : kind === "list" ? [[], ["id"]] : [];
|
|
141
|
-
if (samples.length > 0 && !samples.some(accepts)) fail(name, `"${key}" is edited as ${control.
|
|
228
|
+
if (samples.length > 0 && !samples.some(accepts)) fail(name, `"${key}" is edited as ${control.kind}, but its schema accepts no such value`);
|
|
142
229
|
const { label, description } = wordsOf(key, schema, member);
|
|
143
230
|
const field = {
|
|
144
231
|
key,
|
|
@@ -146,9 +233,19 @@ const planOf = (name, key, schema, member, override, messages) => {
|
|
|
146
233
|
control
|
|
147
234
|
};
|
|
148
235
|
const say = {
|
|
149
|
-
required: messages.required
|
|
150
|
-
|
|
151
|
-
|
|
236
|
+
required: worded(messages.required, [field], {
|
|
237
|
+
label,
|
|
238
|
+
key
|
|
239
|
+
}) ?? "Required",
|
|
240
|
+
unparsed: worded(messages.unparsed, [field], {
|
|
241
|
+
label,
|
|
242
|
+
key
|
|
243
|
+
}) ?? control.unparsed ?? "Not valid",
|
|
244
|
+
invalid: (message) => worded(messages.invalid, [field, message], {
|
|
245
|
+
label,
|
|
246
|
+
key,
|
|
247
|
+
message
|
|
248
|
+
}) ?? message
|
|
152
249
|
};
|
|
153
250
|
const check = (draft) => {
|
|
154
251
|
if (isBlank(control, draft)) {
|
|
@@ -159,8 +256,8 @@ const planOf = (name, key, schema, member, override, messages) => {
|
|
|
159
256
|
]) if (accepts(nothing)) return Result.succeed(nothing);
|
|
160
257
|
return Result.fail(say.required);
|
|
161
258
|
}
|
|
162
|
-
const value = control.
|
|
163
|
-
if (
|
|
259
|
+
const value = control.parse !== void 0 && typeof draft === "string" ? control.parse(draft) : draft;
|
|
260
|
+
if (value === void 0) return Result.fail(say.unparsed);
|
|
164
261
|
return Result.mapError(decode(value), (error) => say.invalid(error.message));
|
|
165
262
|
};
|
|
166
263
|
const required = kind !== "flag" && Result.isFailure(check(empty));
|
|
@@ -184,12 +281,22 @@ const planOf = (name, key, schema, member, override, messages) => {
|
|
|
184
281
|
})
|
|
185
282
|
};
|
|
186
283
|
};
|
|
284
|
+
/** Words given as text have their blanks filled; given as a function, it is asked. */
|
|
285
|
+
const worded = (words, args, values) => words === void 0 ? void 0 : typeof words === "string" ? fillWords(words, values) : words(...args);
|
|
187
286
|
const fail = (name, message) => {
|
|
188
287
|
throw new Error(`Form "${name}": ${message}`);
|
|
189
288
|
};
|
|
289
|
+
/** A failure inside a control change, said as the form's. */
|
|
290
|
+
const attempt = (name, run) => {
|
|
291
|
+
try {
|
|
292
|
+
return run();
|
|
293
|
+
} catch (error) {
|
|
294
|
+
return fail(name, error instanceof Error ? error.message : String(error));
|
|
295
|
+
}
|
|
296
|
+
};
|
|
190
297
|
/** The draft that shows `value`: a number as its text, nothing as the empty draft. */
|
|
191
298
|
const draftOf = (plan, value) => value === null || value === void 0 ? plan.empty : plan.kind === "text" ? String(value) : value;
|
|
192
|
-
const
|
|
299
|
+
const Core = {
|
|
193
300
|
/**
|
|
194
301
|
* A form for one operation's input. A key mapped with `Relation.nested` holds
|
|
195
302
|
* rows of a form of its own, built here from the nested input; every other key
|
|
@@ -203,11 +310,10 @@ const Form = {
|
|
|
203
310
|
const rowsKeys = everyKey.filter((key) => members[key]._tag === "NestedInput");
|
|
204
311
|
const overrides = options.inputs ?? {};
|
|
205
312
|
const plans = Object.fromEntries(keys.map((key) => [key, planOf(name, key, input.schema.fields[key], members[key], overrides[key], options.messages ?? {})]));
|
|
206
|
-
const
|
|
207
|
-
const nestedPlans = Object.fromEntries(rowsKeys.map((key) => [key, nestedPlanOf(name, key, input.schema.fields[key], members[key], {
|
|
313
|
+
const givenForms = options.nested ?? {};
|
|
314
|
+
const nestedPlans = Object.fromEntries(rowsKeys.map((key) => [key, nestedPlanOf(name, key, input.schema.fields[key], members[key], givenForms[key], {
|
|
208
315
|
messages: options.messages,
|
|
209
|
-
debounce: options.debounce
|
|
210
|
-
...nestedOptions[key]
|
|
316
|
+
debounce: options.debounce
|
|
211
317
|
})]));
|
|
212
318
|
const draftSchema = {
|
|
213
319
|
text: Schema.String,
|
|
@@ -221,8 +327,11 @@ const Form = {
|
|
|
221
327
|
model: nestedPlans[key].form.bundle.Model
|
|
222
328
|
}))]))),
|
|
223
329
|
nextRow: Schema.Number,
|
|
330
|
+
searches: Schema.Record(Schema.String, Schema.String),
|
|
331
|
+
touched: Schema.Record(Schema.String, Schema.Boolean),
|
|
224
332
|
errors: Schema.Array(Schema.String),
|
|
225
|
-
submitPending: Schema.Boolean
|
|
333
|
+
submitPending: Schema.Boolean,
|
|
334
|
+
subject: Schema.Record(Schema.String, Schema.String)
|
|
226
335
|
});
|
|
227
336
|
const KeySchema = keys.length === 0 ? Schema.Never : Schema.Literals(keys);
|
|
228
337
|
const DraftSchema = Schema.Union([
|
|
@@ -245,6 +354,28 @@ const Form = {
|
|
|
245
354
|
draft: DraftSchema,
|
|
246
355
|
error: Schema.NullOr(Schema.String)
|
|
247
356
|
},
|
|
357
|
+
/**
|
|
358
|
+
* The outside world refused this key's value: a unique index, a rule only a
|
|
359
|
+
* server knows. The key reads invalid with that reason, keeping what was
|
|
360
|
+
* typed, until it is edited again. Unlike `Checked`, it answers no question
|
|
361
|
+
* the form asked, so it lands whatever state the key is in.
|
|
362
|
+
*/
|
|
363
|
+
Refused: {
|
|
364
|
+
key: KeySchema,
|
|
365
|
+
error: Schema.String
|
|
366
|
+
},
|
|
367
|
+
/**
|
|
368
|
+
* What the form is editing, for the checks that need to know: a post's own
|
|
369
|
+
* row id, so "is this address taken?" can pass over the post's own address.
|
|
370
|
+
* It changes no draft and validates nothing; a form that creates something
|
|
371
|
+
* never sends it. See `FormModel.subject`.
|
|
372
|
+
*/
|
|
373
|
+
About: { subject: Schema.Record(Schema.String, Schema.String) },
|
|
374
|
+
/** What was typed to find a choice for a relation key. It changes no draft and validates nothing. */
|
|
375
|
+
Searched: {
|
|
376
|
+
key: KeySchema,
|
|
377
|
+
text: Schema.String
|
|
378
|
+
},
|
|
248
379
|
/** A Message of the nested form in one row of a nested key. One that row does not take is dropped. */
|
|
249
380
|
Nested: {
|
|
250
381
|
key: Schema.String,
|
|
@@ -280,8 +411,11 @@ const Form = {
|
|
|
280
411
|
fields: fieldsFrom((plan) => FieldValidation.NotValidated({ value: plan.empty })),
|
|
281
412
|
rows: {},
|
|
282
413
|
nextRow: 0,
|
|
414
|
+
searches: {},
|
|
415
|
+
touched: {},
|
|
283
416
|
errors: [],
|
|
284
|
-
submitPending: false
|
|
417
|
+
submitPending: false,
|
|
418
|
+
subject: {}
|
|
285
419
|
});
|
|
286
420
|
const drafts = (model) => model.fields;
|
|
287
421
|
const withField = (model, key, field) => ({
|
|
@@ -341,7 +475,10 @@ const Form = {
|
|
|
341
475
|
commands: [{
|
|
342
476
|
name: `${name}.check`,
|
|
343
477
|
args: { key },
|
|
344
|
-
effect: Effect.sleep(debounce).pipe(Effect.andThen(check(value, {
|
|
478
|
+
effect: Effect.sleep(debounce).pipe(Effect.andThen(check(value, {
|
|
479
|
+
values: decoded(asking),
|
|
480
|
+
subject: model.subject
|
|
481
|
+
})), Effect.map((error) => Message.Checked({
|
|
345
482
|
key,
|
|
346
483
|
draft,
|
|
347
484
|
error: error ?? null
|
|
@@ -349,6 +486,64 @@ const Form = {
|
|
|
349
486
|
}]
|
|
350
487
|
};
|
|
351
488
|
};
|
|
489
|
+
const followers = keys.filter((key) => plans[key].control.follows !== void 0);
|
|
490
|
+
const followedBy = (key) => followers.filter((follower) => plans[follower].control.follows.key === key);
|
|
491
|
+
for (const follower of followers) {
|
|
492
|
+
const source = plans[follower].control.follows.key;
|
|
493
|
+
if (plans[source] === void 0 || plans[source].kind !== "text" || source === follower) fail(name, `"${follower}" follows "${source}", which is not another text key of the form`);
|
|
494
|
+
const seen = /* @__PURE__ */ new Set([follower]);
|
|
495
|
+
for (let next = source; next !== void 0;) {
|
|
496
|
+
if (seen.has(next)) fail(name, `"${follower}" follows itself, through "${next}"`);
|
|
497
|
+
seen.add(next);
|
|
498
|
+
next = plans[next].control.follows?.key;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
/** What a follower's draft is, from the key it follows. */
|
|
502
|
+
const derived = (model, follower) => {
|
|
503
|
+
const { key, through } = plans[follower].control.follows;
|
|
504
|
+
return through(String(drafts(model)[key].value));
|
|
505
|
+
};
|
|
506
|
+
/**
|
|
507
|
+
* An author's edit of one key, and what follows from it. The key becomes theirs,
|
|
508
|
+
* unless they emptied a key that follows another, which hands it back. Every
|
|
509
|
+
* key still following this one is rewritten, and so on down.
|
|
510
|
+
*/
|
|
511
|
+
const changed = (model, key, draft) => {
|
|
512
|
+
const follows = plans[key].control.follows !== void 0;
|
|
513
|
+
const handedBack = follows && isEmpty(draft);
|
|
514
|
+
const owned = follows ? {
|
|
515
|
+
...model,
|
|
516
|
+
touched: {
|
|
517
|
+
...model.touched,
|
|
518
|
+
[key]: !handedBack
|
|
519
|
+
}
|
|
520
|
+
} : model;
|
|
521
|
+
const written = validateKey(owned, key, handedBack ? derived(owned, key) : draft);
|
|
522
|
+
return followedBy(key).reduce((result, follower) => {
|
|
523
|
+
if (result.model.touched[follower] === true) return result;
|
|
524
|
+
const next = rewritten(result.model, follower);
|
|
525
|
+
return {
|
|
526
|
+
model: next.model,
|
|
527
|
+
commands: [...result.commands, ...next.commands]
|
|
528
|
+
};
|
|
529
|
+
}, written);
|
|
530
|
+
};
|
|
531
|
+
/** A follower written from the key it follows, and its own followers after it. */
|
|
532
|
+
const rewritten = (model, follower) => {
|
|
533
|
+
const draft = derived(model, follower);
|
|
534
|
+
const written = isEmpty(draft) ? {
|
|
535
|
+
model: withField(model, follower, FieldValidation.NotValidated({ value: draft })),
|
|
536
|
+
commands: []
|
|
537
|
+
} : validateKey(model, follower, draft);
|
|
538
|
+
return followedBy(follower).reduce((result, next) => {
|
|
539
|
+
if (result.model.touched[next] === true) return result;
|
|
540
|
+
const after = rewritten(result.model, next);
|
|
541
|
+
return {
|
|
542
|
+
model: after.model,
|
|
543
|
+
commands: [...result.commands, ...after.commands]
|
|
544
|
+
};
|
|
545
|
+
}, written);
|
|
546
|
+
};
|
|
352
547
|
const decodeInput = Schema.decodeUnknownResult(Schema.toType(input.schema));
|
|
353
548
|
const acceptable = (model) => keys.every((key) => FieldValidation.isValid(plans[key].rules)(drafts(model)[key]));
|
|
354
549
|
/** A check is running, in this form or in a row of it. */
|
|
@@ -374,7 +569,7 @@ const Form = {
|
|
|
374
569
|
}),
|
|
375
570
|
onFailure: (error) => ({ model: {
|
|
376
571
|
...settled,
|
|
377
|
-
errors: [options.messages?.form
|
|
572
|
+
errors: [worded(options.messages?.form, [error.message], { message: error.message }) ?? error.message]
|
|
378
573
|
} })
|
|
379
574
|
});
|
|
380
575
|
};
|
|
@@ -455,6 +650,8 @@ const Form = {
|
|
|
455
650
|
const given = values;
|
|
456
651
|
const filled = {
|
|
457
652
|
...model,
|
|
653
|
+
searches: {},
|
|
654
|
+
touched: Object.fromEntries(followers.flatMap((key) => key in given ? isEmpty(draftOf(plans[key], given[key])) ? [] : [[key, true]] : model.touched[key] === true ? [[key, true]] : [])),
|
|
458
655
|
errors: [],
|
|
459
656
|
submitPending: false,
|
|
460
657
|
fields: fieldsFrom((plan) => plan.key in given ? FieldValidation.NotValidated({ value: draftOf(plan, given[plan.key]) }) : drafts(model)[plan.key])
|
|
@@ -468,17 +665,39 @@ const Form = {
|
|
|
468
665
|
}, filled) };
|
|
469
666
|
};
|
|
470
667
|
return {
|
|
668
|
+
/** The name, input and options the form was made from: what a pipe step makes the next form from. */
|
|
669
|
+
name,
|
|
670
|
+
options,
|
|
671
|
+
/** Type-only: the form's type parameters, for a pipe step to read. Never set. */
|
|
672
|
+
types: void 0,
|
|
673
|
+
pipe() {
|
|
674
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
675
|
+
},
|
|
471
676
|
bundle: Bundle.make(name, {
|
|
472
677
|
Model,
|
|
473
678
|
Message,
|
|
474
679
|
init: () => ({ model: initial }),
|
|
475
680
|
update: (model, message) => {
|
|
476
681
|
switch (message._tag) {
|
|
477
|
-
case "Changed": return kindOf(message.value) !== plans[message.key].kind ? { model } :
|
|
682
|
+
case "Changed": return kindOf(message.value) !== plans[message.key].kind ? { model } : changed(edited(model), message.key, message.value);
|
|
478
683
|
case "Blurred": {
|
|
479
684
|
const state = drafts(model)[message.key];
|
|
480
685
|
return state._tag === "NotValidated" ? validateKey(model, message.key, state.value) : { model };
|
|
481
686
|
}
|
|
687
|
+
case "Refused": {
|
|
688
|
+
const state = drafts(model)[message.key];
|
|
689
|
+
return { model: {
|
|
690
|
+
...withField(model, message.key, FieldValidation.Invalid({
|
|
691
|
+
value: state.value,
|
|
692
|
+
errors: [message.error]
|
|
693
|
+
})),
|
|
694
|
+
submitPending: false
|
|
695
|
+
} };
|
|
696
|
+
}
|
|
697
|
+
case "About": return { model: {
|
|
698
|
+
...model,
|
|
699
|
+
subject: message.subject
|
|
700
|
+
} };
|
|
482
701
|
case "Checked": {
|
|
483
702
|
const state = drafts(model)[message.key];
|
|
484
703
|
if (state._tag !== "Validating" || !sameDraft(state.value, message.draft)) return { model };
|
|
@@ -487,6 +706,13 @@ const Form = {
|
|
|
487
706
|
errors: [message.error]
|
|
488
707
|
})));
|
|
489
708
|
}
|
|
709
|
+
case "Searched": return plans[message.key].control.searches ? { model: {
|
|
710
|
+
...model,
|
|
711
|
+
searches: {
|
|
712
|
+
...model.searches,
|
|
713
|
+
[message.key]: message.text
|
|
714
|
+
}
|
|
715
|
+
} } : { model };
|
|
490
716
|
case "Nested": {
|
|
491
717
|
const plan = nestedPlans[message.key];
|
|
492
718
|
const row = rowsOf(model)[message.key]?.find((held) => held.id === message.row);
|
|
@@ -516,7 +742,10 @@ const Form = {
|
|
|
516
742
|
if (plan.cardinality === "one" && !plan.optional) return { model };
|
|
517
743
|
return { model: withRows(edited(model), message.key, rows.filter((held) => held.id !== message.row)) };
|
|
518
744
|
}
|
|
519
|
-
case "Reset": return { model:
|
|
745
|
+
case "Reset": return { model: {
|
|
746
|
+
...initial,
|
|
747
|
+
subject: model.subject
|
|
748
|
+
} };
|
|
520
749
|
case "Submitted": return submitted(model);
|
|
521
750
|
}
|
|
522
751
|
},
|
|
@@ -547,6 +776,44 @@ const Form = {
|
|
|
547
776
|
* nested key has rows and no draft.
|
|
548
777
|
*/
|
|
549
778
|
field: (model, key) => drafts(model)[key] ?? fail(name, `"${key}" holds rows, not a draft; read it with rows`),
|
|
779
|
+
/** What was typed to find a choice for a relation key that searches; `''` until something is. */
|
|
780
|
+
search: (model, key) => model.searches[key] ?? "",
|
|
781
|
+
/** What the form is editing; `{}` while it creates. See `FormModel.subject`. */
|
|
782
|
+
subject: (model) => model.subject,
|
|
783
|
+
/**
|
|
784
|
+
* The form of each nested key, typed when it was given under `nested`: its
|
|
785
|
+
* `controls`, its `field`, its Messages.
|
|
786
|
+
*/
|
|
787
|
+
nested: Object.fromEntries(rowsKeys.map((key) => [key, nestedPlans[key].form])),
|
|
788
|
+
/**
|
|
789
|
+
* One row of a nested key, addressed: `form.row('author', id).Changed({ key: 'name', value })`
|
|
790
|
+
* is this form's Message for that edit in that row.
|
|
791
|
+
*/
|
|
792
|
+
row: (key, row) => {
|
|
793
|
+
const make = (nestedPlans[key] ?? fail(name, `"${key}" holds a draft, not rows`)).form.Message;
|
|
794
|
+
const send = (message) => Message.Nested({
|
|
795
|
+
key,
|
|
796
|
+
row,
|
|
797
|
+
message
|
|
798
|
+
});
|
|
799
|
+
return {
|
|
800
|
+
send,
|
|
801
|
+
...Object.fromEntries([
|
|
802
|
+
"Changed",
|
|
803
|
+
"Blurred",
|
|
804
|
+
"Searched",
|
|
805
|
+
"Submitted",
|
|
806
|
+
"Reset",
|
|
807
|
+
"RowAdded",
|
|
808
|
+
"RowRemoved"
|
|
809
|
+
].map((constructor) => [constructor, (...args) => send(make[constructor](...args))]))
|
|
810
|
+
};
|
|
811
|
+
},
|
|
812
|
+
/**
|
|
813
|
+
* Whether a key that follows another still does: `false` once the author has
|
|
814
|
+
* written it themselves. A view offers "regenerate" by sending the key an empty draft.
|
|
815
|
+
*/
|
|
816
|
+
isFollowing: (model, key) => plans[key].control.follows !== void 0 && model.touched[key] !== true,
|
|
550
817
|
/** The rows of a nested key, each a Model of `control.form`. */
|
|
551
818
|
rows: (model, key) => rowsOf(model)[key] ?? fail(name, `"${key}" holds a draft, not rows`),
|
|
552
819
|
/**
|
|
@@ -557,6 +824,37 @@ const Form = {
|
|
|
557
824
|
const next = submit(model);
|
|
558
825
|
return next.value !== void 0 || next.model.submitPending;
|
|
559
826
|
},
|
|
827
|
+
/**
|
|
828
|
+
* The Model with nothing in flight, for one that was stored and is shown
|
|
829
|
+
* again: a check that was running when it was stored will never answer, so
|
|
830
|
+
* its key is not validated yet, and no submit is waiting. Rows too.
|
|
831
|
+
*/
|
|
832
|
+
settled: (model) => ({
|
|
833
|
+
...model,
|
|
834
|
+
submitPending: false,
|
|
835
|
+
fields: fieldsFrom((plan) => {
|
|
836
|
+
const field = drafts(model)[plan.key];
|
|
837
|
+
return field._tag === "Validating" ? FieldValidation.NotValidated({ value: field.value }) : field;
|
|
838
|
+
}),
|
|
839
|
+
rows: Object.fromEntries(rowsKeys.map((key) => [key, rowsOf(model)[key].map((row) => ({
|
|
840
|
+
...row,
|
|
841
|
+
model: nestedPlans[key].form.settled(row.model)
|
|
842
|
+
}))]))
|
|
843
|
+
}),
|
|
844
|
+
/**
|
|
845
|
+
* What of the form decodes as it stands, by key: the value, less every key
|
|
846
|
+
* that is not valid yet. For saving unfinished work, which `engine.value`
|
|
847
|
+
* cannot give. A nested key is there once each of its rows has a value.
|
|
848
|
+
*/
|
|
849
|
+
partial: (model) => ({
|
|
850
|
+
...decoded(model),
|
|
851
|
+
...Object.fromEntries(rowsKeys.flatMap((key) => {
|
|
852
|
+
const plan = nestedPlans[key];
|
|
853
|
+
const values = rowsOf(model)[key].map((row) => plan.form.engine.value(row.model));
|
|
854
|
+
if (values.includes(void 0)) return [];
|
|
855
|
+
return [[key, plan.cardinality === "many" ? values : values.length === 0 ? plan.nothing : values[0]]];
|
|
856
|
+
}))
|
|
857
|
+
}),
|
|
560
858
|
/** The form as the form that nests it drives it. */
|
|
561
859
|
engine: {
|
|
562
860
|
submit: (model) => {
|
|
@@ -587,8 +885,49 @@ const Form = {
|
|
|
587
885
|
*/
|
|
588
886
|
labelOf: (member) => labelKey.get(member.metadata)[0]?.label
|
|
589
887
|
};
|
|
590
|
-
|
|
888
|
+
/** The form again, with some of its options replaced or added to. */
|
|
889
|
+
const remake = (form, change) => Core.make(form.name, form.input, {
|
|
890
|
+
...form.options,
|
|
891
|
+
...change(form.options)
|
|
892
|
+
});
|
|
893
|
+
/**
|
|
894
|
+
* Pipe steps. Each gives a new form, made from the same input with one option
|
|
895
|
+
* added to, so a form can be handed over partly configured and finished where it
|
|
896
|
+
* is used: `AuthorForm.pipe(Form.inputs({ bio: Input.multiline() }), Form.messages(german))`.
|
|
897
|
+
*/
|
|
898
|
+
const steps = {
|
|
899
|
+
/** Controls by key, beside the ones the form already names. */
|
|
900
|
+
inputs: (inputs) => (form) => remake(form, (options) => ({ inputs: {
|
|
901
|
+
...options.inputs,
|
|
902
|
+
...inputs
|
|
903
|
+
} })),
|
|
904
|
+
/** The form's words, beside the ones it already has. */
|
|
905
|
+
messages: (messages) => (form) => remake(form, (options) => ({ messages: {
|
|
906
|
+
...options.messages,
|
|
907
|
+
...messages
|
|
908
|
+
} })),
|
|
909
|
+
/** How long a key rests before its check runs. */
|
|
910
|
+
debounce: (debounce) => (form) => remake(form, () => ({ debounce })),
|
|
911
|
+
/**
|
|
912
|
+
* Checks by key, beside the ones the form already has. What they need (`R`) is
|
|
913
|
+
* added to what the form needs.
|
|
914
|
+
*/
|
|
915
|
+
checks: (checks) => (form) => remake(form, (options) => ({ checks: {
|
|
916
|
+
...options.checks,
|
|
917
|
+
...checks
|
|
918
|
+
} })),
|
|
919
|
+
/** The forms of nested keys, beside the ones the form was already given. */
|
|
920
|
+
nested: (forms) => (form) => remake(form, (options) => ({ nested: {
|
|
921
|
+
...options.nested,
|
|
922
|
+
...forms
|
|
923
|
+
} }))
|
|
924
|
+
};
|
|
925
|
+
const Form = {
|
|
926
|
+
...Core,
|
|
927
|
+
...steps
|
|
928
|
+
};
|
|
929
|
+
makeNested = Core.make;
|
|
591
930
|
//#endregion
|
|
592
|
-
export { Form, Input };
|
|
931
|
+
export { Form, Input, fillWords };
|
|
593
932
|
|
|
594
933
|
//# sourceMappingURL=index.mjs.map
|