@conform-to/react 1.0.0-pre.4 → 1.0.0-pre.5
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 +23 -32
- package/context.js +21 -20
- package/context.mjs +21 -20
- package/helpers.d.ts +7 -14
- package/helpers.js +0 -10
- package/helpers.mjs +1 -10
- package/hooks.d.ts +15 -23
- package/hooks.js +18 -34
- package/hooks.mjs +18 -34
- package/index.d.ts +3 -3
- package/index.js +2 -7
- package/index.mjs +2 -2
- package/integrations.d.ts +10 -29
- package/integrations.js +43 -68
- package/integrations.mjs +43 -68
- package/package.json +2 -2
package/context.d.ts
CHANGED
|
@@ -1,65 +1,56 @@
|
|
|
1
|
-
import { type Constraint, type FormId, type FieldName, type FormContext, type FormValue, type FormState, type SubscriptionScope, type SubscriptionSubject, type UnionKeyof, type UnionKeyType } from '@conform-to/dom';
|
|
1
|
+
import { type Constraint, type ControlButtonProps, type FormId, type FieldName, type FormContext, type FormControl, type FormValue, type FormState, type SubscriptionScope, type SubscriptionSubject, type UnionKeyof, type UnionKeyType } from '@conform-to/dom';
|
|
2
2
|
import { type ReactElement, type ReactNode, type MutableRefObject } from 'react';
|
|
3
3
|
export type Pretty<T> = {
|
|
4
4
|
[K in keyof T]: T[K];
|
|
5
5
|
} & {};
|
|
6
6
|
export type Primitive = string | number | boolean | Date | File | null | undefined;
|
|
7
|
-
export type
|
|
8
|
-
|
|
9
|
-
name: FieldName<FieldSchema>;
|
|
10
|
-
} | {
|
|
11
|
-
formId: FieldSchema extends Record<string, unknown> ? FormId<FieldSchema, Error> : never;
|
|
12
|
-
name?: undefined;
|
|
13
|
-
};
|
|
14
|
-
export type Metadata<Schema, Error> = {
|
|
15
|
-
key?: string;
|
|
7
|
+
export type Metadata<Schema, FormError, FormSchema extends Record<string, unknown>> = {
|
|
8
|
+
key: string | undefined;
|
|
16
9
|
id: string;
|
|
17
10
|
errorId: string;
|
|
18
11
|
descriptionId: string;
|
|
12
|
+
name: FieldName<Schema, FormError, FormSchema>;
|
|
19
13
|
initialValue: FormValue<Schema>;
|
|
20
14
|
value: FormValue<Schema>;
|
|
21
|
-
errors:
|
|
22
|
-
allErrors: Record<string,
|
|
15
|
+
errors: FormError | undefined;
|
|
16
|
+
allErrors: Record<string, FormError>;
|
|
23
17
|
allValid: boolean;
|
|
24
18
|
valid: boolean;
|
|
25
19
|
dirty: boolean;
|
|
26
20
|
};
|
|
27
|
-
export type FormMetadata<Schema extends Record<string, unknown> = Record<string, unknown>,
|
|
28
|
-
id: FormId<Schema,
|
|
29
|
-
context: FormContext<Schema,
|
|
21
|
+
export type FormMetadata<Schema extends Record<string, unknown> = Record<string, unknown>, FormError = string[]> = Omit<Metadata<Schema, FormError, Schema>, 'id'> & {
|
|
22
|
+
id: FormId<Schema, FormError>;
|
|
23
|
+
context: FormContext<Schema, FormError>;
|
|
30
24
|
status?: 'success' | 'error';
|
|
25
|
+
dispatch(control: FormControl): void;
|
|
26
|
+
getControlButtonProps(control: FormControl): ControlButtonProps;
|
|
31
27
|
getFieldset: () => {
|
|
32
|
-
[Key in UnionKeyof<Schema>]: FieldMetadata<UnionKeyType<Schema, Key>,
|
|
28
|
+
[Key in UnionKeyof<Schema>]: FieldMetadata<UnionKeyType<Schema, Key>, FormError, Schema>;
|
|
33
29
|
};
|
|
34
30
|
onSubmit: (event: React.FormEvent<HTMLFormElement>) => ReturnType<FormContext<Schema>['submit']>;
|
|
35
31
|
onReset: (event: React.FormEvent<HTMLFormElement>) => void;
|
|
36
32
|
noValidate: boolean;
|
|
37
33
|
};
|
|
38
|
-
export type FieldMetadata<Schema = unknown,
|
|
39
|
-
formId: FormId<FormSchema,
|
|
40
|
-
name: FieldName<Schema>;
|
|
34
|
+
export type FieldMetadata<Schema = unknown, FormError = string[], FormSchema extends Record<string, any> = Record<string, unknown>> = Metadata<Schema, FormError, FormSchema> & {
|
|
35
|
+
formId: FormId<FormSchema, FormError>;
|
|
41
36
|
constraint?: Constraint;
|
|
42
37
|
getFieldset: unknown extends Schema ? () => unknown : Schema extends Primitive | Array<any> ? never : () => {
|
|
43
|
-
[Key in UnionKeyof<Schema>]: FieldMetadata<UnionKeyType<Schema, Key>,
|
|
38
|
+
[Key in UnionKeyof<Schema>]: FieldMetadata<UnionKeyType<Schema, Key>, FormError>;
|
|
44
39
|
};
|
|
45
|
-
getFieldList: unknown extends Schema ? () => unknown : Schema extends Array<infer Item> ? () => Array<FieldMetadata<Item,
|
|
40
|
+
getFieldList: unknown extends Schema ? () => unknown : Schema extends Array<infer Item> ? () => Array<FieldMetadata<Item, FormError>> : never;
|
|
46
41
|
};
|
|
47
|
-
export declare const
|
|
48
|
-
export declare function useFormContext<Schema extends Record<string, any>,
|
|
49
|
-
export declare function useFormState<
|
|
42
|
+
export declare const Form: import("react").Context<FormContext[]>;
|
|
43
|
+
export declare function useFormContext<Schema extends Record<string, any>, FormError>(formId?: FormId<Schema, FormError>): FormContext<Schema, FormError, unknown>;
|
|
44
|
+
export declare function useFormState<FormError>(form: FormContext<any, FormError>, subjectRef?: MutableRefObject<SubscriptionSubject>): FormState<FormError>;
|
|
50
45
|
export declare function FormProvider(props: {
|
|
51
46
|
context: FormContext<any, any, any>;
|
|
52
47
|
children: ReactNode;
|
|
53
48
|
}): ReactElement;
|
|
54
49
|
export declare function FormStateInput(props: {
|
|
55
|
-
formId
|
|
56
|
-
context?: undefined;
|
|
57
|
-
} | {
|
|
58
|
-
formId?: undefined;
|
|
59
|
-
context: FormContext;
|
|
50
|
+
formId?: string;
|
|
60
51
|
}): React.ReactElement;
|
|
61
52
|
export declare function useSubjectRef(initialSubject?: SubscriptionSubject): MutableRefObject<SubscriptionSubject>;
|
|
62
53
|
export declare function updateSubjectRef(ref: MutableRefObject<SubscriptionSubject>, name: string, subject: keyof SubscriptionSubject, scope: keyof SubscriptionScope): void;
|
|
63
|
-
export declare function getMetadata<Schema,
|
|
64
|
-
export declare function getFieldMetadata<Schema,
|
|
65
|
-
export declare function getFormMetadata<Schema extends Record<string, any>,
|
|
54
|
+
export declare function getMetadata<Schema, FormError, FormSchema extends Record<string, any>>(formId: FormId<FormSchema, FormError>, state: FormState<FormError>, subjectRef: MutableRefObject<SubscriptionSubject>, name?: FieldName<Schema, FormError, FormSchema>): Metadata<Schema, FormError, FormSchema>;
|
|
55
|
+
export declare function getFieldMetadata<Schema, FormError, FormSchema extends Record<string, any>>(formId: FormId<FormSchema, FormError>, state: FormState<FormError>, subjectRef: MutableRefObject<SubscriptionSubject>, prefix?: string, key?: string | number): FieldMetadata<Schema, FormError, FormSchema>;
|
|
56
|
+
export declare function getFormMetadata<Schema extends Record<string, any>, FormError = string[]>(formId: FormId<Schema, FormError>, state: FormState<FormError>, subjectRef: MutableRefObject<SubscriptionSubject>, context: FormContext<Schema, FormError, any>, noValidate: boolean): FormMetadata<Schema, FormError>;
|
package/context.js
CHANGED
|
@@ -7,10 +7,10 @@ var dom = require('@conform-to/dom');
|
|
|
7
7
|
var react = require('react');
|
|
8
8
|
var jsxRuntime = require('react/jsx-runtime');
|
|
9
9
|
|
|
10
|
-
var
|
|
11
|
-
function useFormContext(formId
|
|
12
|
-
var
|
|
13
|
-
var form =
|
|
10
|
+
var Form = /*#__PURE__*/react.createContext([]);
|
|
11
|
+
function useFormContext(formId) {
|
|
12
|
+
var contexts = react.useContext(Form);
|
|
13
|
+
var form = formId ? contexts.find(context => context.formId === formId) : contexts[0];
|
|
14
14
|
if (!form) {
|
|
15
15
|
throw new Error('Form context is not available');
|
|
16
16
|
}
|
|
@@ -21,18 +21,17 @@ function useFormState(form, subjectRef) {
|
|
|
21
21
|
return react.useSyncExternalStore(subscribe, form.getState, form.getState);
|
|
22
22
|
}
|
|
23
23
|
function FormProvider(props) {
|
|
24
|
-
var
|
|
25
|
-
var value = react.useMemo(() =>
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return /*#__PURE__*/jsxRuntime.jsx(
|
|
24
|
+
var forms = react.useContext(Form);
|
|
25
|
+
var value = react.useMemo(() => [props.context].concat(forms),
|
|
26
|
+
// Put the latest form context first
|
|
27
|
+
[forms, props.context]);
|
|
28
|
+
return /*#__PURE__*/jsxRuntime.jsx(Form.Provider, {
|
|
29
29
|
value: value,
|
|
30
30
|
children: props.children
|
|
31
31
|
});
|
|
32
32
|
}
|
|
33
33
|
function FormStateInput(props) {
|
|
34
|
-
var
|
|
35
|
-
var context = useFormContext((_props$formId = props.formId) !== null && _props$formId !== void 0 ? _props$formId : props.context.formId, props.context);
|
|
34
|
+
var context = useFormContext(props.formId);
|
|
36
35
|
return /*#__PURE__*/jsxRuntime.jsx("input", {
|
|
37
36
|
type: "hidden",
|
|
38
37
|
name: dom.STATE,
|
|
@@ -64,6 +63,7 @@ function getMetadata(formId, state, subjectRef) {
|
|
|
64
63
|
var id = name ? "".concat(formId, "-").concat(name) : formId;
|
|
65
64
|
return new Proxy({
|
|
66
65
|
id,
|
|
66
|
+
name,
|
|
67
67
|
errorId: "".concat(id, "-error"),
|
|
68
68
|
descriptionId: "".concat(id, "-description"),
|
|
69
69
|
initialValue: state.initialValue[name],
|
|
@@ -139,13 +139,11 @@ function getFieldMetadata(formId, state, subjectRef) {
|
|
|
139
139
|
var key = arguments.length > 4 ? arguments[4] : undefined;
|
|
140
140
|
var name = typeof key === 'undefined' ? prefix : dom.formatPaths([...dom.getPaths(prefix), key]);
|
|
141
141
|
var metadata = getMetadata(formId, state, subjectRef, name);
|
|
142
|
-
return new Proxy(
|
|
143
|
-
get(
|
|
142
|
+
return new Proxy({}, {
|
|
143
|
+
get(_, key, receiver) {
|
|
144
144
|
switch (key) {
|
|
145
145
|
case 'formId':
|
|
146
146
|
return formId;
|
|
147
|
-
case 'name':
|
|
148
|
-
return name;
|
|
149
147
|
case 'constraint':
|
|
150
148
|
return state.constraint[name];
|
|
151
149
|
case 'getFieldList':
|
|
@@ -161,19 +159,22 @@ function getFieldMetadata(formId, state, subjectRef) {
|
|
|
161
159
|
};
|
|
162
160
|
}
|
|
163
161
|
}
|
|
164
|
-
return Reflect.get(
|
|
162
|
+
return Reflect.get(metadata, key, receiver);
|
|
165
163
|
}
|
|
166
164
|
});
|
|
167
165
|
}
|
|
168
166
|
function getFormMetadata(formId, state, subjectRef, context, noValidate) {
|
|
169
167
|
var metadata = getMetadata(formId, state, subjectRef);
|
|
170
|
-
return new Proxy(
|
|
171
|
-
get(
|
|
168
|
+
return new Proxy({}, {
|
|
169
|
+
get(_, key, receiver) {
|
|
172
170
|
switch (key) {
|
|
173
171
|
case 'context':
|
|
174
172
|
return context;
|
|
175
173
|
case 'status':
|
|
176
174
|
return state.submissionStatus;
|
|
175
|
+
case 'dispatch':
|
|
176
|
+
case 'getControlButtonProps':
|
|
177
|
+
return context[key];
|
|
177
178
|
case 'onSubmit':
|
|
178
179
|
return event => {
|
|
179
180
|
var submitEvent = event.nativeEvent;
|
|
@@ -185,14 +186,14 @@ function getFormMetadata(formId, state, subjectRef, context, noValidate) {
|
|
|
185
186
|
case 'noValidate':
|
|
186
187
|
return noValidate;
|
|
187
188
|
}
|
|
188
|
-
return Reflect.get(
|
|
189
|
+
return Reflect.get(metadata, key, receiver);
|
|
189
190
|
}
|
|
190
191
|
});
|
|
191
192
|
}
|
|
192
193
|
|
|
194
|
+
exports.Form = Form;
|
|
193
195
|
exports.FormProvider = FormProvider;
|
|
194
196
|
exports.FormStateInput = FormStateInput;
|
|
195
|
-
exports.Registry = Registry;
|
|
196
197
|
exports.getFieldMetadata = getFieldMetadata;
|
|
197
198
|
exports.getFormMetadata = getFormMetadata;
|
|
198
199
|
exports.getMetadata = getMetadata;
|
package/context.mjs
CHANGED
|
@@ -3,10 +3,10 @@ import { STATE, formatPaths, getPaths, 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
|
|
7
|
-
function useFormContext(formId
|
|
8
|
-
var
|
|
9
|
-
var form =
|
|
6
|
+
var Form = /*#__PURE__*/createContext([]);
|
|
7
|
+
function useFormContext(formId) {
|
|
8
|
+
var contexts = useContext(Form);
|
|
9
|
+
var form = formId ? contexts.find(context => context.formId === formId) : contexts[0];
|
|
10
10
|
if (!form) {
|
|
11
11
|
throw new Error('Form context is not available');
|
|
12
12
|
}
|
|
@@ -17,18 +17,17 @@ function useFormState(form, subjectRef) {
|
|
|
17
17
|
return useSyncExternalStore(subscribe, form.getState, form.getState);
|
|
18
18
|
}
|
|
19
19
|
function FormProvider(props) {
|
|
20
|
-
var
|
|
21
|
-
var value = useMemo(() =>
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return /*#__PURE__*/jsx(
|
|
20
|
+
var forms = useContext(Form);
|
|
21
|
+
var value = useMemo(() => [props.context].concat(forms),
|
|
22
|
+
// Put the latest form context first
|
|
23
|
+
[forms, props.context]);
|
|
24
|
+
return /*#__PURE__*/jsx(Form.Provider, {
|
|
25
25
|
value: value,
|
|
26
26
|
children: props.children
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
29
|
function FormStateInput(props) {
|
|
30
|
-
var
|
|
31
|
-
var context = useFormContext((_props$formId = props.formId) !== null && _props$formId !== void 0 ? _props$formId : props.context.formId, props.context);
|
|
30
|
+
var context = useFormContext(props.formId);
|
|
32
31
|
return /*#__PURE__*/jsx("input", {
|
|
33
32
|
type: "hidden",
|
|
34
33
|
name: STATE,
|
|
@@ -60,6 +59,7 @@ function getMetadata(formId, state, subjectRef) {
|
|
|
60
59
|
var id = name ? "".concat(formId, "-").concat(name) : formId;
|
|
61
60
|
return new Proxy({
|
|
62
61
|
id,
|
|
62
|
+
name,
|
|
63
63
|
errorId: "".concat(id, "-error"),
|
|
64
64
|
descriptionId: "".concat(id, "-description"),
|
|
65
65
|
initialValue: state.initialValue[name],
|
|
@@ -135,13 +135,11 @@ function getFieldMetadata(formId, state, subjectRef) {
|
|
|
135
135
|
var key = arguments.length > 4 ? arguments[4] : undefined;
|
|
136
136
|
var name = typeof key === 'undefined' ? prefix : formatPaths([...getPaths(prefix), key]);
|
|
137
137
|
var metadata = getMetadata(formId, state, subjectRef, name);
|
|
138
|
-
return new Proxy(
|
|
139
|
-
get(
|
|
138
|
+
return new Proxy({}, {
|
|
139
|
+
get(_, key, receiver) {
|
|
140
140
|
switch (key) {
|
|
141
141
|
case 'formId':
|
|
142
142
|
return formId;
|
|
143
|
-
case 'name':
|
|
144
|
-
return name;
|
|
145
143
|
case 'constraint':
|
|
146
144
|
return state.constraint[name];
|
|
147
145
|
case 'getFieldList':
|
|
@@ -157,19 +155,22 @@ function getFieldMetadata(formId, state, subjectRef) {
|
|
|
157
155
|
};
|
|
158
156
|
}
|
|
159
157
|
}
|
|
160
|
-
return Reflect.get(
|
|
158
|
+
return Reflect.get(metadata, key, receiver);
|
|
161
159
|
}
|
|
162
160
|
});
|
|
163
161
|
}
|
|
164
162
|
function getFormMetadata(formId, state, subjectRef, context, noValidate) {
|
|
165
163
|
var metadata = getMetadata(formId, state, subjectRef);
|
|
166
|
-
return new Proxy(
|
|
167
|
-
get(
|
|
164
|
+
return new Proxy({}, {
|
|
165
|
+
get(_, key, receiver) {
|
|
168
166
|
switch (key) {
|
|
169
167
|
case 'context':
|
|
170
168
|
return context;
|
|
171
169
|
case 'status':
|
|
172
170
|
return state.submissionStatus;
|
|
171
|
+
case 'dispatch':
|
|
172
|
+
case 'getControlButtonProps':
|
|
173
|
+
return context[key];
|
|
173
174
|
case 'onSubmit':
|
|
174
175
|
return event => {
|
|
175
176
|
var submitEvent = event.nativeEvent;
|
|
@@ -181,9 +182,9 @@ function getFormMetadata(formId, state, subjectRef, context, noValidate) {
|
|
|
181
182
|
case 'noValidate':
|
|
182
183
|
return noValidate;
|
|
183
184
|
}
|
|
184
|
-
return Reflect.get(
|
|
185
|
+
return Reflect.get(metadata, key, receiver);
|
|
185
186
|
}
|
|
186
187
|
});
|
|
187
188
|
}
|
|
188
189
|
|
|
189
|
-
export { FormProvider, FormStateInput,
|
|
190
|
+
export { Form, FormProvider, FormStateInput, getFieldMetadata, getFormMetadata, getMetadata, updateSubjectRef, useFormContext, useFormState, useSubjectRef };
|
package/helpers.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { type Intent } from '@conform-to/dom';
|
|
3
2
|
import type { FormMetadata, FieldMetadata, Metadata, Pretty, Primitive } from './context';
|
|
4
3
|
type FormControlProps = {
|
|
5
4
|
key: string | undefined;
|
|
@@ -79,7 +78,7 @@ type TextareaOptions = Pretty<FormControlOptions & {
|
|
|
79
78
|
/**
|
|
80
79
|
* Derives aria attributes of a form control based on the field metadata.
|
|
81
80
|
*/
|
|
82
|
-
export declare function getAriaAttributes
|
|
81
|
+
export declare function getAriaAttributes(metadata: Metadata<any, any, any>, options?: FormControlOptions): {
|
|
83
82
|
'aria-invalid'?: boolean;
|
|
84
83
|
'aria-describedby'?: string;
|
|
85
84
|
};
|
|
@@ -92,10 +91,10 @@ export declare function getAriaAttributes<Schema, Error>(metadata: Metadata<Sche
|
|
|
92
91
|
* <form {...getFormProps(metadata)} />
|
|
93
92
|
* ```
|
|
94
93
|
*/
|
|
95
|
-
export declare function getFormProps<Schema extends Record<string, any>,
|
|
94
|
+
export declare function getFormProps<Schema extends Record<string, any>, FormError>(metadata: FormMetadata<Schema, FormError>, options?: FormControlOptions): {
|
|
96
95
|
'aria-invalid'?: boolean | undefined;
|
|
97
96
|
'aria-describedby'?: string | undefined;
|
|
98
|
-
id: import("@conform-to/dom").FormId<Schema,
|
|
97
|
+
id: import("@conform-to/dom").FormId<Schema, FormError>;
|
|
99
98
|
onSubmit: (event: import("react").FormEvent<HTMLFormElement>) => void;
|
|
100
99
|
noValidate: boolean;
|
|
101
100
|
};
|
|
@@ -108,18 +107,18 @@ export declare function getFormProps<Schema extends Record<string, any>, Error>(
|
|
|
108
107
|
* <fieldset {...getFieldsetProps(metadata)} />
|
|
109
108
|
* ```
|
|
110
109
|
*/
|
|
111
|
-
export declare function getFieldsetProps<Schema extends Record<string, any> | undefined | unknown
|
|
110
|
+
export declare function getFieldsetProps<Schema extends Record<string, any> | undefined | unknown>(metadata: FieldMetadata<Schema, any, any>, options?: FormControlOptions): {
|
|
112
111
|
'aria-invalid'?: boolean | undefined;
|
|
113
112
|
'aria-describedby'?: string | undefined;
|
|
114
113
|
id: string;
|
|
115
|
-
name: import("@conform-to/dom").FieldName<Schema>;
|
|
116
|
-
form: import("@conform-to/dom").FormId<any,
|
|
114
|
+
name: import("@conform-to/dom").FieldName<Schema, any, any>;
|
|
115
|
+
form: import("@conform-to/dom").FormId<any, any>;
|
|
117
116
|
};
|
|
118
117
|
/**
|
|
119
118
|
* Derives common properties of a form control based on the field metadata,
|
|
120
119
|
* including `key`, `id`, `name`, `form`, `required`, `autoFocus`, `aria-invalid` and `aria-describedby`.
|
|
121
120
|
*/
|
|
122
|
-
export declare function getFormControlProps<Schema
|
|
121
|
+
export declare function getFormControlProps<Schema>(metadata: FieldMetadata<Schema, any, any>, options?: FormControlOptions): FormControlProps;
|
|
123
122
|
/**
|
|
124
123
|
* Derives the properties of an input element based on the field metadata,
|
|
125
124
|
* including common form control attributes like `key`, `id`, `name`, `form`, `autoFocus`, `aria-invalid`, `aria-describedby`
|
|
@@ -210,10 +209,4 @@ export declare function getCollectionProps<Schema extends Array<string | boolean
|
|
|
210
209
|
*/
|
|
211
210
|
value?: boolean;
|
|
212
211
|
}>): Array<InputProps & Pick<Required<InputProps>, 'type' | 'value'>>;
|
|
213
|
-
export declare function getControlButtonProps(formId: string, intent: Intent): {
|
|
214
|
-
name: string;
|
|
215
|
-
value: string;
|
|
216
|
-
form: string;
|
|
217
|
-
formNoValidate: boolean;
|
|
218
|
-
};
|
|
219
212
|
export {};
|
package/helpers.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
|
|
6
|
-
var dom = require('@conform-to/dom');
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Cleanup `undefined` from the result.
|
|
@@ -217,18 +216,9 @@ function getCollectionProps(metadata, options) {
|
|
|
217
216
|
}));
|
|
218
217
|
});
|
|
219
218
|
}
|
|
220
|
-
function getControlButtonProps(formId, intent) {
|
|
221
|
-
return {
|
|
222
|
-
name: dom.INTENT,
|
|
223
|
-
value: dom.serializeIntent(intent),
|
|
224
|
-
form: formId,
|
|
225
|
-
formNoValidate: true
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
219
|
|
|
229
220
|
exports.getAriaAttributes = getAriaAttributes;
|
|
230
221
|
exports.getCollectionProps = getCollectionProps;
|
|
231
|
-
exports.getControlButtonProps = getControlButtonProps;
|
|
232
222
|
exports.getFieldsetProps = getFieldsetProps;
|
|
233
223
|
exports.getFormControlProps = getFormControlProps;
|
|
234
224
|
exports.getFormProps = getFormProps;
|
package/helpers.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
-
import { INTENT, serializeIntent } from '@conform-to/dom';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Cleanup `undefined` from the result.
|
|
@@ -213,13 +212,5 @@ function getCollectionProps(metadata, options) {
|
|
|
213
212
|
}));
|
|
214
213
|
});
|
|
215
214
|
}
|
|
216
|
-
function getControlButtonProps(formId, intent) {
|
|
217
|
-
return {
|
|
218
|
-
name: INTENT,
|
|
219
|
-
value: serializeIntent(intent),
|
|
220
|
-
form: formId,
|
|
221
|
-
formNoValidate: true
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
215
|
|
|
225
|
-
export { getAriaAttributes, getCollectionProps,
|
|
216
|
+
export { getAriaAttributes, getCollectionProps, getFieldsetProps, getFormControlProps, getFormProps, getInputProps, getSelectProps, getTextareaProps };
|
package/hooks.d.ts
CHANGED
|
@@ -6,12 +6,11 @@ import { type FormMetadata, type FieldMetadata, type Pretty } from './context';
|
|
|
6
6
|
* This basically makes it a no-op on server
|
|
7
7
|
*/
|
|
8
8
|
export declare const useSafeLayoutEffect: typeof useEffect;
|
|
9
|
-
export declare function useFormId<Schema extends Record<string, unknown>,
|
|
9
|
+
export declare function useFormId<Schema extends Record<string, unknown>, FormError>(preferredId?: string): FormId<Schema, FormError>;
|
|
10
10
|
export declare function useNoValidate(defaultNoValidate?: boolean): boolean;
|
|
11
|
-
export declare function useForm<Schema extends Record<string, any>,
|
|
11
|
+
export declare function useForm<Schema extends Record<string, any>, FormError = string[], FormValue = Schema>(options: Pretty<Omit<FormOptions<Schema, FormError, FormValue>, 'formId'> & {
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
* input and error elements will be derived.
|
|
13
|
+
* The form id. If not provided, a random id will be generated.
|
|
15
14
|
*/
|
|
16
15
|
id?: string;
|
|
17
16
|
/**
|
|
@@ -20,23 +19,16 @@ export declare function useForm<Schema extends Record<string, any>, Error = stri
|
|
|
20
19
|
* Default to `true`.
|
|
21
20
|
*/
|
|
22
21
|
defaultNoValidate?: boolean;
|
|
23
|
-
}>):
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
export declare function useFormMetadata<Schema extends Record<string, any>,
|
|
28
|
-
formId: FormId<Schema, Error>;
|
|
22
|
+
}>): [
|
|
23
|
+
FormMetadata<Schema, FormError>,
|
|
24
|
+
ReturnType<FormMetadata<Schema, FormError>['getFieldset']>
|
|
25
|
+
];
|
|
26
|
+
export declare function useFormMetadata<Schema extends Record<string, any>, FormError>(formId: FormId<Schema, FormError>, options?: {
|
|
29
27
|
defaultNoValidate?: boolean;
|
|
30
|
-
}): FormMetadata<Schema,
|
|
31
|
-
export declare function useField<FormSchema extends Record<string, unknown
|
|
32
|
-
formId
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}): {
|
|
38
|
-
meta: FieldMetadata<FieldSchema, Error, FormSchema>;
|
|
39
|
-
fields: FieldMetadata<FieldSchema, Error, FormSchema>['getFieldset'] extends Function ? ReturnType<FieldMetadata<FieldSchema, Error, FormSchema>['getFieldset']> : never;
|
|
40
|
-
list: FieldMetadata<FieldSchema, Error, FormSchema>['getFieldList'] extends Function ? ReturnType<FieldMetadata<FieldSchema, Error, FormSchema>['getFieldList']> : never;
|
|
41
|
-
form: FormMetadata<FormSchema, Error>;
|
|
42
|
-
};
|
|
28
|
+
}): FormMetadata<Schema, FormError>;
|
|
29
|
+
export declare function useField<FieldSchema, FormError = string[], FormSchema extends Record<string, unknown> = Record<string, unknown>>(name: FieldName<FieldSchema, FormError, FormSchema>, options?: {
|
|
30
|
+
formId?: FormId<FormSchema, FormError>;
|
|
31
|
+
}): [
|
|
32
|
+
FieldMetadata<FieldSchema, FormError, FormSchema>,
|
|
33
|
+
FormMetadata<FormSchema, FormError>
|
|
34
|
+
];
|
package/hooks.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
|
|
5
6
|
var dom = require('@conform-to/dom');
|
|
6
7
|
var react = require('react');
|
|
7
8
|
var context = require('./context.js');
|
|
@@ -30,13 +31,9 @@ function useNoValidate() {
|
|
|
30
31
|
}
|
|
31
32
|
function useForm(options) {
|
|
32
33
|
var formId = useFormId(options.id);
|
|
33
|
-
var
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// If id changes, reinitialize the form immediately
|
|
37
|
-
if (formId !== context$1.formId) {
|
|
38
|
-
setFormContext(initializeContext);
|
|
39
|
-
}
|
|
34
|
+
var [context$1] = react.useState(() => dom.createFormContext(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, options), {}, {
|
|
35
|
+
formId
|
|
36
|
+
})));
|
|
40
37
|
var optionsRef = react.useRef(options);
|
|
41
38
|
useSafeLayoutEffect(() => {
|
|
42
39
|
document.addEventListener('input', context$1.input);
|
|
@@ -55,49 +52,36 @@ function useForm(options) {
|
|
|
55
52
|
}
|
|
56
53
|
if (options.lastResult) {
|
|
57
54
|
context$1.report(options.lastResult);
|
|
58
|
-
} else {
|
|
59
|
-
var _document$forms$named;
|
|
60
|
-
(_document$forms$named = document.forms.namedItem(context$1.formId)) === null || _document$forms$named === void 0 || _document$forms$named.reset();
|
|
61
55
|
}
|
|
62
56
|
}, [context$1, options.lastResult]);
|
|
63
57
|
useSafeLayoutEffect(() => {
|
|
64
58
|
optionsRef.current = options;
|
|
65
|
-
context$1.update(options)
|
|
59
|
+
context$1.update(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, options), {}, {
|
|
60
|
+
formId
|
|
61
|
+
}));
|
|
66
62
|
});
|
|
67
63
|
var subjectRef = context.useSubjectRef();
|
|
68
64
|
var state = context.useFormState(context$1, subjectRef);
|
|
69
65
|
var noValidate = useNoValidate(options.defaultNoValidate);
|
|
70
|
-
var
|
|
71
|
-
return
|
|
72
|
-
meta,
|
|
73
|
-
fields: meta.getFieldset()
|
|
74
|
-
};
|
|
66
|
+
var form = context.getFormMetadata(formId, state, subjectRef, context$1, noValidate);
|
|
67
|
+
return [form, form.getFieldset()];
|
|
75
68
|
}
|
|
76
|
-
function useFormMetadata(
|
|
69
|
+
function useFormMetadata(formId) {
|
|
70
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
77
71
|
var subjectRef = context.useSubjectRef();
|
|
78
|
-
var context$1 = context.useFormContext(
|
|
72
|
+
var context$1 = context.useFormContext(formId);
|
|
79
73
|
var state = context.useFormState(context$1, subjectRef);
|
|
80
74
|
var noValidate = useNoValidate(options.defaultNoValidate);
|
|
81
|
-
return context.getFormMetadata(
|
|
75
|
+
return context.getFormMetadata(context$1.formId, state, subjectRef, context$1, noValidate);
|
|
82
76
|
}
|
|
83
|
-
function useField(
|
|
77
|
+
function useField(name) {
|
|
78
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
84
79
|
var subjectRef = context.useSubjectRef();
|
|
85
80
|
var context$1 = context.useFormContext(options.formId);
|
|
86
81
|
var state = context.useFormState(context$1, subjectRef);
|
|
87
|
-
var
|
|
88
|
-
var form = context.getFormMetadata(
|
|
89
|
-
return
|
|
90
|
-
meta,
|
|
91
|
-
// @ts-expect-error The types is used as a hint only
|
|
92
|
-
get fields() {
|
|
93
|
-
return meta.getFieldset();
|
|
94
|
-
},
|
|
95
|
-
// @ts-expect-error The types is used as a hint only
|
|
96
|
-
get list() {
|
|
97
|
-
return meta.getFieldList();
|
|
98
|
-
},
|
|
99
|
-
form
|
|
100
|
-
};
|
|
82
|
+
var field = context.getFieldMetadata(context$1.formId, state, subjectRef, name);
|
|
83
|
+
var form = context.getFormMetadata(context$1.formId, state, subjectRef, context$1, false);
|
|
84
|
+
return [field, form];
|
|
101
85
|
}
|
|
102
86
|
|
|
103
87
|
exports.useField = useField;
|
package/hooks.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
1
2
|
import { createFormContext } from '@conform-to/dom';
|
|
2
3
|
import { useState, useRef, useEffect, useLayoutEffect, useId } from 'react';
|
|
3
4
|
import { useSubjectRef, useFormState, getFormMetadata, useFormContext, getFieldMetadata } from './context.mjs';
|
|
@@ -26,13 +27,9 @@ function useNoValidate() {
|
|
|
26
27
|
}
|
|
27
28
|
function useForm(options) {
|
|
28
29
|
var formId = useFormId(options.id);
|
|
29
|
-
var
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// If id changes, reinitialize the form immediately
|
|
33
|
-
if (formId !== context.formId) {
|
|
34
|
-
setFormContext(initializeContext);
|
|
35
|
-
}
|
|
30
|
+
var [context] = useState(() => createFormContext(_objectSpread2(_objectSpread2({}, options), {}, {
|
|
31
|
+
formId
|
|
32
|
+
})));
|
|
36
33
|
var optionsRef = useRef(options);
|
|
37
34
|
useSafeLayoutEffect(() => {
|
|
38
35
|
document.addEventListener('input', context.input);
|
|
@@ -51,49 +48,36 @@ function useForm(options) {
|
|
|
51
48
|
}
|
|
52
49
|
if (options.lastResult) {
|
|
53
50
|
context.report(options.lastResult);
|
|
54
|
-
} else {
|
|
55
|
-
var _document$forms$named;
|
|
56
|
-
(_document$forms$named = document.forms.namedItem(context.formId)) === null || _document$forms$named === void 0 || _document$forms$named.reset();
|
|
57
51
|
}
|
|
58
52
|
}, [context, options.lastResult]);
|
|
59
53
|
useSafeLayoutEffect(() => {
|
|
60
54
|
optionsRef.current = options;
|
|
61
|
-
context.update(options)
|
|
55
|
+
context.update(_objectSpread2(_objectSpread2({}, options), {}, {
|
|
56
|
+
formId
|
|
57
|
+
}));
|
|
62
58
|
});
|
|
63
59
|
var subjectRef = useSubjectRef();
|
|
64
60
|
var state = useFormState(context, subjectRef);
|
|
65
61
|
var noValidate = useNoValidate(options.defaultNoValidate);
|
|
66
|
-
var
|
|
67
|
-
return
|
|
68
|
-
meta,
|
|
69
|
-
fields: meta.getFieldset()
|
|
70
|
-
};
|
|
62
|
+
var form = getFormMetadata(formId, state, subjectRef, context, noValidate);
|
|
63
|
+
return [form, form.getFieldset()];
|
|
71
64
|
}
|
|
72
|
-
function useFormMetadata(
|
|
65
|
+
function useFormMetadata(formId) {
|
|
66
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
73
67
|
var subjectRef = useSubjectRef();
|
|
74
|
-
var context = useFormContext(
|
|
68
|
+
var context = useFormContext(formId);
|
|
75
69
|
var state = useFormState(context, subjectRef);
|
|
76
70
|
var noValidate = useNoValidate(options.defaultNoValidate);
|
|
77
|
-
return getFormMetadata(
|
|
71
|
+
return getFormMetadata(context.formId, state, subjectRef, context, noValidate);
|
|
78
72
|
}
|
|
79
|
-
function useField(
|
|
73
|
+
function useField(name) {
|
|
74
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
80
75
|
var subjectRef = useSubjectRef();
|
|
81
76
|
var context = useFormContext(options.formId);
|
|
82
77
|
var state = useFormState(context, subjectRef);
|
|
83
|
-
var
|
|
84
|
-
var form = getFormMetadata(
|
|
85
|
-
return
|
|
86
|
-
meta,
|
|
87
|
-
// @ts-expect-error The types is used as a hint only
|
|
88
|
-
get fields() {
|
|
89
|
-
return meta.getFieldset();
|
|
90
|
-
},
|
|
91
|
-
// @ts-expect-error The types is used as a hint only
|
|
92
|
-
get list() {
|
|
93
|
-
return meta.getFieldList();
|
|
94
|
-
},
|
|
95
|
-
form
|
|
96
|
-
};
|
|
78
|
+
var field = getFieldMetadata(context.formId, state, subjectRef, name);
|
|
79
|
+
var form = getFormMetadata(context.formId, state, subjectRef, context, false);
|
|
80
|
+
return [field, form];
|
|
97
81
|
}
|
|
98
82
|
|
|
99
83
|
export { useField, useForm, useFormId, useFormMetadata, useNoValidate, useSafeLayoutEffect };
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { type Submission, type SubmissionResult, type
|
|
2
|
-
export { type
|
|
1
|
+
export { type Submission, type SubmissionResult, type FormControl, type FormId, type FieldName, requestSubmit, isFieldElement, control, } from '@conform-to/dom';
|
|
2
|
+
export { type FieldMetadata, type FormMetadata, FormProvider, FormStateInput, } from './context';
|
|
3
3
|
export { useForm, useFormMetadata, useField } from './hooks';
|
|
4
4
|
export { useInputControl } from './integrations';
|
|
5
5
|
export { validateConstraint } from './validitystate';
|
|
6
|
-
export { getFormProps, getFieldsetProps, getInputProps, getSelectProps, getTextareaProps, getCollectionProps,
|
|
6
|
+
export { getFormProps, getFieldsetProps, getInputProps, getSelectProps, getTextareaProps, getCollectionProps, } from './helpers';
|
package/index.js
CHANGED
|
@@ -11,18 +11,14 @@ var helpers = require('./helpers.js');
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
Object.defineProperty(exports, '
|
|
14
|
+
Object.defineProperty(exports, 'control', {
|
|
15
15
|
enumerable: true,
|
|
16
|
-
get: function () { return dom.
|
|
16
|
+
get: function () { return dom.control; }
|
|
17
17
|
});
|
|
18
18
|
Object.defineProperty(exports, 'isFieldElement', {
|
|
19
19
|
enumerable: true,
|
|
20
20
|
get: function () { return dom.isFieldElement; }
|
|
21
21
|
});
|
|
22
|
-
Object.defineProperty(exports, 'requestIntent', {
|
|
23
|
-
enumerable: true,
|
|
24
|
-
get: function () { return dom.requestIntent; }
|
|
25
|
-
});
|
|
26
22
|
Object.defineProperty(exports, 'requestSubmit', {
|
|
27
23
|
enumerable: true,
|
|
28
24
|
get: function () { return dom.requestSubmit; }
|
|
@@ -35,7 +31,6 @@ exports.useFormMetadata = hooks.useFormMetadata;
|
|
|
35
31
|
exports.useInputControl = integrations.useInputControl;
|
|
36
32
|
exports.validateConstraint = validitystate.validateConstraint;
|
|
37
33
|
exports.getCollectionProps = helpers.getCollectionProps;
|
|
38
|
-
exports.getControlButtonProps = helpers.getControlButtonProps;
|
|
39
34
|
exports.getFieldsetProps = helpers.getFieldsetProps;
|
|
40
35
|
exports.getFormProps = helpers.getFormProps;
|
|
41
36
|
exports.getInputProps = helpers.getInputProps;
|
package/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { control, isFieldElement, requestSubmit } from '@conform-to/dom';
|
|
2
2
|
export { FormProvider, FormStateInput } from './context.mjs';
|
|
3
3
|
export { useField, useForm, useFormMetadata } from './hooks.mjs';
|
|
4
4
|
export { useInputControl } from './integrations.mjs';
|
|
5
5
|
export { validateConstraint } from './validitystate.mjs';
|
|
6
|
-
export { getCollectionProps,
|
|
6
|
+
export { getCollectionProps, getFieldsetProps, getFormProps, getInputProps, getSelectProps, getTextareaProps } from './helpers.mjs';
|
package/integrations.d.ts
CHANGED
|
@@ -1,34 +1,15 @@
|
|
|
1
|
-
import { type FieldElement
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value:
|
|
5
|
-
change: (value: Value) => void;
|
|
1
|
+
import { type FieldElement } from '@conform-to/dom';
|
|
2
|
+
export type InputControl = {
|
|
3
|
+
value: string | undefined;
|
|
4
|
+
change: (value: string) => void;
|
|
6
5
|
focus: () => void;
|
|
7
6
|
blur: () => void;
|
|
8
7
|
};
|
|
9
8
|
export declare function getFieldElement(formId: string, name: string, match?: (element: FieldElement) => boolean): FieldElement | null;
|
|
10
9
|
export declare function getEventTarget(formId: string, name: string): FieldElement;
|
|
11
|
-
export declare function useInputControl
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
formId: FormId<any, any>;
|
|
18
|
-
initialValue: FormValue<Schema>;
|
|
19
|
-
onFocus?: (event: Event) => void;
|
|
20
|
-
}): InputControl<string | undefined>;
|
|
21
|
-
export declare function useInputControl<Schema, Value>(metadata: FieldMetadata<Schema, any, any>, options: {
|
|
22
|
-
initialize: (value: FormValue<Schema> | undefined) => Value;
|
|
23
|
-
serialize?: (value: Value) => string;
|
|
24
|
-
onFocus?: (event: Event) => void;
|
|
25
|
-
}): InputControl<Value>;
|
|
26
|
-
export declare function useInputControl<Schema, Value>(options: {
|
|
27
|
-
key?: string;
|
|
28
|
-
name: FieldName<Schema>;
|
|
29
|
-
formId: FormId<any, any>;
|
|
30
|
-
initialValue: FormValue<Schema>;
|
|
31
|
-
initialize: (value: FormValue<Schema> | undefined) => Value;
|
|
32
|
-
serialize?: (value: Value) => string;
|
|
33
|
-
onFocus?: (event: Event) => void;
|
|
34
|
-
}): InputControl<Value>;
|
|
10
|
+
export declare function useInputControl(options: {
|
|
11
|
+
key?: string | undefined;
|
|
12
|
+
name: string;
|
|
13
|
+
formId: string;
|
|
14
|
+
initialValue: string | undefined;
|
|
15
|
+
}): InputControl;
|
package/integrations.js
CHANGED
|
@@ -32,43 +32,23 @@ function getEventTarget(formId, name) {
|
|
|
32
32
|
form === null || form === void 0 || form.appendChild(input);
|
|
33
33
|
return input;
|
|
34
34
|
}
|
|
35
|
-
function useInputControl(
|
|
36
|
-
var _options$initialize, _options$serialize, _options$onFocus;
|
|
35
|
+
function useInputControl(options) {
|
|
37
36
|
var eventDispatched = react.useRef({
|
|
38
37
|
change: false,
|
|
39
38
|
focus: false,
|
|
40
39
|
blur: false
|
|
41
40
|
});
|
|
42
|
-
var [key, setKey] = react.useState(
|
|
43
|
-
var
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
initialize,
|
|
48
|
-
serialize,
|
|
49
|
-
onFocus
|
|
50
|
-
});
|
|
51
|
-
var [value, setValue] = react.useState(() => initialize(metadata.initialValue));
|
|
52
|
-
if (key !== metadata.key) {
|
|
53
|
-
setValue(initialize(metadata.initialValue));
|
|
54
|
-
setKey(metadata.key);
|
|
41
|
+
var [key, setKey] = react.useState(options.key);
|
|
42
|
+
var [value, setValue] = react.useState(() => options.initialValue);
|
|
43
|
+
if (key !== options.key) {
|
|
44
|
+
setValue(options.initialValue);
|
|
45
|
+
setKey(options.key);
|
|
55
46
|
}
|
|
56
|
-
react.useEffect(() => {
|
|
57
|
-
optionsRef.current = {
|
|
58
|
-
initialize,
|
|
59
|
-
serialize,
|
|
60
|
-
onFocus
|
|
61
|
-
};
|
|
62
|
-
});
|
|
63
47
|
react.useEffect(() => {
|
|
64
48
|
var createEventListener = listener => {
|
|
65
49
|
return event => {
|
|
66
|
-
var element = getFieldElement(
|
|
50
|
+
var element = getFieldElement(options.formId, options.name, element => element === event.target);
|
|
67
51
|
if (element) {
|
|
68
|
-
if (listener === 'focus') {
|
|
69
|
-
var _optionsRef$current, _optionsRef$current$o;
|
|
70
|
-
(_optionsRef$current = optionsRef.current) === null || _optionsRef$current === void 0 || (_optionsRef$current$o = _optionsRef$current.onFocus) === null || _optionsRef$current$o === void 0 || _optionsRef$current$o.call(_optionsRef$current, event);
|
|
71
|
-
}
|
|
72
52
|
eventDispatched.current[listener] = true;
|
|
73
53
|
}
|
|
74
54
|
};
|
|
@@ -84,62 +64,57 @@ function useInputControl(metadata, options) {
|
|
|
84
64
|
document.removeEventListener('focusin', focusHandler, true);
|
|
85
65
|
document.removeEventListener('focusout', blurHandler, true);
|
|
86
66
|
};
|
|
87
|
-
}, [
|
|
67
|
+
}, [options.formId, options.name]);
|
|
88
68
|
var handlers = react.useMemo(() => {
|
|
89
69
|
return {
|
|
90
70
|
change(value) {
|
|
91
71
|
if (!eventDispatched.current.change) {
|
|
92
|
-
var
|
|
93
|
-
var _element = getEventTarget(metadata.formId, metadata.name);
|
|
94
|
-
var serializedValue = (_ref = (_optionsRef$current$s = (_optionsRef$current$s2 = (_optionsRef$current2 = optionsRef.current).serialize) === null || _optionsRef$current$s2 === void 0 ? void 0 : _optionsRef$current$s2.call(_optionsRef$current2, value)) !== null && _optionsRef$current$s !== void 0 ? _optionsRef$current$s : value === null || value === void 0 ? void 0 : value.toString()) !== null && _ref !== void 0 ? _ref : '';
|
|
72
|
+
var _element = getEventTarget(options.formId, options.name);
|
|
95
73
|
eventDispatched.current.change = true;
|
|
96
74
|
if (_element instanceof HTMLInputElement && (_element.type === 'checkbox' || _element.type === 'radio')) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
} else {
|
|
75
|
+
_element.checked = _element.value === value;
|
|
76
|
+
} else if (_element.value !== value) {
|
|
101
77
|
// No change event will be triggered on React if `element.value` is already updated
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Triggering react custom change event
|
|
81
|
+
* Solution based on dom-testing-library
|
|
82
|
+
* @see https://github.com/facebook/react/issues/10135#issuecomment-401496776
|
|
83
|
+
* @see https://github.com/testing-library/dom-testing-library/blob/main/src/events.js#L104-L123
|
|
84
|
+
*/
|
|
85
|
+
var {
|
|
86
|
+
set: valueSetter
|
|
87
|
+
} = Object.getOwnPropertyDescriptor(_element, 'value') || {};
|
|
88
|
+
var prototype = Object.getPrototypeOf(_element);
|
|
89
|
+
var {
|
|
90
|
+
set: prototypeValueSetter
|
|
91
|
+
} = Object.getOwnPropertyDescriptor(prototype, 'value') || {};
|
|
92
|
+
if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
|
|
93
|
+
prototypeValueSetter.call(_element, value);
|
|
94
|
+
} else {
|
|
95
|
+
if (valueSetter) {
|
|
96
|
+
valueSetter.call(_element, value);
|
|
118
97
|
} else {
|
|
119
|
-
|
|
120
|
-
valueSetter.call(_element, value);
|
|
121
|
-
} else {
|
|
122
|
-
throw new Error('The given element does not have a value setter');
|
|
123
|
-
}
|
|
98
|
+
throw new Error('The given element does not have a value setter');
|
|
124
99
|
}
|
|
125
100
|
}
|
|
126
|
-
|
|
127
|
-
// Dispatch input event with the updated input value
|
|
128
|
-
_element.dispatchEvent(new InputEvent('input', {
|
|
129
|
-
bubbles: true
|
|
130
|
-
}));
|
|
131
|
-
// Dispatch change event (necessary for select to update the selected option)
|
|
132
|
-
_element.dispatchEvent(new Event('change', {
|
|
133
|
-
bubbles: true
|
|
134
|
-
}));
|
|
135
101
|
}
|
|
102
|
+
|
|
103
|
+
// Dispatch input event with the updated input value
|
|
104
|
+
_element.dispatchEvent(new InputEvent('input', {
|
|
105
|
+
bubbles: true
|
|
106
|
+
}));
|
|
107
|
+
// Dispatch change event (necessary for select to update the selected option)
|
|
108
|
+
_element.dispatchEvent(new Event('change', {
|
|
109
|
+
bubbles: true
|
|
110
|
+
}));
|
|
136
111
|
}
|
|
137
112
|
setValue(value);
|
|
138
113
|
eventDispatched.current.change = false;
|
|
139
114
|
},
|
|
140
115
|
focus() {
|
|
141
116
|
if (!eventDispatched.current.focus) {
|
|
142
|
-
var _element2 = getEventTarget(
|
|
117
|
+
var _element2 = getEventTarget(options.formId, options.name);
|
|
143
118
|
eventDispatched.current.focus = true;
|
|
144
119
|
_element2.dispatchEvent(new FocusEvent('focusin', {
|
|
145
120
|
bubbles: true
|
|
@@ -150,7 +125,7 @@ function useInputControl(metadata, options) {
|
|
|
150
125
|
},
|
|
151
126
|
blur() {
|
|
152
127
|
if (!eventDispatched.current.blur) {
|
|
153
|
-
var _element3 = getEventTarget(
|
|
128
|
+
var _element3 = getEventTarget(options.formId, options.name);
|
|
154
129
|
eventDispatched.current.blur = true;
|
|
155
130
|
_element3.dispatchEvent(new FocusEvent('focusout', {
|
|
156
131
|
bubbles: true
|
|
@@ -160,7 +135,7 @@ function useInputControl(metadata, options) {
|
|
|
160
135
|
eventDispatched.current.blur = false;
|
|
161
136
|
}
|
|
162
137
|
};
|
|
163
|
-
}, [
|
|
138
|
+
}, [options.formId, options.name]);
|
|
164
139
|
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, handlers), {}, {
|
|
165
140
|
value
|
|
166
141
|
});
|
package/integrations.mjs
CHANGED
|
@@ -28,43 +28,23 @@ function getEventTarget(formId, name) {
|
|
|
28
28
|
form === null || form === void 0 || form.appendChild(input);
|
|
29
29
|
return input;
|
|
30
30
|
}
|
|
31
|
-
function useInputControl(
|
|
32
|
-
var _options$initialize, _options$serialize, _options$onFocus;
|
|
31
|
+
function useInputControl(options) {
|
|
33
32
|
var eventDispatched = useRef({
|
|
34
33
|
change: false,
|
|
35
34
|
focus: false,
|
|
36
35
|
blur: false
|
|
37
36
|
});
|
|
38
|
-
var [key, setKey] = useState(
|
|
39
|
-
var
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
initialize,
|
|
44
|
-
serialize,
|
|
45
|
-
onFocus
|
|
46
|
-
});
|
|
47
|
-
var [value, setValue] = useState(() => initialize(metadata.initialValue));
|
|
48
|
-
if (key !== metadata.key) {
|
|
49
|
-
setValue(initialize(metadata.initialValue));
|
|
50
|
-
setKey(metadata.key);
|
|
37
|
+
var [key, setKey] = useState(options.key);
|
|
38
|
+
var [value, setValue] = useState(() => options.initialValue);
|
|
39
|
+
if (key !== options.key) {
|
|
40
|
+
setValue(options.initialValue);
|
|
41
|
+
setKey(options.key);
|
|
51
42
|
}
|
|
52
|
-
useEffect(() => {
|
|
53
|
-
optionsRef.current = {
|
|
54
|
-
initialize,
|
|
55
|
-
serialize,
|
|
56
|
-
onFocus
|
|
57
|
-
};
|
|
58
|
-
});
|
|
59
43
|
useEffect(() => {
|
|
60
44
|
var createEventListener = listener => {
|
|
61
45
|
return event => {
|
|
62
|
-
var element = getFieldElement(
|
|
46
|
+
var element = getFieldElement(options.formId, options.name, element => element === event.target);
|
|
63
47
|
if (element) {
|
|
64
|
-
if (listener === 'focus') {
|
|
65
|
-
var _optionsRef$current, _optionsRef$current$o;
|
|
66
|
-
(_optionsRef$current = optionsRef.current) === null || _optionsRef$current === void 0 || (_optionsRef$current$o = _optionsRef$current.onFocus) === null || _optionsRef$current$o === void 0 || _optionsRef$current$o.call(_optionsRef$current, event);
|
|
67
|
-
}
|
|
68
48
|
eventDispatched.current[listener] = true;
|
|
69
49
|
}
|
|
70
50
|
};
|
|
@@ -80,62 +60,57 @@ function useInputControl(metadata, options) {
|
|
|
80
60
|
document.removeEventListener('focusin', focusHandler, true);
|
|
81
61
|
document.removeEventListener('focusout', blurHandler, true);
|
|
82
62
|
};
|
|
83
|
-
}, [
|
|
63
|
+
}, [options.formId, options.name]);
|
|
84
64
|
var handlers = useMemo(() => {
|
|
85
65
|
return {
|
|
86
66
|
change(value) {
|
|
87
67
|
if (!eventDispatched.current.change) {
|
|
88
|
-
var
|
|
89
|
-
var _element = getEventTarget(metadata.formId, metadata.name);
|
|
90
|
-
var serializedValue = (_ref = (_optionsRef$current$s = (_optionsRef$current$s2 = (_optionsRef$current2 = optionsRef.current).serialize) === null || _optionsRef$current$s2 === void 0 ? void 0 : _optionsRef$current$s2.call(_optionsRef$current2, value)) !== null && _optionsRef$current$s !== void 0 ? _optionsRef$current$s : value === null || value === void 0 ? void 0 : value.toString()) !== null && _ref !== void 0 ? _ref : '';
|
|
68
|
+
var _element = getEventTarget(options.formId, options.name);
|
|
91
69
|
eventDispatched.current.change = true;
|
|
92
70
|
if (_element instanceof HTMLInputElement && (_element.type === 'checkbox' || _element.type === 'radio')) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
71
|
+
_element.checked = _element.value === value;
|
|
72
|
+
} else if (_element.value !== value) {
|
|
97
73
|
// No change event will be triggered on React if `element.value` is already updated
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Triggering react custom change event
|
|
77
|
+
* Solution based on dom-testing-library
|
|
78
|
+
* @see https://github.com/facebook/react/issues/10135#issuecomment-401496776
|
|
79
|
+
* @see https://github.com/testing-library/dom-testing-library/blob/main/src/events.js#L104-L123
|
|
80
|
+
*/
|
|
81
|
+
var {
|
|
82
|
+
set: valueSetter
|
|
83
|
+
} = Object.getOwnPropertyDescriptor(_element, 'value') || {};
|
|
84
|
+
var prototype = Object.getPrototypeOf(_element);
|
|
85
|
+
var {
|
|
86
|
+
set: prototypeValueSetter
|
|
87
|
+
} = Object.getOwnPropertyDescriptor(prototype, 'value') || {};
|
|
88
|
+
if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
|
|
89
|
+
prototypeValueSetter.call(_element, value);
|
|
90
|
+
} else {
|
|
91
|
+
if (valueSetter) {
|
|
92
|
+
valueSetter.call(_element, value);
|
|
114
93
|
} else {
|
|
115
|
-
|
|
116
|
-
valueSetter.call(_element, value);
|
|
117
|
-
} else {
|
|
118
|
-
throw new Error('The given element does not have a value setter');
|
|
119
|
-
}
|
|
94
|
+
throw new Error('The given element does not have a value setter');
|
|
120
95
|
}
|
|
121
96
|
}
|
|
122
|
-
|
|
123
|
-
// Dispatch input event with the updated input value
|
|
124
|
-
_element.dispatchEvent(new InputEvent('input', {
|
|
125
|
-
bubbles: true
|
|
126
|
-
}));
|
|
127
|
-
// Dispatch change event (necessary for select to update the selected option)
|
|
128
|
-
_element.dispatchEvent(new Event('change', {
|
|
129
|
-
bubbles: true
|
|
130
|
-
}));
|
|
131
97
|
}
|
|
98
|
+
|
|
99
|
+
// Dispatch input event with the updated input value
|
|
100
|
+
_element.dispatchEvent(new InputEvent('input', {
|
|
101
|
+
bubbles: true
|
|
102
|
+
}));
|
|
103
|
+
// Dispatch change event (necessary for select to update the selected option)
|
|
104
|
+
_element.dispatchEvent(new Event('change', {
|
|
105
|
+
bubbles: true
|
|
106
|
+
}));
|
|
132
107
|
}
|
|
133
108
|
setValue(value);
|
|
134
109
|
eventDispatched.current.change = false;
|
|
135
110
|
},
|
|
136
111
|
focus() {
|
|
137
112
|
if (!eventDispatched.current.focus) {
|
|
138
|
-
var _element2 = getEventTarget(
|
|
113
|
+
var _element2 = getEventTarget(options.formId, options.name);
|
|
139
114
|
eventDispatched.current.focus = true;
|
|
140
115
|
_element2.dispatchEvent(new FocusEvent('focusin', {
|
|
141
116
|
bubbles: true
|
|
@@ -146,7 +121,7 @@ function useInputControl(metadata, options) {
|
|
|
146
121
|
},
|
|
147
122
|
blur() {
|
|
148
123
|
if (!eventDispatched.current.blur) {
|
|
149
|
-
var _element3 = getEventTarget(
|
|
124
|
+
var _element3 = getEventTarget(options.formId, options.name);
|
|
150
125
|
eventDispatched.current.blur = true;
|
|
151
126
|
_element3.dispatchEvent(new FocusEvent('focusout', {
|
|
152
127
|
bubbles: true
|
|
@@ -156,7 +131,7 @@ function useInputControl(metadata, options) {
|
|
|
156
131
|
eventDispatched.current.blur = false;
|
|
157
132
|
}
|
|
158
133
|
};
|
|
159
|
-
}, [
|
|
134
|
+
}, [options.formId, options.name]);
|
|
160
135
|
return _objectSpread2(_objectSpread2({}, handlers), {}, {
|
|
161
136
|
value
|
|
162
137
|
});
|
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.0.0-pre.
|
|
6
|
+
"version": "1.0.0-pre.5",
|
|
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.0.0-pre.
|
|
33
|
+
"@conform-to/dom": "1.0.0-pre.5"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/react": "^18.2.43",
|