@conform-to/react 1.1.4 → 1.2.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/context.d.ts CHANGED
@@ -1,72 +1,200 @@
1
- import { type Constraint, type Combine, type FormId, type FieldName, type FormContext as BaseFormContext, type FormValue, type FormState, type Intent, type SubscriptionScope, type SubscriptionSubject, type FormOptions as BaseFormOptions } from '@conform-to/dom';
2
- import { type FormEvent, type ReactElement, type ReactNode, type MutableRefObject } from 'react';
1
+ import {
2
+ type Constraint,
3
+ type Combine,
4
+ type FormId,
5
+ type FieldName,
6
+ type FormContext as BaseFormContext,
7
+ type FormValue,
8
+ type FormState,
9
+ type Intent,
10
+ type SubscriptionScope,
11
+ type SubscriptionSubject,
12
+ type FormOptions as BaseFormOptions,
13
+ } from '@conform-to/dom';
14
+ import {
15
+ type FormEvent,
16
+ type ReactElement,
17
+ type ReactNode,
18
+ type MutableRefObject,
19
+ } from 'react';
3
20
  export type Pretty<T> = {
4
- [K in keyof T]: T[K];
21
+ [K in keyof T]: T[K];
5
22
  } & {};
6
- export type Primitive = string | number | bigint | boolean | Date | File | null | undefined;
7
- export type Metadata<Schema, FormSchema extends Record<string, unknown>, FormError = string[]> = {
8
- key: string | undefined;
9
- id: string;
10
- errorId: string;
11
- descriptionId: string;
12
- name: FieldName<Schema, FormSchema, FormError>;
13
- initialValue: FormValue<Schema>;
14
- value: FormValue<Schema>;
15
- errors: FormError | undefined;
16
- allErrors: Record<string, FormError>;
17
- valid: boolean;
18
- dirty: boolean;
23
+ export type Primitive =
24
+ | string
25
+ | number
26
+ | bigint
27
+ | boolean
28
+ | Date
29
+ | File
30
+ | null
31
+ | undefined;
32
+ export type Metadata<
33
+ Schema,
34
+ FormSchema extends Record<string, unknown>,
35
+ FormError = string[],
36
+ > = {
37
+ key: string | undefined;
38
+ id: string;
39
+ errorId: string;
40
+ descriptionId: string;
41
+ name: FieldName<Schema, FormSchema, FormError>;
42
+ initialValue: FormValue<Schema>;
43
+ value: FormValue<Schema>;
44
+ errors: FormError | undefined;
45
+ allErrors: Record<string, FormError>;
46
+ valid: boolean;
47
+ dirty: boolean;
19
48
  };
20
- export type FormMetadata<Schema extends Record<string, unknown> = Record<string, unknown>, FormError = string[]> = Omit<Metadata<Schema, Schema, FormError>, 'id'> & Pick<FormContext<Schema, FormError>, Intent['type']> & {
21
- id: FormId<Schema, FormError>;
22
- context: Wrapped<FormContext<Schema, FormError>>;
23
- status?: 'success' | 'error';
24
- getFieldset: () => Required<{
25
- [Key in keyof Combine<Schema>]: FieldMetadata<Combine<Schema>[Key], Schema, FormError>;
26
- }>;
27
- onSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
28
- noValidate: boolean;
29
- };
30
- type SubfieldMetadata<Schema, FormSchema extends Record<string, any>, FormError> = [Schema] extends [Primitive] ? {} : [Schema] extends [Array<infer Item> | null | undefined] ? {
31
- getFieldList: () => Array<FieldMetadata<Item, FormSchema, FormError>>;
32
- } : [Schema] extends [Record<string, any> | null | undefined] ? {
33
- getFieldset: () => Required<{
34
- [Key in keyof Combine<Schema>]: FieldMetadata<Combine<Schema>[Key], FormSchema, FormError>;
35
- }>;
36
- } : {};
37
- export type FieldMetadata<Schema = unknown, FormSchema extends Record<string, any> = Record<string, unknown>, FormError = string[]> = Metadata<Schema, FormSchema, FormError> & Constraint & {
38
- formId: FormId<FormSchema, FormError>;
39
- } & SubfieldMetadata<Schema, FormSchema, FormError>;
40
- export declare const Form: import("react").Context<FormContext<any, string[], any>[]>;
49
+ export type FormMetadata<
50
+ Schema extends Record<string, unknown> = Record<string, unknown>,
51
+ FormError = string[],
52
+ > = Omit<Metadata<Schema, Schema, FormError>, 'id'> &
53
+ Pick<FormContext<Schema, FormError>, Intent['type']> & {
54
+ id: FormId<Schema, FormError>;
55
+ context: Wrapped<FormContext<Schema, FormError>>;
56
+ status?: 'success' | 'error';
57
+ getFieldset: () => Required<{
58
+ [Key in keyof Combine<Schema>]: FieldMetadata<
59
+ Combine<Schema>[Key],
60
+ Schema,
61
+ FormError
62
+ >;
63
+ }>;
64
+ onSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
65
+ noValidate: boolean;
66
+ };
67
+ type SubfieldMetadata<
68
+ Schema,
69
+ FormSchema extends Record<string, any>,
70
+ FormError,
71
+ > = [Schema] extends [Primitive]
72
+ ? {}
73
+ : [Schema] extends [Array<infer Item> | null | undefined]
74
+ ? {
75
+ getFieldList: () => Array<FieldMetadata<Item, FormSchema, FormError>>;
76
+ }
77
+ : [Schema] extends [Record<string, any> | null | undefined]
78
+ ? {
79
+ getFieldset: () => Required<{
80
+ [Key in keyof Combine<Schema>]: FieldMetadata<
81
+ Combine<Schema>[Key],
82
+ FormSchema,
83
+ FormError
84
+ >;
85
+ }>;
86
+ }
87
+ : {};
88
+ export type FieldMetadata<
89
+ Schema = unknown,
90
+ FormSchema extends Record<string, any> = Record<string, unknown>,
91
+ FormError = string[],
92
+ > = Metadata<Schema, FormSchema, FormError> &
93
+ Constraint & {
94
+ formId: FormId<FormSchema, FormError>;
95
+ } & SubfieldMetadata<Schema, FormSchema, FormError>;
96
+ export declare const Form: import('react').Context<
97
+ FormContext<any, string[], any>[]
98
+ >;
41
99
  declare const wrappedSymbol: unique symbol;
42
100
  export type Wrapped<Type> = {
43
- [wrappedSymbol]: Type;
101
+ [wrappedSymbol]: Type;
44
102
  };
45
- export declare function getWrappedFormContext(context: Wrapped<FormContext>): FormContext;
46
- export declare function useFormContext<Schema extends Record<string, any>, FormError>(formId?: FormId<Schema, FormError>): FormContext<Schema, FormError, unknown>;
47
- export declare function useFormState<FormError>(form: FormContext<any, FormError>, subjectRef?: MutableRefObject<SubscriptionSubject>): FormState<FormError>;
103
+ export declare function getWrappedFormContext(
104
+ context: Wrapped<FormContext>,
105
+ ): FormContext;
106
+ export declare function useFormContext<
107
+ Schema extends Record<string, any>,
108
+ FormError,
109
+ >(formId?: FormId<Schema, FormError>): FormContext<Schema, FormError, unknown>;
110
+ export declare function useFormState<FormError>(
111
+ form: FormContext<any, FormError>,
112
+ subjectRef?: MutableRefObject<SubscriptionSubject>,
113
+ ): FormState<FormError>;
48
114
  export declare function FormProvider(props: {
49
- context: Wrapped<FormContext<any, any>>;
50
- children: ReactNode;
115
+ context: Wrapped<FormContext<any, any>>;
116
+ children: ReactNode;
51
117
  }): ReactElement;
52
118
  export declare function FormStateInput(props: {
53
- formId?: string;
119
+ formId?: string;
54
120
  }): React.ReactElement;
55
- export declare function useSubjectRef(initialSubject?: SubscriptionSubject): MutableRefObject<SubscriptionSubject>;
56
- export declare function updateSubjectRef(ref: MutableRefObject<SubscriptionSubject>, subject: 'status' | 'formId'): void;
57
- export declare function updateSubjectRef(ref: MutableRefObject<SubscriptionSubject>, subject: Exclude<keyof SubscriptionSubject, 'status' | 'formId'>, scope: keyof SubscriptionScope, name: string): void;
58
- export declare function getMetadata<Schema, FormError, FormSchema extends Record<string, any>>(context: FormContext<FormSchema, FormError, any>, subjectRef: MutableRefObject<SubscriptionSubject>, stateSnapshot: FormState<FormError>, name?: FieldName<Schema, FormSchema, FormError>): Metadata<Schema, FormSchema, FormError>;
59
- export declare function getFieldMetadata<Schema, FormSchema extends Record<string, any>, FormError>(context: FormContext<FormSchema, FormError, any>, subjectRef: MutableRefObject<SubscriptionSubject>, stateSnapshot: FormState<FormError>, prefix?: string, key?: string | number): FieldMetadata<Schema, FormSchema, FormError>;
60
- export declare function getFormMetadata<Schema extends Record<string, any>, FormError = string[], FormValue = Schema>(context: FormContext<Schema, FormError, FormValue>, subjectRef: MutableRefObject<SubscriptionSubject>, stateSnapshot: FormState<FormError>, noValidate: boolean): FormMetadata<Schema, FormError>;
61
- export type FormOptions<Schema extends Record<string, any> = any, FormError = string[], FormValue = Schema> = BaseFormOptions<Schema, FormError, FormValue> & {
62
- /**
63
- * A function to be called before the form is submitted.
64
- */
65
- onSubmit?: (event: FormEvent<HTMLFormElement>, context: ReturnType<BaseFormContext<Schema, FormError, FormValue>['submit']>) => void;
121
+ export declare function useSubjectRef(
122
+ initialSubject?: SubscriptionSubject,
123
+ ): MutableRefObject<SubscriptionSubject>;
124
+ export declare function updateSubjectRef(
125
+ ref: MutableRefObject<SubscriptionSubject>,
126
+ subject: 'status' | 'formId',
127
+ ): void;
128
+ export declare function updateSubjectRef(
129
+ ref: MutableRefObject<SubscriptionSubject>,
130
+ subject: Exclude<keyof SubscriptionSubject, 'status' | 'formId'>,
131
+ scope: keyof SubscriptionScope,
132
+ name: string,
133
+ ): void;
134
+ export declare function getMetadata<
135
+ Schema,
136
+ FormError,
137
+ FormSchema extends Record<string, any>,
138
+ >(
139
+ context: FormContext<FormSchema, FormError, any>,
140
+ subjectRef: MutableRefObject<SubscriptionSubject>,
141
+ stateSnapshot: FormState<FormError>,
142
+ name?: FieldName<Schema, FormSchema, FormError>,
143
+ ): Metadata<Schema, FormSchema, FormError>;
144
+ export declare function getFieldMetadata<
145
+ Schema,
146
+ FormSchema extends Record<string, any>,
147
+ FormError,
148
+ >(
149
+ context: FormContext<FormSchema, FormError, any>,
150
+ subjectRef: MutableRefObject<SubscriptionSubject>,
151
+ stateSnapshot: FormState<FormError>,
152
+ prefix?: string,
153
+ key?: string | number,
154
+ ): FieldMetadata<Schema, FormSchema, FormError>;
155
+ export declare function getFormMetadata<
156
+ Schema extends Record<string, any>,
157
+ FormError = string[],
158
+ FormValue = Schema,
159
+ >(
160
+ context: FormContext<Schema, FormError, FormValue>,
161
+ subjectRef: MutableRefObject<SubscriptionSubject>,
162
+ stateSnapshot: FormState<FormError>,
163
+ noValidate: boolean,
164
+ ): FormMetadata<Schema, FormError>;
165
+ export type FormOptions<
166
+ Schema extends Record<string, any> = any,
167
+ FormError = string[],
168
+ FormValue = Schema,
169
+ > = BaseFormOptions<Schema, FormError, FormValue> & {
170
+ /**
171
+ * A function to be called before the form is submitted.
172
+ */
173
+ onSubmit?: (
174
+ event: FormEvent<HTMLFormElement>,
175
+ context: ReturnType<
176
+ BaseFormContext<Schema, FormError, FormValue>['submit']
177
+ >,
178
+ ) => void;
66
179
  };
67
- export type FormContext<Schema extends Record<string, any> = any, FormError = string[], FormValue = Schema> = Omit<BaseFormContext<Schema, FormError, FormValue>, 'submit' | 'onUpdate'> & {
68
- submit: (event: FormEvent<HTMLFormElement>) => void;
69
- onUpdate: (options: Partial<FormOptions<Schema, FormError, FormValue>>) => void;
180
+ export type FormContext<
181
+ Schema extends Record<string, any> = any,
182
+ FormError = string[],
183
+ FormValue = Schema,
184
+ > = Omit<
185
+ BaseFormContext<Schema, FormError, FormValue>,
186
+ 'submit' | 'onUpdate'
187
+ > & {
188
+ submit: (event: FormEvent<HTMLFormElement>) => void;
189
+ onUpdate: (
190
+ options: Partial<FormOptions<Schema, FormError, FormValue>>,
191
+ ) => void;
70
192
  };
71
- export declare function createFormContext<Schema extends Record<string, any> = any, FormError = string[], FormValue = Schema>(options: FormOptions<Schema, FormError, FormValue>): FormContext<Schema, FormError, FormValue>;
193
+ export declare function createFormContext<
194
+ Schema extends Record<string, any> = any,
195
+ FormError = string[],
196
+ FormValue = Schema,
197
+ >(
198
+ options: FormOptions<Schema, FormError, FormValue>,
199
+ ): FormContext<Schema, FormError, FormValue>;
72
200
  export {};
package/context.js CHANGED
@@ -7,7 +7,6 @@ var dom = require('@conform-to/dom');
7
7
  var react = require('react');
8
8
  var jsxRuntime = require('react/jsx-runtime');
9
9
 
10
- var _excluded = ["onSubmit"];
11
10
  var Form = /*#__PURE__*/react.createContext([]);
12
11
 
13
12
  // To hide the FormContext type from the public API
@@ -220,10 +219,9 @@ function getFormMetadata(context, subjectRef, stateSnapshot, noValidate) {
220
219
  }
221
220
  function createFormContext(options) {
222
221
  var {
223
- onSubmit
224
- } = options,
225
- rest = _rollupPluginBabelHelpers.objectWithoutProperties(options, _excluded);
226
- var context = dom.unstable_createFormContext(rest);
222
+ onSubmit
223
+ } = options;
224
+ var context = dom.unstable_createFormContext(options);
227
225
  return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, context), {}, {
228
226
  submit(event) {
229
227
  var submitEvent = event.nativeEvent;
package/context.mjs CHANGED
@@ -1,9 +1,8 @@
1
- import { objectWithoutProperties as _objectWithoutProperties, objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.mjs';
1
+ import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.mjs';
2
2
  import { STATE, formatPaths, getPaths, unstable_createFormContext, INTENT, isPrefix } from '@conform-to/dom';
3
3
  import { useContext, useMemo, createContext, useCallback, useSyncExternalStore, useRef } from 'react';
4
4
  import { jsx } from 'react/jsx-runtime';
5
5
 
6
- var _excluded = ["onSubmit"];
7
6
  var Form = /*#__PURE__*/createContext([]);
8
7
 
9
8
  // To hide the FormContext type from the public API
@@ -216,10 +215,9 @@ function getFormMetadata(context, subjectRef, stateSnapshot, noValidate) {
216
215
  }
217
216
  function createFormContext(options) {
218
217
  var {
219
- onSubmit
220
- } = options,
221
- rest = _objectWithoutProperties(options, _excluded);
222
- var context = unstable_createFormContext(rest);
218
+ onSubmit
219
+ } = options;
220
+ var context = unstable_createFormContext(options);
223
221
  return _objectSpread2(_objectSpread2({}, context), {}, {
224
222
  submit(event) {
225
223
  var submitEvent = event.nativeEvent;
File without changes
package/helpers.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference types="react" />
2
2
  import type { FormMetadata, FieldMetadata, Metadata, Pretty } from './context';
3
3
  type FormControlProps = {
4
- key: string | undefined;
4
+ key?: string;
5
5
  id: string;
6
6
  name: string;
7
7
  form: string;
package/helpers.js CHANGED
@@ -73,7 +73,6 @@ function getFieldsetProps(metadata, options) {
73
73
  */
74
74
  function getFormControlProps(metadata, options) {
75
75
  return simplify(_rollupPluginBabelHelpers.objectSpread2({
76
- key: metadata.key,
77
76
  required: metadata.required || undefined
78
77
  }, getFieldsetProps(metadata, options)));
79
78
  }
package/helpers.mjs CHANGED
@@ -69,7 +69,6 @@ function getFieldsetProps(metadata, options) {
69
69
  */
70
70
  function getFormControlProps(metadata, options) {
71
71
  return simplify(_objectSpread2({
72
- key: metadata.key,
73
72
  required: metadata.required || undefined
74
73
  }, getFieldsetProps(metadata, options)));
75
74
  }
package/hooks.d.ts CHANGED
@@ -1,34 +1,63 @@
1
1
  import { type FormId, type FieldName } from '@conform-to/dom';
2
2
  import { useEffect } from 'react';
3
- import { type FormMetadata, type FieldMetadata, type Pretty, type FormOptions } from './context';
3
+ import {
4
+ type FormMetadata,
5
+ type FieldMetadata,
6
+ type Pretty,
7
+ type FormOptions,
8
+ } from './context';
4
9
  /**
5
10
  * useLayoutEffect is client-only.
6
11
  * This basically makes it a no-op on server
7
12
  */
8
13
  export declare const useSafeLayoutEffect: typeof useEffect;
9
- export declare function useFormId<Schema extends Record<string, unknown>, FormError>(preferredId?: string): FormId<Schema, FormError>;
14
+ export declare function useFormId<
15
+ Schema extends Record<string, unknown>,
16
+ FormError,
17
+ >(preferredId?: string): FormId<Schema, FormError>;
10
18
  export declare function useNoValidate(defaultNoValidate?: boolean): boolean;
11
- export declare function useForm<Schema extends Record<string, any>, FormValue = Schema, FormError = string[]>(options: Pretty<Omit<FormOptions<Schema, FormError, FormValue>, 'formId'> & {
12
- /**
13
- * The form id. If not provided, a random id will be generated.
14
- */
15
- id?: string;
16
- /**
17
- * Enable constraint validation before the dom is hydated.
18
- *
19
- * Default to `true`.
20
- */
21
- defaultNoValidate?: boolean;
22
- }>): [
23
- FormMetadata<Schema, FormError>,
24
- ReturnType<FormMetadata<Schema, FormError>['getFieldset']>
19
+ export declare function useForm<
20
+ Schema extends Record<string, any>,
21
+ FormValue = Schema,
22
+ FormError = string[],
23
+ >(
24
+ options: Pretty<
25
+ Omit<FormOptions<Schema, FormError, FormValue>, 'formId'> & {
26
+ /**
27
+ * The form id. If not provided, a random id will be generated.
28
+ */
29
+ id?: string;
30
+ /**
31
+ * Enable constraint validation before the dom is hydated.
32
+ *
33
+ * Default to `true`.
34
+ */
35
+ defaultNoValidate?: boolean;
36
+ }
37
+ >,
38
+ ): [
39
+ FormMetadata<Schema, FormError>,
40
+ ReturnType<FormMetadata<Schema, FormError>['getFieldset']>,
25
41
  ];
26
- export declare function useFormMetadata<Schema extends Record<string, any>, FormError = string[]>(formId?: FormId<Schema, FormError>, options?: {
27
- defaultNoValidate?: boolean;
28
- }): FormMetadata<Schema, FormError>;
29
- export declare function useField<FieldSchema, FormSchema extends Record<string, unknown> = Record<string, unknown>, FormError = string[]>(name: FieldName<FieldSchema, FormSchema, FormError>, options?: {
30
- formId?: FormId<FormSchema, FormError>;
31
- }): [
32
- FieldMetadata<FieldSchema, FormSchema, FormError>,
33
- FormMetadata<FormSchema, FormError>
42
+ export declare function useFormMetadata<
43
+ Schema extends Record<string, any>,
44
+ FormError = string[],
45
+ >(
46
+ formId?: FormId<Schema, FormError>,
47
+ options?: {
48
+ defaultNoValidate?: boolean;
49
+ },
50
+ ): FormMetadata<Schema, FormError>;
51
+ export declare function useField<
52
+ FieldSchema,
53
+ FormSchema extends Record<string, unknown> = Record<string, unknown>,
54
+ FormError = string[],
55
+ >(
56
+ name: FieldName<FieldSchema, FormSchema, FormError>,
57
+ options?: {
58
+ formId?: FormId<FormSchema, FormError>;
59
+ },
60
+ ): [
61
+ FieldMetadata<FieldSchema, FormSchema, FormError>,
62
+ FormMetadata<FormSchema, FormError>,
34
63
  ];
package/hooks.js CHANGED
@@ -56,10 +56,61 @@ function useForm(options) {
56
56
  formId
57
57
  }));
58
58
  });
59
- var subjectRef = context.useSubjectRef();
59
+ var subjectRef = context.useSubjectRef({
60
+ key: {
61
+ // Subscribe to all key changes so it will re-render and
62
+ // update the field value as soon as the DOM is updated
63
+ prefix: ['']
64
+ }
65
+ });
60
66
  var stateSnapshot = context.useFormState(context$1, subjectRef);
61
67
  var noValidate = useNoValidate(options.defaultNoValidate);
62
68
  var form = context.getFormMetadata(context$1, subjectRef, stateSnapshot, noValidate);
69
+ react.useEffect(() => {
70
+ var formElement = document.forms.namedItem(formId);
71
+ if (!formElement) {
72
+ return;
73
+ }
74
+ var getAll = value => {
75
+ if (typeof value === 'string') {
76
+ return [value];
77
+ }
78
+ if (Array.isArray(value) && value.every(item => typeof item === 'string')) {
79
+ return value;
80
+ }
81
+ return undefined;
82
+ };
83
+ var get = value => {
84
+ var _getAll;
85
+ return (_getAll = getAll(value)) === null || _getAll === void 0 ? void 0 : _getAll[0];
86
+ };
87
+ for (var element of formElement.elements) {
88
+ if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) {
89
+ var prev = element.dataset.conform;
90
+ var next = stateSnapshot.key[element.name];
91
+ var defaultValue = stateSnapshot.initialValue[element.name];
92
+ if (prev === 'managed') {
93
+ // Skip fields managed by useInputControl()
94
+ continue;
95
+ }
96
+ if (typeof prev === 'undefined' || prev !== next) {
97
+ element.dataset.conform = next;
98
+ if ('options' in element) {
99
+ var _getAll2;
100
+ var value = (_getAll2 = getAll(defaultValue)) !== null && _getAll2 !== void 0 ? _getAll2 : [];
101
+ for (var option of element.options) {
102
+ option.selected = value.includes(option.value);
103
+ }
104
+ } else if ('checked' in element && (element.type === 'checkbox' || element.type === 'radio')) {
105
+ element.checked = get(defaultValue) === element.value;
106
+ } else {
107
+ var _get;
108
+ element.value = (_get = get(defaultValue)) !== null && _get !== void 0 ? _get : '';
109
+ }
110
+ }
111
+ }
112
+ }
113
+ }, [formId, stateSnapshot]);
63
114
  return [form, form.getFieldset()];
64
115
  }
65
116
  function useFormMetadata(formId) {
package/hooks.mjs CHANGED
@@ -52,10 +52,61 @@ function useForm(options) {
52
52
  formId
53
53
  }));
54
54
  });
55
- var subjectRef = useSubjectRef();
55
+ var subjectRef = useSubjectRef({
56
+ key: {
57
+ // Subscribe to all key changes so it will re-render and
58
+ // update the field value as soon as the DOM is updated
59
+ prefix: ['']
60
+ }
61
+ });
56
62
  var stateSnapshot = useFormState(context, subjectRef);
57
63
  var noValidate = useNoValidate(options.defaultNoValidate);
58
64
  var form = getFormMetadata(context, subjectRef, stateSnapshot, noValidate);
65
+ useEffect(() => {
66
+ var formElement = document.forms.namedItem(formId);
67
+ if (!formElement) {
68
+ return;
69
+ }
70
+ var getAll = value => {
71
+ if (typeof value === 'string') {
72
+ return [value];
73
+ }
74
+ if (Array.isArray(value) && value.every(item => typeof item === 'string')) {
75
+ return value;
76
+ }
77
+ return undefined;
78
+ };
79
+ var get = value => {
80
+ var _getAll;
81
+ return (_getAll = getAll(value)) === null || _getAll === void 0 ? void 0 : _getAll[0];
82
+ };
83
+ for (var element of formElement.elements) {
84
+ if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) {
85
+ var prev = element.dataset.conform;
86
+ var next = stateSnapshot.key[element.name];
87
+ var defaultValue = stateSnapshot.initialValue[element.name];
88
+ if (prev === 'managed') {
89
+ // Skip fields managed by useInputControl()
90
+ continue;
91
+ }
92
+ if (typeof prev === 'undefined' || prev !== next) {
93
+ element.dataset.conform = next;
94
+ if ('options' in element) {
95
+ var _getAll2;
96
+ var value = (_getAll2 = getAll(defaultValue)) !== null && _getAll2 !== void 0 ? _getAll2 : [];
97
+ for (var option of element.options) {
98
+ option.selected = value.includes(option.value);
99
+ }
100
+ } else if ('checked' in element && (element.type === 'checkbox' || element.type === 'radio')) {
101
+ element.checked = get(defaultValue) === element.value;
102
+ } else {
103
+ var _get;
104
+ element.value = (_get = get(defaultValue)) !== null && _get !== void 0 ? _get : '';
105
+ }
106
+ }
107
+ }
108
+ }
109
+ }, [formId, stateSnapshot]);
59
110
  return [form, form.getFieldset()];
60
111
  }
61
112
  function useFormMetadata(formId) {
package/index.d.ts CHANGED
@@ -1,5 +1,29 @@
1
- export { type Submission, type SubmissionResult, type DefaultValue, type Intent, type FormId, type FieldName, parse, } from '@conform-to/dom';
2
- export { type FieldMetadata, type FormMetadata, FormProvider, FormStateInput, } from './context';
1
+ export {
2
+ type Submission,
3
+ type SubmissionResult,
4
+ type DefaultValue,
5
+ type Intent,
6
+ type FormId,
7
+ type FieldName,
8
+ parse,
9
+ } from '@conform-to/dom';
10
+ export {
11
+ type FieldMetadata,
12
+ type FormMetadata,
13
+ FormProvider,
14
+ FormStateInput,
15
+ } from './context';
3
16
  export { useForm, useFormMetadata, useField } from './hooks';
4
- export { Control as unstable_Control, useControl as unstable_useControl, useInputControl, } from './integrations';
5
- export { getFormProps, getFieldsetProps, getInputProps, getSelectProps, getTextareaProps, getCollectionProps, } from './helpers';
17
+ export {
18
+ Control as unstable_Control,
19
+ useControl as unstable_useControl,
20
+ useInputControl,
21
+ } from './integrations';
22
+ export {
23
+ getFormProps,
24
+ getFieldsetProps,
25
+ getInputProps,
26
+ getSelectProps,
27
+ getTextareaProps,
28
+ getCollectionProps,
29
+ } from './helpers';
package/integrations.js CHANGED
@@ -31,7 +31,7 @@ function createDummySelect(form, name, value) {
31
31
  var options = typeof value === 'string' ? [value] : value !== null && value !== void 0 ? value : [];
32
32
  select.name = name;
33
33
  select.multiple = true;
34
- select.dataset.conform = 'true';
34
+ select.dataset.conform = 'managed';
35
35
 
36
36
  // To make sure the input is hidden but still focusable
37
37
  select.setAttribute('aria-hidden', 'true');
@@ -52,7 +52,7 @@ function createDummySelect(form, name, value) {
52
52
  return select;
53
53
  }
54
54
  function isDummySelect(element) {
55
- return element.dataset.conform === 'true';
55
+ return element.dataset.conform === 'managed';
56
56
  }
57
57
  function updateFieldValue(element, value) {
58
58
  if (element instanceof HTMLInputElement && (element.type === 'checkbox' || element.type === 'radio')) {
package/integrations.mjs CHANGED
@@ -27,7 +27,7 @@ function createDummySelect(form, name, value) {
27
27
  var options = typeof value === 'string' ? [value] : value !== null && value !== void 0 ? value : [];
28
28
  select.name = name;
29
29
  select.multiple = true;
30
- select.dataset.conform = 'true';
30
+ select.dataset.conform = 'managed';
31
31
 
32
32
  // To make sure the input is hidden but still focusable
33
33
  select.setAttribute('aria-hidden', 'true');
@@ -48,7 +48,7 @@ function createDummySelect(form, name, value) {
48
48
  return select;
49
49
  }
50
50
  function isDummySelect(element) {
51
- return element.dataset.conform === 'true';
51
+ return element.dataset.conform === 'managed';
52
52
  }
53
53
  function updateFieldValue(element, value) {
54
54
  if (element instanceof HTMLInputElement && (element.type === 'checkbox' || element.type === 'radio')) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Conform view adapter for react",
4
4
  "homepage": "https://conform.guide",
5
5
  "license": "MIT",
6
- "version": "1.1.4",
6
+ "version": "1.2.0",
7
7
  "main": "index.js",
8
8
  "module": "index.mjs",
9
9
  "types": "index.d.ts",
@@ -30,7 +30,7 @@
30
30
  "url": "https://github.com/edmundhung/conform/issues"
31
31
  },
32
32
  "dependencies": {
33
- "@conform-to/dom": "1.1.4"
33
+ "@conform-to/dom": "1.2.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@babel/core": "^7.17.8",
package/rollup.config.js DELETED
@@ -1,102 +0,0 @@
1
- import path from 'node:path';
2
- import babel from '@rollup/plugin-babel';
3
- import nodeResolve from '@rollup/plugin-node-resolve';
4
- import copy from 'rollup-plugin-copy';
5
-
6
- /** @returns {import("rollup").RollupOptions[]} */
7
- function configurePackage() {
8
- let sourceDir = '.';
9
- let outputDir = sourceDir;
10
-
11
- /** @type {import("rollup").RollupOptions} */
12
- let ESM = {
13
- external(id) {
14
- return !id.startsWith('.') && !path.isAbsolute(id);
15
- },
16
- input: `${sourceDir}/index.ts`,
17
- output: {
18
- dir: outputDir,
19
- format: 'esm',
20
- preserveModules: true,
21
- entryFileNames: '[name].mjs',
22
- },
23
- plugins: [
24
- babel({
25
- babelrc: false,
26
- configFile: false,
27
- presets: [
28
- [
29
- '@babel/preset-env',
30
- {
31
- targets: {
32
- node: '16',
33
- esmodules: true,
34
- },
35
- },
36
- ],
37
- ['@babel/preset-react', { runtime: 'automatic' }],
38
- '@babel/preset-typescript',
39
- ],
40
- plugins: [],
41
- babelHelpers: 'bundled',
42
- exclude: /node_modules/,
43
- extensions: ['.ts', '.tsx'],
44
- }),
45
- nodeResolve({
46
- extensions: ['.ts', '.tsx'],
47
- }),
48
- copy({
49
- targets: [
50
- { src: `../../README`, dest: sourceDir },
51
- { src: `../../LICENSE`, dest: sourceDir },
52
- ],
53
- }),
54
- ],
55
- };
56
-
57
- /** @type {import("rollup").RollupOptions} */
58
- let CJS = {
59
- external(id) {
60
- return !id.startsWith('.') && !path.isAbsolute(id);
61
- },
62
- input: `${sourceDir}/index.ts`,
63
- output: {
64
- dir: outputDir,
65
- format: 'cjs',
66
- preserveModules: true,
67
- exports: 'auto',
68
- },
69
- plugins: [
70
- babel({
71
- babelrc: false,
72
- configFile: false,
73
- presets: [
74
- [
75
- '@babel/preset-env',
76
- {
77
- targets: {
78
- node: '16',
79
- esmodules: true,
80
- },
81
- },
82
- ],
83
- ['@babel/preset-react', { runtime: 'automatic' }],
84
- '@babel/preset-typescript',
85
- ],
86
- plugins: [],
87
- babelHelpers: 'bundled',
88
- exclude: /node_modules/,
89
- extensions: ['.ts', '.tsx'],
90
- }),
91
- nodeResolve({
92
- extensions: ['.ts', '.tsx'],
93
- }),
94
- ],
95
- };
96
-
97
- return [ESM, CJS];
98
- }
99
-
100
- export default function rollup() {
101
- return configurePackage();
102
- }