@conform-to/react 1.1.0-pre.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README +1 -1
- package/context.d.ts +2 -1
- package/context.js +20 -9
- package/context.mjs +20 -9
- package/helpers.d.ts +3 -3
- package/helpers.js +1 -1
- package/helpers.mjs +1 -1
- package/package.json +2 -2
package/README
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
╚══════╝ ╚═════╝ ╚═╝ ╚══╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
Version 1.1.0
|
|
11
|
+
Version 1.1.0 / License MIT / Copyright (c) 2024 Edmund Hung
|
|
12
12
|
|
|
13
13
|
A type-safe form validation library utilizing web fundamentals to progressively enhance HTML Forms with full support for server frameworks like Remix and Next.js.
|
|
14
14
|
|
package/context.d.ts
CHANGED
|
@@ -53,7 +53,8 @@ export declare function FormStateInput(props: {
|
|
|
53
53
|
formId?: string;
|
|
54
54
|
}): React.ReactElement;
|
|
55
55
|
export declare function useSubjectRef(initialSubject?: SubscriptionSubject): MutableRefObject<SubscriptionSubject>;
|
|
56
|
-
export declare function updateSubjectRef(ref: 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;
|
|
57
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>;
|
|
58
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>;
|
|
59
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>;
|
package/context.js
CHANGED
|
@@ -17,7 +17,7 @@ function getWrappedFormContext(context) {
|
|
|
17
17
|
}
|
|
18
18
|
function useFormContext(formId) {
|
|
19
19
|
var contexts = react.useContext(Form);
|
|
20
|
-
var form = formId ? contexts.find(context =>
|
|
20
|
+
var form = formId ? contexts.find(context => formId === context.getFormId()) : contexts[0];
|
|
21
21
|
if (!form) {
|
|
22
22
|
throw new Error('Form context is not available');
|
|
23
23
|
}
|
|
@@ -56,10 +56,10 @@ function useSubjectRef() {
|
|
|
56
56
|
subjectRef.current = initialSubject;
|
|
57
57
|
return subjectRef;
|
|
58
58
|
}
|
|
59
|
-
function updateSubjectRef(ref,
|
|
60
|
-
if (subject === 'status') {
|
|
59
|
+
function updateSubjectRef(ref, subject, scope, name) {
|
|
60
|
+
if (subject === 'status' || subject === 'formId') {
|
|
61
61
|
ref.current[subject] = true;
|
|
62
|
-
} else {
|
|
62
|
+
} else if (typeof scope !== 'undefined' && typeof name !== 'undefined') {
|
|
63
63
|
var _ref$current$subject$, _ref$current$subject;
|
|
64
64
|
ref.current[subject] = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, ref.current[subject]), {}, {
|
|
65
65
|
[scope]: ((_ref$current$subject$ = (_ref$current$subject = ref.current[subject]) === null || _ref$current$subject === void 0 ? void 0 : _ref$current$subject[scope]) !== null && _ref$current$subject$ !== void 0 ? _ref$current$subject$ : []).concat(name)
|
|
@@ -68,7 +68,7 @@ function updateSubjectRef(ref, name, subject, scope) {
|
|
|
68
68
|
}
|
|
69
69
|
function getMetadata(context, subjectRef, stateSnapshot) {
|
|
70
70
|
var name = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
|
|
71
|
-
var id = name ? "".concat(context.
|
|
71
|
+
var id = name ? "".concat(context.getFormId(), "-").concat(name) : context.getFormId();
|
|
72
72
|
var state = context.getState();
|
|
73
73
|
return new Proxy({
|
|
74
74
|
id,
|
|
@@ -122,16 +122,21 @@ function getMetadata(context, subjectRef, stateSnapshot) {
|
|
|
122
122
|
// if the stateSnapshot is not the latest, then it must be accessed in a callback
|
|
123
123
|
if (state === stateSnapshot) {
|
|
124
124
|
switch (key) {
|
|
125
|
+
case 'id':
|
|
126
|
+
case 'errorId':
|
|
127
|
+
case 'descriptionId':
|
|
128
|
+
updateSubjectRef(subjectRef, 'formId');
|
|
129
|
+
break;
|
|
125
130
|
case 'key':
|
|
126
131
|
case 'initialValue':
|
|
127
132
|
case 'value':
|
|
128
133
|
case 'valid':
|
|
129
134
|
case 'dirty':
|
|
130
|
-
updateSubjectRef(subjectRef,
|
|
135
|
+
updateSubjectRef(subjectRef, key, 'name', name);
|
|
131
136
|
break;
|
|
132
137
|
case 'errors':
|
|
133
138
|
case 'allErrors':
|
|
134
|
-
updateSubjectRef(subjectRef,
|
|
139
|
+
updateSubjectRef(subjectRef, 'error', key === 'errors' ? 'name' : 'prefix', name);
|
|
135
140
|
break;
|
|
136
141
|
}
|
|
137
142
|
}
|
|
@@ -150,7 +155,10 @@ function getFieldMetadata(context, subjectRef, stateSnapshot) {
|
|
|
150
155
|
var state = context.getState();
|
|
151
156
|
switch (key) {
|
|
152
157
|
case 'formId':
|
|
153
|
-
|
|
158
|
+
if (state === stateSnapshot) {
|
|
159
|
+
updateSubjectRef(subjectRef, 'formId');
|
|
160
|
+
}
|
|
161
|
+
return context.getFormId();
|
|
154
162
|
case 'required':
|
|
155
163
|
case 'minLength':
|
|
156
164
|
case 'maxLength':
|
|
@@ -166,7 +174,7 @@ function getFieldMetadata(context, subjectRef, stateSnapshot) {
|
|
|
166
174
|
var _state$initialValue$n;
|
|
167
175
|
var initialValue = (_state$initialValue$n = state.initialValue[name]) !== null && _state$initialValue$n !== void 0 ? _state$initialValue$n : [];
|
|
168
176
|
if (state === stateSnapshot) {
|
|
169
|
-
updateSubjectRef(subjectRef,
|
|
177
|
+
updateSubjectRef(subjectRef, 'initialValue', 'name', name);
|
|
170
178
|
}
|
|
171
179
|
if (!Array.isArray(initialValue)) {
|
|
172
180
|
throw new Error('The initial value at the given name is not a list');
|
|
@@ -190,6 +198,9 @@ function getFormMetadata(context, subjectRef, stateSnapshot, noValidate) {
|
|
|
190
198
|
[wrappedSymbol]: context
|
|
191
199
|
};
|
|
192
200
|
case 'status':
|
|
201
|
+
if (state === stateSnapshot) {
|
|
202
|
+
updateSubjectRef(subjectRef, 'status');
|
|
203
|
+
}
|
|
193
204
|
return state.submissionStatus;
|
|
194
205
|
case 'validate':
|
|
195
206
|
case 'update':
|
package/context.mjs
CHANGED
|
@@ -13,7 +13,7 @@ function getWrappedFormContext(context) {
|
|
|
13
13
|
}
|
|
14
14
|
function useFormContext(formId) {
|
|
15
15
|
var contexts = useContext(Form);
|
|
16
|
-
var form = formId ? contexts.find(context =>
|
|
16
|
+
var form = formId ? contexts.find(context => formId === context.getFormId()) : contexts[0];
|
|
17
17
|
if (!form) {
|
|
18
18
|
throw new Error('Form context is not available');
|
|
19
19
|
}
|
|
@@ -52,10 +52,10 @@ function useSubjectRef() {
|
|
|
52
52
|
subjectRef.current = initialSubject;
|
|
53
53
|
return subjectRef;
|
|
54
54
|
}
|
|
55
|
-
function updateSubjectRef(ref,
|
|
56
|
-
if (subject === 'status') {
|
|
55
|
+
function updateSubjectRef(ref, subject, scope, name) {
|
|
56
|
+
if (subject === 'status' || subject === 'formId') {
|
|
57
57
|
ref.current[subject] = true;
|
|
58
|
-
} else {
|
|
58
|
+
} else if (typeof scope !== 'undefined' && typeof name !== 'undefined') {
|
|
59
59
|
var _ref$current$subject$, _ref$current$subject;
|
|
60
60
|
ref.current[subject] = _objectSpread2(_objectSpread2({}, ref.current[subject]), {}, {
|
|
61
61
|
[scope]: ((_ref$current$subject$ = (_ref$current$subject = ref.current[subject]) === null || _ref$current$subject === void 0 ? void 0 : _ref$current$subject[scope]) !== null && _ref$current$subject$ !== void 0 ? _ref$current$subject$ : []).concat(name)
|
|
@@ -64,7 +64,7 @@ function updateSubjectRef(ref, name, subject, scope) {
|
|
|
64
64
|
}
|
|
65
65
|
function getMetadata(context, subjectRef, stateSnapshot) {
|
|
66
66
|
var name = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
|
|
67
|
-
var id = name ? "".concat(context.
|
|
67
|
+
var id = name ? "".concat(context.getFormId(), "-").concat(name) : context.getFormId();
|
|
68
68
|
var state = context.getState();
|
|
69
69
|
return new Proxy({
|
|
70
70
|
id,
|
|
@@ -118,16 +118,21 @@ function getMetadata(context, subjectRef, stateSnapshot) {
|
|
|
118
118
|
// if the stateSnapshot is not the latest, then it must be accessed in a callback
|
|
119
119
|
if (state === stateSnapshot) {
|
|
120
120
|
switch (key) {
|
|
121
|
+
case 'id':
|
|
122
|
+
case 'errorId':
|
|
123
|
+
case 'descriptionId':
|
|
124
|
+
updateSubjectRef(subjectRef, 'formId');
|
|
125
|
+
break;
|
|
121
126
|
case 'key':
|
|
122
127
|
case 'initialValue':
|
|
123
128
|
case 'value':
|
|
124
129
|
case 'valid':
|
|
125
130
|
case 'dirty':
|
|
126
|
-
updateSubjectRef(subjectRef,
|
|
131
|
+
updateSubjectRef(subjectRef, key, 'name', name);
|
|
127
132
|
break;
|
|
128
133
|
case 'errors':
|
|
129
134
|
case 'allErrors':
|
|
130
|
-
updateSubjectRef(subjectRef,
|
|
135
|
+
updateSubjectRef(subjectRef, 'error', key === 'errors' ? 'name' : 'prefix', name);
|
|
131
136
|
break;
|
|
132
137
|
}
|
|
133
138
|
}
|
|
@@ -146,7 +151,10 @@ function getFieldMetadata(context, subjectRef, stateSnapshot) {
|
|
|
146
151
|
var state = context.getState();
|
|
147
152
|
switch (key) {
|
|
148
153
|
case 'formId':
|
|
149
|
-
|
|
154
|
+
if (state === stateSnapshot) {
|
|
155
|
+
updateSubjectRef(subjectRef, 'formId');
|
|
156
|
+
}
|
|
157
|
+
return context.getFormId();
|
|
150
158
|
case 'required':
|
|
151
159
|
case 'minLength':
|
|
152
160
|
case 'maxLength':
|
|
@@ -162,7 +170,7 @@ function getFieldMetadata(context, subjectRef, stateSnapshot) {
|
|
|
162
170
|
var _state$initialValue$n;
|
|
163
171
|
var initialValue = (_state$initialValue$n = state.initialValue[name]) !== null && _state$initialValue$n !== void 0 ? _state$initialValue$n : [];
|
|
164
172
|
if (state === stateSnapshot) {
|
|
165
|
-
updateSubjectRef(subjectRef,
|
|
173
|
+
updateSubjectRef(subjectRef, 'initialValue', 'name', name);
|
|
166
174
|
}
|
|
167
175
|
if (!Array.isArray(initialValue)) {
|
|
168
176
|
throw new Error('The initial value at the given name is not a list');
|
|
@@ -186,6 +194,9 @@ function getFormMetadata(context, subjectRef, stateSnapshot, noValidate) {
|
|
|
186
194
|
[wrappedSymbol]: context
|
|
187
195
|
};
|
|
188
196
|
case 'status':
|
|
197
|
+
if (state === stateSnapshot) {
|
|
198
|
+
updateSubjectRef(subjectRef, 'status');
|
|
199
|
+
}
|
|
189
200
|
return state.submissionStatus;
|
|
190
201
|
case 'validate':
|
|
191
202
|
case 'update':
|
package/helpers.d.ts
CHANGED
|
@@ -175,7 +175,7 @@ export declare function getTextareaProps<Schema>(metadata: FieldMetadata<Schema,
|
|
|
175
175
|
* Derives the properties of a collection of checkboxes or radio buttons based on the field metadata,
|
|
176
176
|
* including common form control attributes like `key`, `id`, `name`, `form`, `autoFocus`, `aria-invalid`, `aria-describedby` and `required`.
|
|
177
177
|
*
|
|
178
|
-
* @see https://conform.guide/api/react/
|
|
178
|
+
* @see https://conform.guide/api/react/getCollectionProps
|
|
179
179
|
* @example
|
|
180
180
|
* ```tsx
|
|
181
181
|
* <fieldset>
|
|
@@ -191,7 +191,7 @@ export declare function getTextareaProps<Schema>(metadata: FieldMetadata<Schema,
|
|
|
191
191
|
* </fieldset>
|
|
192
192
|
* ```
|
|
193
193
|
*/
|
|
194
|
-
export declare function getCollectionProps<Schema extends Array<string | boolean> | string | boolean | undefined | unknown
|
|
194
|
+
export declare function getCollectionProps<Schema extends Array<string | boolean> | string | boolean | undefined | unknown, Options extends Pretty<FormControlOptions & {
|
|
195
195
|
/**
|
|
196
196
|
* The input type. Use `checkbox` for multiple selection or `radio` for single selection.
|
|
197
197
|
*/
|
|
@@ -204,5 +204,5 @@ export declare function getCollectionProps<Schema extends Array<string | boolean
|
|
|
204
204
|
* Decide whether defaultValue should be returned. Pass `false` if you want to manage the value yourself.
|
|
205
205
|
*/
|
|
206
206
|
value?: boolean;
|
|
207
|
-
}
|
|
207
|
+
}>>(metadata: FieldMetadata<Schema, any, any>, options: Options): Array<InputProps & Pick<Options, 'type'> & Pick<Required<InputProps>, 'value'>>;
|
|
208
208
|
export {};
|
package/helpers.js
CHANGED
|
@@ -179,7 +179,7 @@ function getTextareaProps(metadata) {
|
|
|
179
179
|
* Derives the properties of a collection of checkboxes or radio buttons based on the field metadata,
|
|
180
180
|
* including common form control attributes like `key`, `id`, `name`, `form`, `autoFocus`, `aria-invalid`, `aria-describedby` and `required`.
|
|
181
181
|
*
|
|
182
|
-
* @see https://conform.guide/api/react/
|
|
182
|
+
* @see https://conform.guide/api/react/getCollectionProps
|
|
183
183
|
* @example
|
|
184
184
|
* ```tsx
|
|
185
185
|
* <fieldset>
|
package/helpers.mjs
CHANGED
|
@@ -175,7 +175,7 @@ function getTextareaProps(metadata) {
|
|
|
175
175
|
* Derives the properties of a collection of checkboxes or radio buttons based on the field metadata,
|
|
176
176
|
* including common form control attributes like `key`, `id`, `name`, `form`, `autoFocus`, `aria-invalid`, `aria-describedby` and `required`.
|
|
177
177
|
*
|
|
178
|
-
* @see https://conform.guide/api/react/
|
|
178
|
+
* @see https://conform.guide/api/react/getCollectionProps
|
|
179
179
|
* @example
|
|
180
180
|
* ```tsx
|
|
181
181
|
* <fieldset>
|
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.0
|
|
6
|
+
"version": "1.1.0",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"module": "index.mjs",
|
|
9
9
|
"types": "index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"url": "https://github.com/edmundhung/conform/issues"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@conform-to/dom": "1.1.0
|
|
33
|
+
"@conform-to/dom": "1.1.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/react": "^18.2.43",
|