@conform-to/dom 1.1.0-pre.0 → 1.1.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 CHANGED
@@ -8,7 +8,7 @@
8
8
  ╚══════╝ ╚═════╝ ╚═╝ ╚══╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝
9
9
 
10
10
 
11
- Version 1.1.0-pre.0 / License MIT / Copyright (c) 2024 Edmund Hung
11
+ Version 1.1.0 / License MIT / Copyright (c) 2024 Edmund Hung
12
12
 
13
13
  A type-safe form validation library utilizing web fundamentals to progressively enhance HTML Forms with full support for server frameworks like Remix and Next.js.
14
14
 
package/form.d.ts CHANGED
@@ -33,6 +33,7 @@ export type Constraint = {
33
33
  pattern?: string;
34
34
  };
35
35
  export type FormMeta<FormError> = {
36
+ formId: string;
36
37
  isValueUpdated: boolean;
37
38
  submissionStatus?: 'error' | 'success';
38
39
  defaultValue: Record<string, unknown>;
@@ -43,7 +44,7 @@ export type FormMeta<FormError> = {
43
44
  key: Record<string, string | undefined>;
44
45
  validated: Record<string, boolean>;
45
46
  };
46
- export type FormState<FormError> = Omit<FormMeta<FormError>, 'isValueUpdated'> & {
47
+ export type FormState<FormError> = Omit<FormMeta<FormError>, 'formId' | 'isValueUpdated'> & {
47
48
  valid: Record<string, boolean>;
48
49
  dirty: Record<string, boolean>;
49
50
  };
@@ -95,6 +96,7 @@ export type FormOptions<Schema, FormError = string[], FormValue = Schema> = {
95
96
  export type SubscriptionSubject = {
96
97
  [key in 'error' | 'initialValue' | 'value' | 'key' | 'valid' | 'dirty']?: SubscriptionScope;
97
98
  } & {
99
+ formId?: boolean;
98
100
  status?: boolean;
99
101
  };
100
102
  export type SubscriptionScope = {
@@ -108,7 +110,7 @@ export type ControlButtonProps = {
108
110
  formNoValidate: boolean;
109
111
  };
110
112
  export type FormContext<Schema extends Record<string, any> = any, FormError = string[], FormValue = Schema> = {
111
- formId: string;
113
+ getFormId(): string;
112
114
  submit(event: SubmitEvent): {
113
115
  formData: FormData;
114
116
  action: ReturnType<typeof getFormAction>;
package/form.js CHANGED
@@ -14,6 +14,7 @@ function createFormMeta(options, initialized) {
14
14
  var defaultValue = options.defaultValue ? submission.serialize(options.defaultValue) : {};
15
15
  var initialValue = (_lastResult$initialVa = lastResult === null || lastResult === void 0 ? void 0 : lastResult.initialValue) !== null && _lastResult$initialVa !== void 0 ? _lastResult$initialVa : defaultValue;
16
16
  var result = {
17
+ formId: options.formId,
17
18
  isValueUpdated: false,
18
19
  submissionStatus: lastResult === null || lastResult === void 0 ? void 0 : lastResult.status,
19
20
  defaultValue,
@@ -323,7 +324,7 @@ function createFormContext(options) {
323
324
  for (var subscriber of subscribers) {
324
325
  var _subscriber$getSubjec;
325
326
  var subject = (_subscriber$getSubjec = subscriber.getSubject) === null || _subscriber$getSubjec === void 0 ? void 0 : _subscriber$getSubjec.call(subscriber);
326
- if (!subject || subject.status && prevState.submissionStatus !== nextState.submissionStatus || shouldNotify(prevState.error, nextState.error, cache.error, subject.error) || shouldNotify(prevState.initialValue, nextState.initialValue, cache.initialValue, subject.initialValue) || shouldNotify(prevState.key, nextState.key, cache.key, subject.key, (prev, next) => prev !== next) || shouldNotify(prevState.valid, nextState.valid, cache.valid, subject.valid, compareBoolean) || shouldNotify(prevState.dirty, nextState.dirty, cache.dirty, subject.dirty, compareBoolean) || shouldNotify(prevState.value, nextState.value, cache.value, subject.value)) {
327
+ if (!subject || subject.formId && prevMeta.formId !== nextMeta.formId || subject.status && prevState.submissionStatus !== nextState.submissionStatus || shouldNotify(prevState.error, nextState.error, cache.error, subject.error) || shouldNotify(prevState.initialValue, nextState.initialValue, cache.initialValue, subject.initialValue) || shouldNotify(prevState.key, nextState.key, cache.key, subject.key, (prev, next) => prev !== next) || shouldNotify(prevState.valid, nextState.valid, cache.valid, subject.valid, compareBoolean) || shouldNotify(prevState.dirty, nextState.dirty, cache.dirty, subject.dirty, compareBoolean) || shouldNotify(prevState.value, nextState.value, cache.value, subject.value)) {
327
328
  subscriber.callback();
328
329
  }
329
330
  }
@@ -419,18 +420,21 @@ function createFormContext(options) {
419
420
  }
420
421
  });
421
422
  }
423
+ function reset() {
424
+ updateFormMeta(createFormMeta(latestOptions, true));
425
+ }
422
426
  function onReset(event) {
423
427
  var element = getFormElement();
424
428
  if (event.type !== 'reset' || event.target !== element || event.defaultPrevented) {
425
429
  return;
426
430
  }
427
- updateFormMeta(createFormMeta(latestOptions, true));
431
+ reset();
428
432
  }
429
433
  function report(result) {
430
434
  var _result$error, _result$state;
431
435
  var formElement = getFormElement();
432
436
  if (!result.initialValue) {
433
- formElement === null || formElement === void 0 || formElement.reset();
437
+ reset();
434
438
  return;
435
439
  }
436
440
  var error = Object.entries((_result$error = result.error) !== null && _result$error !== void 0 ? _result$error : {}).reduce((result, _ref5) => {
@@ -466,7 +470,7 @@ function createFormContext(options) {
466
470
  // Merge new options with the latest options
467
471
  Object.assign(latestOptions, options);
468
472
  if (latestOptions.formId !== currentFormId) {
469
- updateFormMeta(createFormMeta(latestOptions, true));
473
+ reset();
470
474
  } else if (options.lastResult && options.lastResult !== currentResult) {
471
475
  report(options.lastResult);
472
476
  }
@@ -550,8 +554,8 @@ function createFormContext(options) {
550
554
  };
551
555
  }
552
556
  return {
553
- get formId() {
554
- return latestOptions.formId;
557
+ getFormId() {
558
+ return meta.formId;
555
559
  },
556
560
  submit,
557
561
  onReset,
package/form.mjs CHANGED
@@ -10,6 +10,7 @@ function createFormMeta(options, initialized) {
10
10
  var defaultValue = options.defaultValue ? serialize(options.defaultValue) : {};
11
11
  var initialValue = (_lastResult$initialVa = lastResult === null || lastResult === void 0 ? void 0 : lastResult.initialValue) !== null && _lastResult$initialVa !== void 0 ? _lastResult$initialVa : defaultValue;
12
12
  var result = {
13
+ formId: options.formId,
13
14
  isValueUpdated: false,
14
15
  submissionStatus: lastResult === null || lastResult === void 0 ? void 0 : lastResult.status,
15
16
  defaultValue,
@@ -319,7 +320,7 @@ function createFormContext(options) {
319
320
  for (var subscriber of subscribers) {
320
321
  var _subscriber$getSubjec;
321
322
  var subject = (_subscriber$getSubjec = subscriber.getSubject) === null || _subscriber$getSubjec === void 0 ? void 0 : _subscriber$getSubjec.call(subscriber);
322
- if (!subject || subject.status && prevState.submissionStatus !== nextState.submissionStatus || shouldNotify(prevState.error, nextState.error, cache.error, subject.error) || shouldNotify(prevState.initialValue, nextState.initialValue, cache.initialValue, subject.initialValue) || shouldNotify(prevState.key, nextState.key, cache.key, subject.key, (prev, next) => prev !== next) || shouldNotify(prevState.valid, nextState.valid, cache.valid, subject.valid, compareBoolean) || shouldNotify(prevState.dirty, nextState.dirty, cache.dirty, subject.dirty, compareBoolean) || shouldNotify(prevState.value, nextState.value, cache.value, subject.value)) {
323
+ if (!subject || subject.formId && prevMeta.formId !== nextMeta.formId || subject.status && prevState.submissionStatus !== nextState.submissionStatus || shouldNotify(prevState.error, nextState.error, cache.error, subject.error) || shouldNotify(prevState.initialValue, nextState.initialValue, cache.initialValue, subject.initialValue) || shouldNotify(prevState.key, nextState.key, cache.key, subject.key, (prev, next) => prev !== next) || shouldNotify(prevState.valid, nextState.valid, cache.valid, subject.valid, compareBoolean) || shouldNotify(prevState.dirty, nextState.dirty, cache.dirty, subject.dirty, compareBoolean) || shouldNotify(prevState.value, nextState.value, cache.value, subject.value)) {
323
324
  subscriber.callback();
324
325
  }
325
326
  }
@@ -415,18 +416,21 @@ function createFormContext(options) {
415
416
  }
416
417
  });
417
418
  }
419
+ function reset() {
420
+ updateFormMeta(createFormMeta(latestOptions, true));
421
+ }
418
422
  function onReset(event) {
419
423
  var element = getFormElement();
420
424
  if (event.type !== 'reset' || event.target !== element || event.defaultPrevented) {
421
425
  return;
422
426
  }
423
- updateFormMeta(createFormMeta(latestOptions, true));
427
+ reset();
424
428
  }
425
429
  function report(result) {
426
430
  var _result$error, _result$state;
427
431
  var formElement = getFormElement();
428
432
  if (!result.initialValue) {
429
- formElement === null || formElement === void 0 || formElement.reset();
433
+ reset();
430
434
  return;
431
435
  }
432
436
  var error = Object.entries((_result$error = result.error) !== null && _result$error !== void 0 ? _result$error : {}).reduce((result, _ref5) => {
@@ -462,7 +466,7 @@ function createFormContext(options) {
462
466
  // Merge new options with the latest options
463
467
  Object.assign(latestOptions, options);
464
468
  if (latestOptions.formId !== currentFormId) {
465
- updateFormMeta(createFormMeta(latestOptions, true));
469
+ reset();
466
470
  } else if (options.lastResult && options.lastResult !== currentResult) {
467
471
  report(options.lastResult);
468
472
  }
@@ -546,8 +550,8 @@ function createFormContext(options) {
546
550
  };
547
551
  }
548
552
  return {
549
- get formId() {
550
- return latestOptions.formId;
553
+ getFormId() {
554
+ return meta.formId;
551
555
  },
552
556
  submit,
553
557
  onReset,
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "A set of opinionated helpers built on top of the Constraint Validation API",
4
4
  "homepage": "https://conform.guide",
5
5
  "license": "MIT",
6
- "version": "1.1.0-pre.0",
6
+ "version": "1.1.0",
7
7
  "main": "index.js",
8
8
  "module": "index.mjs",
9
9
  "types": "index.d.ts",