@conform-to/react 1.0.0-rc.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README +3 -3
- package/context.d.ts +19 -9
- package/context.js +45 -12
- package/context.mjs +47 -15
- package/helpers.d.ts +1 -1
- package/helpers.js +13 -17
- package/helpers.mjs +13 -17
- package/hooks.d.ts +2 -2
- package/hooks.js +1 -2
- package/hooks.mjs +2 -3
- package/index.d.ts +1 -1
- package/package.json +2 -2
package/README
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
╚══════╝ ╚═════╝ ╚═╝ ╚══╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
Version 1.0.0-rc.
|
|
11
|
+
Version 1.0.0-rc.1 / License MIT / Copyright (c) 2024 Edmund Hung
|
|
12
12
|
|
|
13
|
-
A type-safe form validation library utilizing web fundamentals to progressively enhance HTML Forms with full support for server frameworks like Remix
|
|
13
|
+
A type-safe form validation library utilizing web fundamentals to progressively enhance HTML Forms with full support for server frameworks like Remix and Next.js.
|
|
14
14
|
|
|
15
15
|
> Getting Started
|
|
16
16
|
|
|
@@ -21,7 +21,7 @@ Check out the overview and tutorial at our website https://conform.guide
|
|
|
21
21
|
The documentation is divided into several sections:
|
|
22
22
|
|
|
23
23
|
* Overview: https://conform.guide/overview
|
|
24
|
-
*
|
|
24
|
+
* Examples: https://conform.guide/examples
|
|
25
25
|
* Complex structures: https://conform.guide/complex-structures
|
|
26
26
|
* UI Integrations: https://conform.guide/integrations
|
|
27
27
|
* Accessibility Guide: https://conform.guide/accessibility
|
package/context.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type Constraint, type FormId, type FieldName, type FormContext, type FormValue, type FormState, type Intent, type SubscriptionScope, type SubscriptionSubject, type UnionKeyof, type UnionKeyType } from '@conform-to/dom';
|
|
2
|
-
import { type ReactElement, type ReactNode, type MutableRefObject } from 'react';
|
|
1
|
+
import { type Constraint, type FormId, type FieldName, type FormContext as BaseFormContext, type FormValue, type FormState, type Intent, type SubscriptionScope, type SubscriptionSubject, type UnionKeyof, type UnionKeyType, type FormOptions as BaseFormOptions } from '@conform-to/dom';
|
|
2
|
+
import { type FormEvent, type ReactElement, type ReactNode, type MutableRefObject } from 'react';
|
|
3
3
|
export type Pretty<T> = {
|
|
4
4
|
[K in keyof T]: T[K];
|
|
5
5
|
} & {};
|
|
@@ -27,24 +27,23 @@ export type FormMetadata<Schema extends Record<string, unknown> = Record<string,
|
|
|
27
27
|
onSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
|
|
28
28
|
noValidate: boolean;
|
|
29
29
|
};
|
|
30
|
-
export type FieldMetadata<Schema = unknown, FormSchema extends Record<string, any> = Record<string, unknown>, FormError = string[]> = Metadata<Schema, FormSchema, FormError> & {
|
|
30
|
+
export type FieldMetadata<Schema = unknown, FormSchema extends Record<string, any> = Record<string, unknown>, FormError = string[]> = Metadata<Schema, FormSchema, FormError> & Constraint & {
|
|
31
31
|
formId: FormId<FormSchema, FormError>;
|
|
32
|
-
constraint?: Constraint;
|
|
33
32
|
getFieldset: unknown extends Schema ? () => unknown : Schema extends Primitive | Array<any> ? never : () => {
|
|
34
33
|
[Key in UnionKeyof<Schema>]: FieldMetadata<UnionKeyType<Schema, Key>, FormSchema, FormError>;
|
|
35
34
|
};
|
|
36
35
|
getFieldList: unknown extends Schema ? () => unknown : Schema extends Array<infer Item> ? () => Array<FieldMetadata<Item, FormSchema, FormError>> : never;
|
|
37
36
|
};
|
|
38
|
-
export declare const Form: import("react").Context<FormContext[]>;
|
|
37
|
+
export declare const Form: import("react").Context<FormContext<any, string[], any>[]>;
|
|
39
38
|
declare const wrappedSymbol: unique symbol;
|
|
40
39
|
export type Wrapped<Type> = {
|
|
41
40
|
[wrappedSymbol]: Type;
|
|
42
41
|
};
|
|
43
42
|
export declare function getWrappedFormContext(context: Wrapped<FormContext>): FormContext;
|
|
44
|
-
export declare function useFormContext<Schema extends Record<string, any>, FormError>(formId?: FormId<Schema, FormError>): FormContext<Schema,
|
|
45
|
-
export declare function useFormState<FormError>(form: FormContext<any,
|
|
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>;
|
|
46
45
|
export declare function FormProvider(props: {
|
|
47
|
-
context: Wrapped<FormContext<any, any
|
|
46
|
+
context: Wrapped<FormContext<any, any>>;
|
|
48
47
|
children: ReactNode;
|
|
49
48
|
}): ReactElement;
|
|
50
49
|
export declare function FormStateInput(props: {
|
|
@@ -54,5 +53,16 @@ export declare function useSubjectRef(initialSubject?: SubscriptionSubject): Mut
|
|
|
54
53
|
export declare function updateSubjectRef(ref: MutableRefObject<SubscriptionSubject>, name: string, subject: keyof SubscriptionSubject, scope: keyof SubscriptionScope): void;
|
|
55
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, FormSchema, FormError>): Metadata<Schema, FormSchema, FormError>;
|
|
56
55
|
export declare function getFieldMetadata<Schema, FormSchema extends Record<string, any>, FormError>(formId: FormId<FormSchema, FormError>, state: FormState<FormError>, subjectRef: MutableRefObject<SubscriptionSubject>, prefix?: string, key?: string | number): FieldMetadata<Schema, FormSchema, FormError>;
|
|
57
|
-
export declare function getFormMetadata<Schema extends Record<string, any>,
|
|
56
|
+
export declare function getFormMetadata<Schema extends Record<string, any>, FormError = string[], FormValue = Schema>(formId: FormId<Schema, FormError>, state: FormState<FormError>, subjectRef: MutableRefObject<SubscriptionSubject>, context: FormContext<Schema, FormError, FormValue>, noValidate: boolean): FormMetadata<Schema, FormError>;
|
|
57
|
+
export type FormOptions<Schema extends Record<string, any> = any, FormError = string[], FormValue = Schema> = BaseFormOptions<Schema, FormError, FormValue> & {
|
|
58
|
+
/**
|
|
59
|
+
* A function to be called before the form is submitted.
|
|
60
|
+
*/
|
|
61
|
+
onSubmit?: (event: FormEvent<HTMLFormElement>, context: ReturnType<BaseFormContext<Schema, FormError, FormValue>['submit']>) => void;
|
|
62
|
+
};
|
|
63
|
+
export type FormContext<Schema extends Record<string, any> = any, FormError = string[], FormValue = Schema> = Omit<BaseFormContext<Schema, FormError, FormValue>, 'submit' | 'onUpdate'> & {
|
|
64
|
+
submit: (event: FormEvent<HTMLFormElement>) => void;
|
|
65
|
+
onUpdate: (options: Partial<FormOptions<Schema, FormError, FormValue>>) => void;
|
|
66
|
+
};
|
|
67
|
+
export declare function createFormContext<Schema extends Record<string, any> = any, FormError = string[], FormValue = Schema>(options: FormOptions<Schema, FormError, FormValue>): FormContext<Schema, FormError, FormValue>;
|
|
58
68
|
export {};
|
package/context.js
CHANGED
|
@@ -7,6 +7,7 @@ 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"];
|
|
10
11
|
var Form = /*#__PURE__*/react.createContext([]);
|
|
11
12
|
|
|
12
13
|
// To hide the FormContext type from the public API
|
|
@@ -73,9 +74,15 @@ function getMetadata(formId, state, subjectRef) {
|
|
|
73
74
|
name,
|
|
74
75
|
errorId: "".concat(id, "-error"),
|
|
75
76
|
descriptionId: "".concat(id, "-description"),
|
|
76
|
-
initialValue
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
get initialValue() {
|
|
78
|
+
return state.initialValue[name];
|
|
79
|
+
},
|
|
80
|
+
get value() {
|
|
81
|
+
return state.value[name];
|
|
82
|
+
},
|
|
83
|
+
get errors() {
|
|
84
|
+
return state.error[name];
|
|
85
|
+
},
|
|
79
86
|
get key() {
|
|
80
87
|
return state.key[name];
|
|
81
88
|
},
|
|
@@ -133,11 +140,19 @@ function getFieldMetadata(formId, state, subjectRef) {
|
|
|
133
140
|
var metadata = getMetadata(formId, state, subjectRef, name);
|
|
134
141
|
return new Proxy({}, {
|
|
135
142
|
get(_, key, receiver) {
|
|
143
|
+
var _state$constraint$nam;
|
|
136
144
|
switch (key) {
|
|
137
145
|
case 'formId':
|
|
138
146
|
return formId;
|
|
139
|
-
case '
|
|
140
|
-
|
|
147
|
+
case 'required':
|
|
148
|
+
case 'minLength':
|
|
149
|
+
case 'maxLength':
|
|
150
|
+
case 'min':
|
|
151
|
+
case 'max':
|
|
152
|
+
case 'pattern':
|
|
153
|
+
case 'step':
|
|
154
|
+
case 'multiple':
|
|
155
|
+
return (_state$constraint$nam = state.constraint[name]) === null || _state$constraint$nam === void 0 ? void 0 : _state$constraint$nam[key];
|
|
141
156
|
case 'getFieldList':
|
|
142
157
|
{
|
|
143
158
|
return () => {
|
|
@@ -174,13 +189,7 @@ function getFormMetadata(formId, state, subjectRef, context, noValidate) {
|
|
|
174
189
|
case 'reorder':
|
|
175
190
|
return context[key];
|
|
176
191
|
case 'onSubmit':
|
|
177
|
-
return
|
|
178
|
-
var submitEvent = event.nativeEvent;
|
|
179
|
-
var submission = context.submit(submitEvent);
|
|
180
|
-
if (submission && submission.status !== 'success' && submission.error !== null) {
|
|
181
|
-
event.preventDefault();
|
|
182
|
-
}
|
|
183
|
-
};
|
|
192
|
+
return context.submit;
|
|
184
193
|
case 'noValidate':
|
|
185
194
|
return noValidate;
|
|
186
195
|
}
|
|
@@ -188,10 +197,34 @@ function getFormMetadata(formId, state, subjectRef, context, noValidate) {
|
|
|
188
197
|
}
|
|
189
198
|
});
|
|
190
199
|
}
|
|
200
|
+
function createFormContext(options) {
|
|
201
|
+
var {
|
|
202
|
+
onSubmit
|
|
203
|
+
} = options,
|
|
204
|
+
rest = _rollupPluginBabelHelpers.objectWithoutProperties(options, _excluded);
|
|
205
|
+
var context = dom.unstable_createFormContext(rest);
|
|
206
|
+
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, context), {}, {
|
|
207
|
+
submit(event) {
|
|
208
|
+
var submitEvent = event.nativeEvent;
|
|
209
|
+
var result = context.submit(submitEvent);
|
|
210
|
+
if (result.submission && result.submission.status !== 'success' && result.submission.error !== null) {
|
|
211
|
+
event.preventDefault();
|
|
212
|
+
} else {
|
|
213
|
+
var _onSubmit;
|
|
214
|
+
(_onSubmit = onSubmit) === null || _onSubmit === void 0 || _onSubmit(event, result);
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
onUpdate(options) {
|
|
218
|
+
onSubmit = options.onSubmit;
|
|
219
|
+
context.onUpdate(options);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
191
223
|
|
|
192
224
|
exports.Form = Form;
|
|
193
225
|
exports.FormProvider = FormProvider;
|
|
194
226
|
exports.FormStateInput = FormStateInput;
|
|
227
|
+
exports.createFormContext = createFormContext;
|
|
195
228
|
exports.getFieldMetadata = getFieldMetadata;
|
|
196
229
|
exports.getFormMetadata = getFormMetadata;
|
|
197
230
|
exports.getMetadata = getMetadata;
|
package/context.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
-
import { STATE, formatPaths, getPaths, isPrefix } from '@conform-to/dom';
|
|
1
|
+
import { objectWithoutProperties as _objectWithoutProperties, objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
+
import { STATE, formatPaths, getPaths, unstable_createFormContext, 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"];
|
|
6
7
|
var Form = /*#__PURE__*/createContext([]);
|
|
7
8
|
|
|
8
9
|
// To hide the FormContext type from the public API
|
|
@@ -69,9 +70,15 @@ function getMetadata(formId, state, subjectRef) {
|
|
|
69
70
|
name,
|
|
70
71
|
errorId: "".concat(id, "-error"),
|
|
71
72
|
descriptionId: "".concat(id, "-description"),
|
|
72
|
-
initialValue
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
get initialValue() {
|
|
74
|
+
return state.initialValue[name];
|
|
75
|
+
},
|
|
76
|
+
get value() {
|
|
77
|
+
return state.value[name];
|
|
78
|
+
},
|
|
79
|
+
get errors() {
|
|
80
|
+
return state.error[name];
|
|
81
|
+
},
|
|
75
82
|
get key() {
|
|
76
83
|
return state.key[name];
|
|
77
84
|
},
|
|
@@ -129,11 +136,19 @@ function getFieldMetadata(formId, state, subjectRef) {
|
|
|
129
136
|
var metadata = getMetadata(formId, state, subjectRef, name);
|
|
130
137
|
return new Proxy({}, {
|
|
131
138
|
get(_, key, receiver) {
|
|
139
|
+
var _state$constraint$nam;
|
|
132
140
|
switch (key) {
|
|
133
141
|
case 'formId':
|
|
134
142
|
return formId;
|
|
135
|
-
case '
|
|
136
|
-
|
|
143
|
+
case 'required':
|
|
144
|
+
case 'minLength':
|
|
145
|
+
case 'maxLength':
|
|
146
|
+
case 'min':
|
|
147
|
+
case 'max':
|
|
148
|
+
case 'pattern':
|
|
149
|
+
case 'step':
|
|
150
|
+
case 'multiple':
|
|
151
|
+
return (_state$constraint$nam = state.constraint[name]) === null || _state$constraint$nam === void 0 ? void 0 : _state$constraint$nam[key];
|
|
137
152
|
case 'getFieldList':
|
|
138
153
|
{
|
|
139
154
|
return () => {
|
|
@@ -170,13 +185,7 @@ function getFormMetadata(formId, state, subjectRef, context, noValidate) {
|
|
|
170
185
|
case 'reorder':
|
|
171
186
|
return context[key];
|
|
172
187
|
case 'onSubmit':
|
|
173
|
-
return
|
|
174
|
-
var submitEvent = event.nativeEvent;
|
|
175
|
-
var submission = context.submit(submitEvent);
|
|
176
|
-
if (submission && submission.status !== 'success' && submission.error !== null) {
|
|
177
|
-
event.preventDefault();
|
|
178
|
-
}
|
|
179
|
-
};
|
|
188
|
+
return context.submit;
|
|
180
189
|
case 'noValidate':
|
|
181
190
|
return noValidate;
|
|
182
191
|
}
|
|
@@ -184,5 +193,28 @@ function getFormMetadata(formId, state, subjectRef, context, noValidate) {
|
|
|
184
193
|
}
|
|
185
194
|
});
|
|
186
195
|
}
|
|
196
|
+
function createFormContext(options) {
|
|
197
|
+
var {
|
|
198
|
+
onSubmit
|
|
199
|
+
} = options,
|
|
200
|
+
rest = _objectWithoutProperties(options, _excluded);
|
|
201
|
+
var context = unstable_createFormContext(rest);
|
|
202
|
+
return _objectSpread2(_objectSpread2({}, context), {}, {
|
|
203
|
+
submit(event) {
|
|
204
|
+
var submitEvent = event.nativeEvent;
|
|
205
|
+
var result = context.submit(submitEvent);
|
|
206
|
+
if (result.submission && result.submission.status !== 'success' && result.submission.error !== null) {
|
|
207
|
+
event.preventDefault();
|
|
208
|
+
} else {
|
|
209
|
+
var _onSubmit;
|
|
210
|
+
(_onSubmit = onSubmit) === null || _onSubmit === void 0 || _onSubmit(event, result);
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
onUpdate(options) {
|
|
214
|
+
onSubmit = options.onSubmit;
|
|
215
|
+
context.onUpdate(options);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
}
|
|
187
219
|
|
|
188
|
-
export { Form, FormProvider, FormStateInput, getFieldMetadata, getFormMetadata, getMetadata, getWrappedFormContext, updateSubjectRef, useFormContext, useFormState, useSubjectRef };
|
|
220
|
+
export { Form, FormProvider, FormStateInput, createFormContext, getFieldMetadata, getFormMetadata, getMetadata, getWrappedFormContext, updateSubjectRef, useFormContext, useFormState, useSubjectRef };
|
package/helpers.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ type FormControlOptions = {
|
|
|
27
27
|
ariaAttributes: false;
|
|
28
28
|
};
|
|
29
29
|
type InputProps = Pretty<FormControlProps & {
|
|
30
|
-
type: 'checkbox' | 'color' | 'date' | 'datetime-local' | 'email' | 'file' | 'hidden' | '
|
|
30
|
+
type: 'checkbox' | 'color' | 'date' | 'datetime-local' | 'email' | 'file' | 'hidden' | 'month' | 'number' | 'password' | 'radio' | 'range' | 'search' | 'tel' | 'text' | 'time' | 'url' | 'week';
|
|
31
31
|
minLength?: number;
|
|
32
32
|
maxLength?: number;
|
|
33
33
|
min?: string | number;
|
package/helpers.js
CHANGED
|
@@ -72,10 +72,9 @@ function getFieldsetProps(metadata, options) {
|
|
|
72
72
|
* including `key`, `id`, `name`, `form`, `required`, `autoFocus`, `aria-invalid` and `aria-describedby`.
|
|
73
73
|
*/
|
|
74
74
|
function getFormControlProps(metadata, options) {
|
|
75
|
-
var _metadata$constraint;
|
|
76
75
|
return simplify(_rollupPluginBabelHelpers.objectSpread2({
|
|
77
76
|
key: metadata.key,
|
|
78
|
-
required:
|
|
77
|
+
required: metadata.required || undefined
|
|
79
78
|
}, getFieldsetProps(metadata, options)));
|
|
80
79
|
}
|
|
81
80
|
|
|
@@ -100,16 +99,15 @@ function getFormControlProps(metadata, options) {
|
|
|
100
99
|
* ```
|
|
101
100
|
*/
|
|
102
101
|
function getInputProps(metadata, options) {
|
|
103
|
-
var _metadata$constraint2, _metadata$constraint3, _metadata$constraint4, _metadata$constraint5, _metadata$constraint6, _metadata$constraint7, _metadata$constraint8;
|
|
104
102
|
var props = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, getFormControlProps(metadata, options)), {}, {
|
|
105
103
|
type: options.type,
|
|
106
|
-
minLength:
|
|
107
|
-
maxLength:
|
|
108
|
-
min:
|
|
109
|
-
max:
|
|
110
|
-
step:
|
|
111
|
-
pattern:
|
|
112
|
-
multiple:
|
|
104
|
+
minLength: metadata.minLength,
|
|
105
|
+
maxLength: metadata.maxLength,
|
|
106
|
+
min: metadata.min,
|
|
107
|
+
max: metadata.max,
|
|
108
|
+
step: metadata.step,
|
|
109
|
+
pattern: metadata.pattern,
|
|
110
|
+
multiple: metadata.multiple
|
|
113
111
|
});
|
|
114
112
|
if (typeof options.value === 'undefined' || options.value) {
|
|
115
113
|
if (options.type === 'checkbox' || options.type === 'radio') {
|
|
@@ -138,10 +136,9 @@ function getInputProps(metadata, options) {
|
|
|
138
136
|
* ```
|
|
139
137
|
*/
|
|
140
138
|
function getSelectProps(metadata) {
|
|
141
|
-
var _metadata$constraint9;
|
|
142
139
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
143
140
|
var props = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, getFormControlProps(metadata, options)), {}, {
|
|
144
|
-
multiple:
|
|
141
|
+
multiple: metadata.multiple
|
|
145
142
|
});
|
|
146
143
|
if (typeof options.value === 'undefined' || options.value) {
|
|
147
144
|
props.defaultValue = Array.isArray(metadata.initialValue) ? metadata.initialValue.map(item => "".concat(item !== null && item !== void 0 ? item : '')) : metadata.initialValue;
|
|
@@ -165,11 +162,10 @@ function getSelectProps(metadata) {
|
|
|
165
162
|
* ```
|
|
166
163
|
*/
|
|
167
164
|
function getTextareaProps(metadata) {
|
|
168
|
-
var _metadata$constraint10, _metadata$constraint11;
|
|
169
165
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
170
166
|
var props = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, getFormControlProps(metadata, options)), {}, {
|
|
171
|
-
minLength:
|
|
172
|
-
maxLength:
|
|
167
|
+
minLength: metadata.minLength,
|
|
168
|
+
maxLength: metadata.maxLength
|
|
173
169
|
});
|
|
174
170
|
if (typeof options.value === 'undefined' || options.value) {
|
|
175
171
|
props.defaultValue = metadata.initialValue;
|
|
@@ -199,7 +195,7 @@ function getTextareaProps(metadata) {
|
|
|
199
195
|
*/
|
|
200
196
|
function getCollectionProps(metadata, options) {
|
|
201
197
|
return options.options.map(value => {
|
|
202
|
-
var _metadata$key
|
|
198
|
+
var _metadata$key;
|
|
203
199
|
return simplify(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, getFormControlProps(metadata, options)), {}, {
|
|
204
200
|
key: "".concat((_metadata$key = metadata.key) !== null && _metadata$key !== void 0 ? _metadata$key : '').concat(value),
|
|
205
201
|
id: "".concat(metadata.id, "-").concat(value),
|
|
@@ -209,7 +205,7 @@ function getCollectionProps(metadata, options) {
|
|
|
209
205
|
// The required attribute doesn't make sense for checkbox group
|
|
210
206
|
// As it would require all checkboxes to be checked instead of at least one
|
|
211
207
|
// It is overriden with `undefiend` so it could be cleaned up properly
|
|
212
|
-
required: options.type === 'checkbox' ? undefined :
|
|
208
|
+
required: options.type === 'checkbox' ? undefined : metadata.required
|
|
213
209
|
}));
|
|
214
210
|
});
|
|
215
211
|
}
|
package/helpers.mjs
CHANGED
|
@@ -68,10 +68,9 @@ function getFieldsetProps(metadata, options) {
|
|
|
68
68
|
* including `key`, `id`, `name`, `form`, `required`, `autoFocus`, `aria-invalid` and `aria-describedby`.
|
|
69
69
|
*/
|
|
70
70
|
function getFormControlProps(metadata, options) {
|
|
71
|
-
var _metadata$constraint;
|
|
72
71
|
return simplify(_objectSpread2({
|
|
73
72
|
key: metadata.key,
|
|
74
|
-
required:
|
|
73
|
+
required: metadata.required || undefined
|
|
75
74
|
}, getFieldsetProps(metadata, options)));
|
|
76
75
|
}
|
|
77
76
|
|
|
@@ -96,16 +95,15 @@ function getFormControlProps(metadata, options) {
|
|
|
96
95
|
* ```
|
|
97
96
|
*/
|
|
98
97
|
function getInputProps(metadata, options) {
|
|
99
|
-
var _metadata$constraint2, _metadata$constraint3, _metadata$constraint4, _metadata$constraint5, _metadata$constraint6, _metadata$constraint7, _metadata$constraint8;
|
|
100
98
|
var props = _objectSpread2(_objectSpread2({}, getFormControlProps(metadata, options)), {}, {
|
|
101
99
|
type: options.type,
|
|
102
|
-
minLength:
|
|
103
|
-
maxLength:
|
|
104
|
-
min:
|
|
105
|
-
max:
|
|
106
|
-
step:
|
|
107
|
-
pattern:
|
|
108
|
-
multiple:
|
|
100
|
+
minLength: metadata.minLength,
|
|
101
|
+
maxLength: metadata.maxLength,
|
|
102
|
+
min: metadata.min,
|
|
103
|
+
max: metadata.max,
|
|
104
|
+
step: metadata.step,
|
|
105
|
+
pattern: metadata.pattern,
|
|
106
|
+
multiple: metadata.multiple
|
|
109
107
|
});
|
|
110
108
|
if (typeof options.value === 'undefined' || options.value) {
|
|
111
109
|
if (options.type === 'checkbox' || options.type === 'radio') {
|
|
@@ -134,10 +132,9 @@ function getInputProps(metadata, options) {
|
|
|
134
132
|
* ```
|
|
135
133
|
*/
|
|
136
134
|
function getSelectProps(metadata) {
|
|
137
|
-
var _metadata$constraint9;
|
|
138
135
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
139
136
|
var props = _objectSpread2(_objectSpread2({}, getFormControlProps(metadata, options)), {}, {
|
|
140
|
-
multiple:
|
|
137
|
+
multiple: metadata.multiple
|
|
141
138
|
});
|
|
142
139
|
if (typeof options.value === 'undefined' || options.value) {
|
|
143
140
|
props.defaultValue = Array.isArray(metadata.initialValue) ? metadata.initialValue.map(item => "".concat(item !== null && item !== void 0 ? item : '')) : metadata.initialValue;
|
|
@@ -161,11 +158,10 @@ function getSelectProps(metadata) {
|
|
|
161
158
|
* ```
|
|
162
159
|
*/
|
|
163
160
|
function getTextareaProps(metadata) {
|
|
164
|
-
var _metadata$constraint10, _metadata$constraint11;
|
|
165
161
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
166
162
|
var props = _objectSpread2(_objectSpread2({}, getFormControlProps(metadata, options)), {}, {
|
|
167
|
-
minLength:
|
|
168
|
-
maxLength:
|
|
163
|
+
minLength: metadata.minLength,
|
|
164
|
+
maxLength: metadata.maxLength
|
|
169
165
|
});
|
|
170
166
|
if (typeof options.value === 'undefined' || options.value) {
|
|
171
167
|
props.defaultValue = metadata.initialValue;
|
|
@@ -195,7 +191,7 @@ function getTextareaProps(metadata) {
|
|
|
195
191
|
*/
|
|
196
192
|
function getCollectionProps(metadata, options) {
|
|
197
193
|
return options.options.map(value => {
|
|
198
|
-
var _metadata$key
|
|
194
|
+
var _metadata$key;
|
|
199
195
|
return simplify(_objectSpread2(_objectSpread2({}, getFormControlProps(metadata, options)), {}, {
|
|
200
196
|
key: "".concat((_metadata$key = metadata.key) !== null && _metadata$key !== void 0 ? _metadata$key : '').concat(value),
|
|
201
197
|
id: "".concat(metadata.id, "-").concat(value),
|
|
@@ -205,7 +201,7 @@ function getCollectionProps(metadata, options) {
|
|
|
205
201
|
// The required attribute doesn't make sense for checkbox group
|
|
206
202
|
// As it would require all checkboxes to be checked instead of at least one
|
|
207
203
|
// It is overriden with `undefiend` so it could be cleaned up properly
|
|
208
|
-
required: options.type === 'checkbox' ? undefined :
|
|
204
|
+
required: options.type === 'checkbox' ? undefined : metadata.required
|
|
209
205
|
}));
|
|
210
206
|
});
|
|
211
207
|
}
|
package/hooks.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type FormId, type FieldName
|
|
1
|
+
import { type FormId, type FieldName } from '@conform-to/dom';
|
|
2
2
|
import { useEffect } from 'react';
|
|
3
|
-
import { type FormMetadata, type FieldMetadata, type Pretty } from './context';
|
|
3
|
+
import { type FormMetadata, type FieldMetadata, type Pretty, type FormOptions } from './context';
|
|
4
4
|
/**
|
|
5
5
|
* useLayoutEffect is client-only.
|
|
6
6
|
* This basically makes it a no-op on server
|
package/hooks.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
|
var react = require('react');
|
|
8
7
|
var context = require('./context.js');
|
|
9
8
|
|
|
@@ -37,7 +36,7 @@ function useForm(options) {
|
|
|
37
36
|
} = options,
|
|
38
37
|
formConfig = _rollupPluginBabelHelpers.objectWithoutProperties(options, _excluded);
|
|
39
38
|
var formId = useFormId(id);
|
|
40
|
-
var [context$1] = react.useState(() =>
|
|
39
|
+
var [context$1] = react.useState(() => context.createFormContext(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, formConfig), {}, {
|
|
41
40
|
formId
|
|
42
41
|
})));
|
|
43
42
|
useSafeLayoutEffect(() => {
|
package/hooks.mjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { objectWithoutProperties as _objectWithoutProperties, objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
-
import { unstable_createFormContext } from '@conform-to/dom';
|
|
3
2
|
import { useState, useEffect, useLayoutEffect, useId } from 'react';
|
|
4
|
-
import { useSubjectRef, useFormState, getFormMetadata, useFormContext, getFieldMetadata } from './context.mjs';
|
|
3
|
+
import { createFormContext, useSubjectRef, useFormState, getFormMetadata, useFormContext, getFieldMetadata } from './context.mjs';
|
|
5
4
|
|
|
6
5
|
var _excluded = ["id"];
|
|
7
6
|
|
|
@@ -33,7 +32,7 @@ function useForm(options) {
|
|
|
33
32
|
} = options,
|
|
34
33
|
formConfig = _objectWithoutProperties(options, _excluded);
|
|
35
34
|
var formId = useFormId(id);
|
|
36
|
-
var [context] = useState(() =>
|
|
35
|
+
var [context] = useState(() => createFormContext(_objectSpread2(_objectSpread2({}, formConfig), {}, {
|
|
37
36
|
formId
|
|
38
37
|
})));
|
|
39
38
|
useSafeLayoutEffect(() => {
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { type Submission, type SubmissionResult, type DefaultValue, type Intent, type FormId, type FieldName, parse, } from '@conform-to/dom';
|
|
2
2
|
export { type FieldMetadata, type FormMetadata, FormProvider, FormStateInput, } from './context';
|
|
3
3
|
export { useForm, useFormMetadata, useField } from './hooks';
|
|
4
|
-
export {
|
|
4
|
+
export { useInputControl } from './integrations';
|
|
5
5
|
export { getFormProps, getFieldsetProps, getInputProps, getSelectProps, getTextareaProps, getCollectionProps, } from './helpers';
|
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
|
|
6
|
+
"version": "1.0.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.0.0
|
|
33
|
+
"@conform-to/dom": "1.0.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/react": "^18.2.43",
|