@conform-to/react 1.1.5 → 1.2.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/context.d.ts +59 -187
- package/helpers.d.ts +1 -1
- package/helpers.js +0 -1
- package/helpers.mjs +0 -1
- package/hooks.d.ts +24 -53
- package/hooks.js +52 -1
- package/hooks.mjs +52 -1
- package/index.d.ts +4 -28
- package/integrations.js +3 -16
- package/integrations.mjs +3 -16
- package/package.json +2 -2
- package/README +0 -37
- package/experimental.d.ts +0 -0
package/context.d.ts
CHANGED
|
@@ -1,200 +1,72 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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';
|
|
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';
|
|
20
3
|
export type Pretty<T> = {
|
|
21
|
-
|
|
4
|
+
[K in keyof T]: T[K];
|
|
22
5
|
} & {};
|
|
23
|
-
export type Primitive =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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;
|
|
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;
|
|
48
19
|
};
|
|
49
|
-
export type FormMetadata<
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
>;
|
|
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>[]>;
|
|
99
41
|
declare const wrappedSymbol: unique symbol;
|
|
100
42
|
export type Wrapped<Type> = {
|
|
101
|
-
|
|
43
|
+
[wrappedSymbol]: Type;
|
|
102
44
|
};
|
|
103
|
-
export declare function getWrappedFormContext(
|
|
104
|
-
|
|
105
|
-
):
|
|
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>;
|
|
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>;
|
|
114
48
|
export declare function FormProvider(props: {
|
|
115
|
-
|
|
116
|
-
|
|
49
|
+
context: Wrapped<FormContext<any, any>>;
|
|
50
|
+
children: ReactNode;
|
|
117
51
|
}): ReactElement;
|
|
118
52
|
export declare function FormStateInput(props: {
|
|
119
|
-
|
|
53
|
+
formId?: string;
|
|
120
54
|
}): React.ReactElement;
|
|
121
|
-
export declare function useSubjectRef(
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
export declare function
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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;
|
|
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;
|
|
179
66
|
};
|
|
180
|
-
export type FormContext<
|
|
181
|
-
|
|
182
|
-
|
|
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;
|
|
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;
|
|
192
70
|
};
|
|
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>;
|
|
71
|
+
export declare function createFormContext<Schema extends Record<string, any> = any, FormError = string[], FormValue = Schema>(options: FormOptions<Schema, FormError, FormValue>): FormContext<Schema, FormError, FormValue>;
|
|
200
72
|
export {};
|
package/helpers.d.ts
CHANGED
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
package/hooks.d.ts
CHANGED
|
@@ -1,63 +1,34 @@
|
|
|
1
1
|
import { type FormId, type FieldName } from '@conform-to/dom';
|
|
2
2
|
import { useEffect } from 'react';
|
|
3
|
-
import {
|
|
4
|
-
type FormMetadata,
|
|
5
|
-
type FieldMetadata,
|
|
6
|
-
type Pretty,
|
|
7
|
-
type FormOptions,
|
|
8
|
-
} from './context';
|
|
3
|
+
import { type FormMetadata, type FieldMetadata, type Pretty, type FormOptions } from './context';
|
|
9
4
|
/**
|
|
10
5
|
* useLayoutEffect is client-only.
|
|
11
6
|
* This basically makes it a no-op on server
|
|
12
7
|
*/
|
|
13
8
|
export declare const useSafeLayoutEffect: typeof useEffect;
|
|
14
|
-
export declare function useFormId<
|
|
15
|
-
Schema extends Record<string, unknown>,
|
|
16
|
-
FormError,
|
|
17
|
-
>(preferredId?: string): FormId<Schema, FormError>;
|
|
9
|
+
export declare function useFormId<Schema extends Record<string, unknown>, FormError>(preferredId?: string): FormId<Schema, FormError>;
|
|
18
10
|
export declare function useNoValidate(defaultNoValidate?: boolean): boolean;
|
|
19
|
-
export declare function useForm<
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* Default to `true`.
|
|
34
|
-
*/
|
|
35
|
-
defaultNoValidate?: boolean;
|
|
36
|
-
}
|
|
37
|
-
>,
|
|
38
|
-
): [
|
|
39
|
-
FormMetadata<Schema, FormError>,
|
|
40
|
-
ReturnType<FormMetadata<Schema, FormError>['getFieldset']>,
|
|
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']>
|
|
41
25
|
];
|
|
42
|
-
export declare function useFormMetadata<
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
>(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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>,
|
|
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>
|
|
63
34
|
];
|
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' || element.type === 'submit' || element.type === 'reset' || element.type === 'button') {
|
|
93
|
+
// Skip buttons and 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' || element.type === 'submit' || element.type === 'reset' || element.type === 'button') {
|
|
89
|
+
// Skip buttons and 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,29 +1,5 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
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';
|
|
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';
|
|
16
3
|
export { useForm, useFormMetadata, useField } from './hooks';
|
|
17
|
-
export {
|
|
18
|
-
|
|
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';
|
|
4
|
+
export { Control as unstable_Control, useControl as unstable_useControl, useInputControl, } from './integrations';
|
|
5
|
+
export { getFormProps, getFieldsetProps, getInputProps, getSelectProps, getTextareaProps, getCollectionProps, } 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 = '
|
|
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 === '
|
|
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')) {
|
|
@@ -216,21 +216,8 @@ function useControl(meta) {
|
|
|
216
216
|
setValue(value);
|
|
217
217
|
change(value);
|
|
218
218
|
};
|
|
219
|
-
var refCallback = element => {
|
|
220
|
-
var _meta$key;
|
|
221
|
-
register(element);
|
|
222
|
-
if (!element) {
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
var prevKey = element.dataset.conform;
|
|
226
|
-
var nextKey = "".concat((_meta$key = meta.key) !== null && _meta$key !== void 0 ? _meta$key : '');
|
|
227
|
-
if (prevKey !== nextKey) {
|
|
228
|
-
element.dataset.conform = nextKey;
|
|
229
|
-
updateFieldValue(element, value !== null && value !== void 0 ? value : '');
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
219
|
return {
|
|
233
|
-
register
|
|
220
|
+
register,
|
|
234
221
|
value,
|
|
235
222
|
change: handleChange,
|
|
236
223
|
focus,
|
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 = '
|
|
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 === '
|
|
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')) {
|
|
@@ -212,21 +212,8 @@ function useControl(meta) {
|
|
|
212
212
|
setValue(value);
|
|
213
213
|
change(value);
|
|
214
214
|
};
|
|
215
|
-
var refCallback = element => {
|
|
216
|
-
var _meta$key;
|
|
217
|
-
register(element);
|
|
218
|
-
if (!element) {
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
var prevKey = element.dataset.conform;
|
|
222
|
-
var nextKey = "".concat((_meta$key = meta.key) !== null && _meta$key !== void 0 ? _meta$key : '');
|
|
223
|
-
if (prevKey !== nextKey) {
|
|
224
|
-
element.dataset.conform = nextKey;
|
|
225
|
-
updateFieldValue(element, value !== null && value !== void 0 ? value : '');
|
|
226
|
-
}
|
|
227
|
-
};
|
|
228
215
|
return {
|
|
229
|
-
register
|
|
216
|
+
register,
|
|
230
217
|
value,
|
|
231
218
|
change: handleChange,
|
|
232
219
|
focus,
|
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
|
|
6
|
+
"version": "1.2.1",
|
|
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
|
|
33
|
+
"@conform-to/dom": "1.2.1"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@babel/core": "^7.17.8",
|
package/README
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
███████╗ ██████╗ ███╗ ██╗ ████████╗ ██████╗ ███████╗ ███╗ ███╗
|
|
4
|
-
██╔═════╝ ██╔═══██╗ ████╗ ██║ ██╔═════╝ ██╔═══██╗ ██╔═══██╗ ████████║
|
|
5
|
-
██║ ██║ ██║ ██╔██╗██║ ███████╗ ██║ ██║ ███████╔╝ ██╔██╔██║
|
|
6
|
-
██║ ██║ ██║ ██║╚████║ ██╔════╝ ██║ ██║ ██╔═══██╗ ██║╚═╝██║
|
|
7
|
-
╚███████╗ ╚██████╔╝ ██║ ╚███║ ██║ ╚██████╔╝ ██║ ██║ ██║ ██║
|
|
8
|
-
╚══════╝ ╚═════╝ ╚═╝ ╚══╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
Version 1.1.3 / License MIT / Copyright (c) 2024 Edmund Hung
|
|
12
|
-
|
|
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
|
-
|
|
15
|
-
# Getting Started
|
|
16
|
-
|
|
17
|
-
Check out the overview and tutorial at our website https://conform.guide
|
|
18
|
-
|
|
19
|
-
# Features
|
|
20
|
-
|
|
21
|
-
- Progressive enhancement first APIs
|
|
22
|
-
- Type-safe field inference
|
|
23
|
-
- Fine-grained subscription
|
|
24
|
-
- Built-in accessibility helpers
|
|
25
|
-
- Automatic type coercion with Zod
|
|
26
|
-
|
|
27
|
-
# Documentation
|
|
28
|
-
|
|
29
|
-
- Validation: https://conform.guide/validation
|
|
30
|
-
- Nested object and Array: https://conform.guide/complex-structures
|
|
31
|
-
- UI Integrations: https://conform.guide/integration/ui-libraries
|
|
32
|
-
- Intent button: https://conform.guide/intent-button
|
|
33
|
-
- Accessibility Guide: https://conform.guide/accessibility
|
|
34
|
-
|
|
35
|
-
# Support
|
|
36
|
-
|
|
37
|
-
To report a bug, please open an issue on the repository at https://github.com/edmundhung/conform. For feature requests and questions, you can post them in the Discussions section.
|
package/experimental.d.ts
DELETED
|
File without changes
|