react-f0rm 0.11.0 → 0.11.1

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/dist/index.umd.js CHANGED
@@ -58,6 +58,7 @@
58
58
  pathCache.set(path, value);
59
59
  return value;
60
60
  }
61
+ const isIndex = (segment) => /^-?\d+$/.test(segment);
61
62
  function parsePath(path) {
62
63
  const result = [];
63
64
  let identifier = "";
@@ -90,7 +91,7 @@
90
91
  throw new TypeError(`Unterminated bracket in path: ${path}`);
91
92
  }
92
93
  const content = path.slice(i + 1, close);
93
- result.push(/^-?\d+$/.test(content) ? Number(content) : content);
94
+ result.push(isIndex(content) ? Number(content) : content);
94
95
  i = close;
95
96
  }
96
97
  } else {
@@ -127,9 +128,10 @@
127
128
  function set(values, path, value) {
128
129
  if (!path.length) return value;
129
130
  const [prop, ...props] = path;
130
- if (typeof prop === "number") {
131
+ const index = typeof prop === "number" ? prop : Array.isArray(values) && typeof prop === "string" && isIndex(prop) ? Number(prop) : void 0;
132
+ if (index !== void 0) {
131
133
  const arr = Array.isArray(values) ? values.slice() : [];
132
- arr[prop] = set(arr[prop], props, value);
134
+ arr[index] = set(arr[index], props, value);
133
135
  return arr;
134
136
  }
135
137
  return { ...values, [prop]: set(values && values[prop], props, value) };
@@ -143,7 +145,7 @@
143
145
  const prop = path[i];
144
146
  if (!owned.has(container)) {
145
147
  let copy;
146
- if (typeof prop === "number") {
148
+ if (typeof prop === "number" || Array.isArray(container) && typeof prop === "string" && isIndex(prop)) {
147
149
  copy = Array.isArray(container) ? container.slice() : [];
148
150
  } else {
149
151
  copy = { ...container };
@@ -253,9 +255,16 @@
253
255
  return getValueByPath(form, create$1(name));
254
256
  }
255
257
  function getValueByPath({ initialValues, parsedValues, values, deleted }, path) {
256
- if (values.has(path.key)) return values.get(path.key);
257
- if (deleted.has(path.key)) return void 0;
258
- return get(parsedValues ?? initialValues, path.value);
258
+ const { key, value: segments } = path;
259
+ if (values.has(key)) return values.get(key);
260
+ if (deleted.has(key)) return void 0;
261
+ for (let i = segments.length - 1; i > 0; i--) {
262
+ const ancestorKey = JSON.stringify(segments.slice(0, i));
263
+ if (values.has(ancestorKey)) {
264
+ return get(values.get(ancestorKey), segments.slice(i));
265
+ }
266
+ }
267
+ return get(parsedValues ?? initialValues, segments);
259
268
  }
260
269
  function setValue(form, name, value, options) {
261
270
  setValueByPath(form, create$1(name), value, options);
@@ -263,6 +272,7 @@
263
272
  function setValueByPath(form, path, value, options) {
264
273
  const { emitter, values, deleted } = form;
265
274
  values.set(path.key, value);
275
+ pruneDescendantKeys(values, path);
266
276
  reviveBranch(deleted, path);
267
277
  pruneDirtyBaselines(form, path);
268
278
  if (options?.shouldDirty === false) setDirtyBaseline(form, path, value);
@@ -496,6 +506,13 @@
496
506
  }
497
507
  return false;
498
508
  }
509
+ function pruneDescendantKeys(values, { key }) {
510
+ if (!values.size) return;
511
+ const stem = `${key.slice(0, -1)},`;
512
+ for (const k of values.keys()) {
513
+ if (k.startsWith(stem)) values.delete(k);
514
+ }
515
+ }
499
516
  function reviveBranch(deleted, { key }) {
500
517
  if (!deleted.size) return;
501
518
  for (const tombstone of deleted) {