react-f0rm 0.8.0 → 0.9.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/dist/index.umd.js CHANGED
@@ -263,18 +263,21 @@
263
263
  const { emitter, values, deleted } = form;
264
264
  values.set(path.key, value);
265
265
  reviveBranch(deleted, path);
266
+ pruneDirtyBaselines(form, path);
267
+ if (options?.shouldDirty === false) setDirtyBaseline(form, path, value);
266
268
  bumpDirtyVersion(form);
267
269
  if (options?.shouldTouch) setTouchedByPath(form, path);
268
270
  if (options?.shouldValidate) form.validators.get(path.key)?.();
269
271
  emit(emitter, "change", path);
270
272
  }
271
- function changeValue(form, name, value) {
272
- changeValueByPath(form, create$1(name), value);
273
+ function changeValue(form, name, value, options) {
274
+ changeValueByPath(form, create$1(name), value, options);
273
275
  }
274
- function changeValueByPath(form, path, value) {
276
+ function changeValueByPath(form, path, value, options) {
277
+ if (options?.shouldDirty === false) setDirtyBaseline(form, path, value);
275
278
  const change = form.changeHandlers.get(path.key);
276
279
  if (change) change(value);
277
- else setValueByPath(form, path, value);
280
+ else setValueByPath(form, path, value, options);
278
281
  }
279
282
  function getError(form, name) {
280
283
  return getErrorByPath(form, create$1(name));
@@ -302,13 +305,15 @@
302
305
  }
303
306
  function getFieldState(form, name) {
304
307
  const path = create$1(name);
305
- const { initialValues, values, touched, validating } = form;
308
+ const { values, touched, validating } = form;
306
309
  const live = values.get(path.key);
307
310
  return {
308
311
  value: getValueByPath(form, path),
309
312
  error: getErrorByPath(form, path),
310
313
  errors: getFieldErrorsByPath(form, path),
311
- isDirty: values.has(path.key) && get(initialValues, path.value) !== live,
314
+ // Same rule as getDirtyFields, committed baselines included: the field
315
+ // is dirty while its live value differs from its effective baseline.
316
+ isDirty: values.has(path.key) && getDirtyBaseline(form, path.key, path.value) !== live,
312
317
  isTouched: touched.has(path.key),
313
318
  isValidating: validating.has(path.key)
314
319
  };
@@ -391,11 +396,39 @@
391
396
  });
392
397
  return dirty;
393
398
  }
394
- function forEachDirtyField({ initialValues, values }, fn) {
395
- for (const [key, value] of values) {
399
+ function forEachDirtyField(form, fn) {
400
+ for (const [key, value] of form.values) {
396
401
  const path = JSON.parse(key);
397
- if (get(initialValues, path) !== value) fn(path.join("."));
402
+ if (getDirtyBaseline(form, key, path) !== value) fn(path.join("."));
403
+ }
404
+ }
405
+ const dirtyBaselines = /* @__PURE__ */ new WeakMap();
406
+ function getDirtyBaseline(form, key, segments) {
407
+ const baselines = dirtyBaselines.get(form);
408
+ if (baselines?.has(key)) return baselines.get(key);
409
+ return get(form.initialValues, segments);
410
+ }
411
+ function setDirtyBaseline(form, { key }, value) {
412
+ let baselines = dirtyBaselines.get(form);
413
+ if (!baselines) {
414
+ baselines = /* @__PURE__ */ new Map();
415
+ dirtyBaselines.set(form, baselines);
398
416
  }
417
+ baselines.set(key, value);
418
+ }
419
+ function pruneDirtyBaselines(form, { key }) {
420
+ const baselines = dirtyBaselines.get(form);
421
+ if (!baselines?.size) return;
422
+ const stem = `${key.slice(0, -1)},`;
423
+ for (const baselineKey of baselines.keys()) {
424
+ if (baselineKey.startsWith(stem)) baselines.delete(baselineKey);
425
+ }
426
+ }
427
+ function clearDirtyBaselines(form, key) {
428
+ const baselines = dirtyBaselines.get(form);
429
+ if (!baselines) return;
430
+ if (key === void 0) baselines.clear();
431
+ else baselines.delete(key);
399
432
  }
400
433
  const dirtyFieldsCaches = /* @__PURE__ */ new WeakMap();
401
434
  function bumpDirtyVersion(form) {
@@ -444,6 +477,7 @@
444
477
  touched.delete(key);
445
478
  errors.delete(key);
446
479
  validating.delete(key);
480
+ clearDirtyBaselines(form, key);
447
481
  if (!hasLiveBranch(values, segments)) deleted.add(key);
448
482
  bumpDirtyVersion(form);
449
483
  emit(emitter, "change");
@@ -477,6 +511,7 @@
477
511
  form.parsedValues = void 0;
478
512
  form.values.clear();
479
513
  form.deleted.clear();
514
+ clearDirtyBaselines(form);
480
515
  bumpDirtyVersion(form);
481
516
  emit(form.emitter, "change");
482
517
  }
@@ -491,6 +526,7 @@
491
526
  const { emitter, touched, values, deleted, validating } = form;
492
527
  values.clear();
493
528
  deleted.clear();
529
+ clearDirtyBaselines(form);
494
530
  if (!options?.keepTouched) touched.clear();
495
531
  validating.clear();
496
532
  if (!options?.keepIsSubmitting) form.isSubmitting = false;
@@ -512,6 +548,7 @@
512
548
  const path = create$1(name);
513
549
  const { emitter, values, touched, errors, deleted } = form;
514
550
  values.delete(path.key);
551
+ clearDirtyBaselines(form, path.key);
515
552
  if (form.parsedValues !== void 0) {
516
553
  form.parsedValues = unset(form.parsedValues, path.value);
517
554
  const initial = get(form.initialValues, path.value);