@opentf/afw-form 1.28.0 → 1.30.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 (3) hide show
  1. package/README.md +4 -3
  2. package/index.js +27 -12
  3. package/package.json +4 -4
package/README.md CHANGED
@@ -123,15 +123,16 @@ const form = createForm({ initialValues: { username: "", email: "" }, validator:
123
123
 
124
124
  ## API
125
125
 
126
- `createForm(options)` returns a stable form object; called again from the same
127
- component, it returns the same instance.
126
+ `createForm(options)` returns a stable form object. When the same component instance
127
+ remounts, each `createForm` call gets its form back, matched by `key` or else by call
128
+ order in the component body. Several forms in one component stay independent.
128
129
 
129
130
  | Option | Purpose |
130
131
  | --- | --- |
131
132
  | `initialValues` | Starting values; also the baseline for `isChanged` |
132
133
  | `validate` / `validator` | Validation function, as above |
133
134
  | `mode` / `reValidateMode` | Validation timing, as above |
134
- | `key` | Distinguishes several forms created in one component |
135
+ | `key` | Names the form within its component, instead of its call order |
135
136
 
136
137
  | Member | Purpose |
137
138
  | --- | --- |
package/index.js CHANGED
@@ -33,10 +33,24 @@ export function createForm(options = {}) {
33
33
  const inst = getCurrentInstance();
34
34
  if (!inst) return _createForm(options);
35
35
 
36
- let forms = INST_FORMS_REGISTRY.get(inst);
37
- if (!forms) INST_FORMS_REGISTRY.set(inst, (forms = new Map()));
38
-
39
- const key = options.key || JSON.stringify({ initialValues: options.initialValues });
36
+ let entry = INST_FORMS_REGISTRY.get(inst);
37
+ if (!entry) INST_FORMS_REGISTRY.set(inst, (entry = { forms: new Map(), calls: 0, counting: false }));
38
+ const { forms } = entry;
39
+
40
+ // Without a `key`, a form is identified by its call position in the component
41
+ // body: the first `createForm` is `#0`, the next `#1`, … The count restarts once
42
+ // the body finishes (a microtask later), so a remount of the same host gets the
43
+ // same forms back, while two forms in one body never share state — even with
44
+ // identical (or no) initial values.
45
+ if (!entry.counting) {
46
+ entry.counting = true;
47
+ queueMicrotask(() => {
48
+ entry.calls = 0;
49
+ entry.counting = false;
50
+ });
51
+ }
52
+ const position = entry.calls++;
53
+ const key = options.key != null ? `key:${options.key}` : `#${position}`;
40
54
  if (forms.has(key)) {
41
55
  const form = forms.get(key);
42
56
  form._updateConfig?.(options.mode, options.reValidateMode);
@@ -193,8 +207,9 @@ function _createForm(options = {}) {
193
207
 
194
208
  const register = (path) => ({
195
209
  name: path,
196
- value: readPath(valuesStore, path),
197
- checked: readPath(valuesStore, path),
210
+ // A field with no initial value renders empty, not the text "undefined".
211
+ value: readPath(valuesStore, path) ?? "",
212
+ checked: readPath(valuesStore, path) ?? false,
198
213
  error: computed(() => readPath(errorsStore, path)),
199
214
  isTouched: computed(() => readPath(touchedStore, path) ?? false),
200
215
  oninput: (e) => {
@@ -220,15 +235,15 @@ function _createForm(options = {}) {
220
235
  syncStore(touchedStore, mirrorLeaves(snapshot(valuesStore), true));
221
236
  isSubmittingSig.value = true;
222
237
  submitCountSig.value++;
223
- await runValidation();
224
- if (!hasTruthy(toRawValue(errorsStore))) {
225
- try {
238
+ // `finally` covers a validator that throws or rejects as well as `fn`, so a
239
+ // failed submit never leaves the form stuck in `isSubmitting`.
240
+ try {
241
+ await runValidation();
242
+ if (!hasTruthy(toRawValue(errorsStore))) {
226
243
  await fn(snapshot(valuesStore));
227
244
  isSubmittedSig.value = true;
228
- } finally {
229
- isSubmittingSig.value = false;
230
245
  }
231
- } else {
246
+ } finally {
232
247
  isSubmittingSig.value = false;
233
248
  }
234
249
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentf/afw-form",
3
- "version": "1.28.0",
3
+ "version": "1.30.0",
4
4
  "description": "Path-based reactive forms for AFW.",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
@@ -34,13 +34,13 @@
34
34
  "@opentf/std": "^0.19.0"
35
35
  },
36
36
  "peerDependencies": {
37
- "@opentf/afw": "0.33.0"
37
+ "@opentf/afw": "0.35.0"
38
38
  },
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  },
42
42
  "devDependencies": {
43
- "@opentf/afw": "0.33.0",
44
- "@opentf/afw-test": "1.28.0"
43
+ "@opentf/afw": "0.35.0",
44
+ "@opentf/afw-test": "1.30.0"
45
45
  }
46
46
  }