foldkit-form 0.7.0 → 0.8.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/README.md +19 -1
- package/dist/index.d.mts +172 -6
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +92 -29
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { Metadata } from "foldkit-metadata";
|
|
|
5
5
|
import { define, mapMessages } from "foldkit/command";
|
|
6
6
|
import * as FieldValidation from "foldkit/fieldValidation";
|
|
7
7
|
import { defineMessageUnion } from "foldkit/message";
|
|
8
|
-
|
|
8
|
+
// src/input.ts
|
|
9
9
|
/**
|
|
10
10
|
* Controls: what edits one key of a form, described without a renderer.
|
|
11
11
|
*
|
|
@@ -134,19 +134,23 @@ const Input = {
|
|
|
134
134
|
* Messages travel as the form's `Control` Message, and its Commands,
|
|
135
135
|
* Subscriptions and Resources become the form's.
|
|
136
136
|
*/
|
|
137
|
-
bundle: (kind, spec) =>
|
|
138
|
-
kind,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
137
|
+
bundle: (kind, spec) => {
|
|
138
|
+
if (spec.busy !== void 0 && spec.settled === void 0) throw new Error(`Input.bundle("${kind}"): give busy a settled too, or a stored form shown again waits for good`);
|
|
139
|
+
return Object.freeze({
|
|
140
|
+
kind,
|
|
141
|
+
draft: "model",
|
|
142
|
+
shown: true,
|
|
143
|
+
searches: false,
|
|
144
|
+
data: Object.freeze({
|
|
145
|
+
bundle: spec.bundle,
|
|
146
|
+
args: spec.args,
|
|
147
|
+
value: spec.value,
|
|
148
|
+
fill: spec.fill,
|
|
149
|
+
settled: spec.settled ?? ((model) => model),
|
|
150
|
+
busy: spec.busy ?? (() => false)
|
|
151
|
+
})
|
|
152
|
+
});
|
|
153
|
+
},
|
|
150
154
|
/** Whether a control is backed by a Bundle, whatever its kind is called. */
|
|
151
155
|
isBundle: (control) => control.draft === "model",
|
|
152
156
|
text: () => Text.of(nothing),
|
|
@@ -238,8 +242,7 @@ const Input = {
|
|
|
238
242
|
}
|
|
239
243
|
}
|
|
240
244
|
};
|
|
241
|
-
|
|
242
|
-
//#region src/index.ts
|
|
245
|
+
// src/index.ts
|
|
243
246
|
/**
|
|
244
247
|
* `foldkit-form` — a form as a Bundle over Foldkit's own field validation.
|
|
245
248
|
*
|
|
@@ -261,6 +264,15 @@ const labelKey = Metadata.key("foldkit-form/label", {
|
|
|
261
264
|
merge: (labels) => labels.slice(-1),
|
|
262
265
|
summarize: (label) => label.label
|
|
263
266
|
});
|
|
267
|
+
/**
|
|
268
|
+
* A draft after a press: a flag flipped, or a list with `choice` let go if it is
|
|
269
|
+
* held and added if not. None when the press does not fit the draft's kind.
|
|
270
|
+
*/
|
|
271
|
+
const toggled = (kind, draft, choice) => {
|
|
272
|
+
if (choice === void 0) return kind === "flag" ? Option.some(draft !== true) : Option.none();
|
|
273
|
+
if (kind !== "list" || !Array.isArray(draft)) return Option.none();
|
|
274
|
+
return Option.some(draft.includes(choice) ? draft.filter((held) => held !== choice) : [...draft, choice]);
|
|
275
|
+
};
|
|
264
276
|
/** Assigned once `Form` exists: a form builds the forms of its nested keys with itself. */
|
|
265
277
|
let makeNested;
|
|
266
278
|
/**
|
|
@@ -277,10 +289,13 @@ const edits = {
|
|
|
277
289
|
Refused: false,
|
|
278
290
|
About: false,
|
|
279
291
|
Searched: false,
|
|
292
|
+
Toggled: true,
|
|
280
293
|
Nested: false,
|
|
281
294
|
Control: false,
|
|
282
295
|
RowAdded: true,
|
|
283
|
-
RowRemoved: true
|
|
296
|
+
RowRemoved: true,
|
|
297
|
+
FollowedIssue: false,
|
|
298
|
+
AskedForMore: false
|
|
284
299
|
};
|
|
285
300
|
/** An edit, however deep. */
|
|
286
301
|
const isEdit = (message) => message._tag === "Nested" ? isEdit(message.message) : edits[message._tag];
|
|
@@ -352,7 +367,11 @@ const planOf = (name, key, schema, member, override, messages) => {
|
|
|
352
367
|
const value = bundled.value(draft);
|
|
353
368
|
return value === void 0 || value === null;
|
|
354
369
|
};
|
|
355
|
-
const samples = kind === "flag" ? [true, false] : kind === "list" ? [
|
|
370
|
+
const samples = kind === "flag" ? [true, false] : kind === "list" ? [
|
|
371
|
+
[],
|
|
372
|
+
["id"],
|
|
373
|
+
...Input.Choices.is(control) ? control.data.options.map((_, index) => control.data.options.slice(0, index + 1)) : []
|
|
374
|
+
] : [];
|
|
356
375
|
if (samples.length > 0 && !samples.some(accepts)) fail(name, `"${key}" is edited as ${control.kind}, but its schema accepts no such value`);
|
|
357
376
|
const { label, description } = wordsOf(key, schema, member);
|
|
358
377
|
const field = {
|
|
@@ -459,7 +478,8 @@ const Core = {
|
|
|
459
478
|
const givenForms = options.nested ?? {};
|
|
460
479
|
const nestedPlans = Object.fromEntries(rowsKeys.map((key) => [key, nestedPlanOf(name, key, input.schema.fields[key], members[key], givenForms[key], {
|
|
461
480
|
messages: options.messages,
|
|
462
|
-
debounce: options.debounce
|
|
481
|
+
debounce: options.debounce,
|
|
482
|
+
operators: options.operators
|
|
463
483
|
})]));
|
|
464
484
|
const draftSchema = {
|
|
465
485
|
text: Schema.String,
|
|
@@ -530,6 +550,16 @@ const Core = {
|
|
|
530
550
|
key: KeySchema,
|
|
531
551
|
text: Schema.String
|
|
532
552
|
},
|
|
553
|
+
/**
|
|
554
|
+
* A press on a flag (no `choice`), or on one choice of a list: the key's
|
|
555
|
+
* draft as it stands now, flipped, or with `choice` let go if held and added
|
|
556
|
+
* if not. A view sends this rather than `Changed`, so two presses before the
|
|
557
|
+
* next draw both land.
|
|
558
|
+
*/
|
|
559
|
+
Toggled: {
|
|
560
|
+
key: KeySchema,
|
|
561
|
+
choice: Schema.optional(Schema.String)
|
|
562
|
+
},
|
|
533
563
|
/** A Message of the nested form in one row of a nested key. One that row does not take is dropped. */
|
|
534
564
|
Nested: {
|
|
535
565
|
key: Schema.String,
|
|
@@ -551,7 +581,19 @@ const Core = {
|
|
|
551
581
|
RowRemoved: {
|
|
552
582
|
key: Schema.String,
|
|
553
583
|
row: Schema.String
|
|
554
|
-
}
|
|
584
|
+
},
|
|
585
|
+
/**
|
|
586
|
+
* A link to the field drawn with this id was followed, such as one in a
|
|
587
|
+
* view's list of fields to fix. The view moves focus there itself, in the
|
|
588
|
+
* press; the form changes nothing.
|
|
589
|
+
*/
|
|
590
|
+
FollowedIssue: { id: Schema.String },
|
|
591
|
+
/**
|
|
592
|
+
* A relation picker's reader asked for more choices than the list shows. The
|
|
593
|
+
* list is the page's, so the page answers (`Crud.screen` does, given
|
|
594
|
+
* `Crud.options`); the form changes nothing.
|
|
595
|
+
*/
|
|
596
|
+
AskedForMore: { key: KeySchema }
|
|
555
597
|
});
|
|
556
598
|
const fieldsFrom = (draft) => Object.fromEntries(keys.map((key) => [key, draft(plans[key])]));
|
|
557
599
|
const rowsOf = (model) => model.rows;
|
|
@@ -776,7 +818,7 @@ const Core = {
|
|
|
776
818
|
* it: each failure beside the keys its path names that this form sets, or
|
|
777
819
|
* on the whole input when it sets none of them.
|
|
778
820
|
*/
|
|
779
|
-
const judged = (model, value) => Match.value(Entity.judgeInput(input, value)).pipe(Match.tagsExhaustive({
|
|
821
|
+
const judged = (model, value) => Match.value(Entity.judgeInput(input, value, options.operators)).pipe(Match.tagsExhaustive({
|
|
780
822
|
Valid: () => Option.none(),
|
|
781
823
|
Undecided: () => Option.none(),
|
|
782
824
|
Invalid: ({ failures }) => {
|
|
@@ -800,8 +842,10 @@ const Core = {
|
|
|
800
842
|
const field = drafts(model)[key];
|
|
801
843
|
return FieldValidation.isValid(plans[key].rules)(field) && (field._tag !== "NotValidated" || Result.isSuccess(plans[key].check(field.value)));
|
|
802
844
|
});
|
|
803
|
-
/** A
|
|
804
|
-
const
|
|
845
|
+
/** A key's Bundle has work in flight its value waits on (`Input.bundle`'s `busy`). */
|
|
846
|
+
const isBusy = (model, key) => plans[key].bundled?.busy(drafts(model)[key].value) === true;
|
|
847
|
+
/** A check is running, or a Bundle's work, in this form or in a row of it. */
|
|
848
|
+
const isValidating = (model) => keys.some((key) => drafts(model)[key]._tag === "Validating" || isBusy(model, key)) || rowsKeys.some((key) => rowsOf(model)[key].some((row) => nestedPlans[key].form.engine.isValidating(row.model)));
|
|
805
849
|
/** The decoded input, once every key and every row is acceptable; else what is wrong with the whole. */
|
|
806
850
|
const finish = (model) => {
|
|
807
851
|
const settled = {
|
|
@@ -839,6 +883,10 @@ const Core = {
|
|
|
839
883
|
const commands = [];
|
|
840
884
|
for (const key of keys) {
|
|
841
885
|
const state = drafts(next)[key];
|
|
886
|
+
if (isBusy(next, key)) {
|
|
887
|
+
if (state._tag === "NotValidated" || state._tag === "Invalid") next = withField(next, key, FieldValidation.Validating({ value: state.value }));
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
842
890
|
if (state._tag !== "NotValidated") continue;
|
|
843
891
|
const validated = validateKey(next, key, state.value);
|
|
844
892
|
next = validated.model;
|
|
@@ -959,6 +1007,10 @@ const Core = {
|
|
|
959
1007
|
update: (model, message) => {
|
|
960
1008
|
switch (message._tag) {
|
|
961
1009
|
case "Changed": return kindOf(message.value) !== plans[message.key].kind ? { model } : changed(edited(model), message.key, message.value);
|
|
1010
|
+
case "Toggled": return Option.match(toggled(plans[message.key].kind, drafts(model)[message.key].value, message.choice), {
|
|
1011
|
+
onNone: () => ({ model }),
|
|
1012
|
+
onSome: (value) => changed(edited(model), message.key, value)
|
|
1013
|
+
});
|
|
962
1014
|
case "Blurred": {
|
|
963
1015
|
const state = drafts(model)[message.key];
|
|
964
1016
|
return state._tag === "NotValidated" ? validateKey(model, message.key, state.value) : { model };
|
|
@@ -1002,12 +1054,17 @@ const Core = {
|
|
|
1002
1054
|
const { model: moved, commands = [] } = step.value;
|
|
1003
1055
|
const after = drafts(moved)[message.key].value;
|
|
1004
1056
|
const lifted = commands;
|
|
1005
|
-
if (sameValue(message.key, before, after))
|
|
1006
|
-
model
|
|
1007
|
-
|
|
1008
|
-
|
|
1057
|
+
if (sameValue(message.key, before, after)) {
|
|
1058
|
+
if (!(isBusy(model, message.key) && !isBusy(moved, message.key) && drafts(moved)[message.key]._tag === "Validating")) return resume(moved, lifted);
|
|
1059
|
+
const validated = validateKey(moved, message.key, after);
|
|
1060
|
+
return resume(validated.model, [...lifted, ...validated.commands]);
|
|
1061
|
+
}
|
|
1062
|
+
const waited = model.submitPending && isBusy(model, message.key);
|
|
1009
1063
|
const edit = changed(edited(moved), message.key, after);
|
|
1010
|
-
return {
|
|
1064
|
+
return waited ? resume({
|
|
1065
|
+
...edit.model,
|
|
1066
|
+
submitPending: true
|
|
1067
|
+
}, [...lifted, ...edit.commands]) : {
|
|
1011
1068
|
model: edit.model,
|
|
1012
1069
|
commands: [...lifted, ...edit.commands]
|
|
1013
1070
|
};
|
|
@@ -1026,6 +1083,10 @@ const Core = {
|
|
|
1026
1083
|
model: answered.model
|
|
1027
1084
|
} : held));
|
|
1028
1085
|
const edit = isEdit(inner) || plan.form.engine.authoredChanged(row.model, answered.model);
|
|
1086
|
+
if (model.submitPending && !isEdit(inner) && plan.form.engine.isValidating(row.model)) return resume(next === model ? model : {
|
|
1087
|
+
...edited(next),
|
|
1088
|
+
submitPending: true
|
|
1089
|
+
}, lifted);
|
|
1029
1090
|
return resume(edit ? edited(next) : next, lifted);
|
|
1030
1091
|
}
|
|
1031
1092
|
case "RowAdded": {
|
|
@@ -1048,6 +1109,8 @@ const Core = {
|
|
|
1048
1109
|
subject: model.subject
|
|
1049
1110
|
} };
|
|
1050
1111
|
case "Submitted": return submitted(model);
|
|
1112
|
+
case "FollowedIssue":
|
|
1113
|
+
case "AskedForMore": return { model };
|
|
1051
1114
|
}
|
|
1052
1115
|
},
|
|
1053
1116
|
helpers: { fill },
|
|
@@ -1161,6 +1224,7 @@ const Core = {
|
|
|
1161
1224
|
"Blurred",
|
|
1162
1225
|
"ValidatedAll",
|
|
1163
1226
|
"Searched",
|
|
1227
|
+
"Toggled",
|
|
1164
1228
|
"Submitted",
|
|
1165
1229
|
"Reset",
|
|
1166
1230
|
"RowAdded",
|
|
@@ -1305,7 +1369,6 @@ const Form = {
|
|
|
1305
1369
|
...steps
|
|
1306
1370
|
};
|
|
1307
1371
|
makeNested = Core.make;
|
|
1308
|
-
//#endregion
|
|
1309
1372
|
export { Form, Input, fillWords };
|
|
1310
1373
|
|
|
1311
1374
|
//# sourceMappingURL=index.mjs.map
|