@conform-to/react 0.6.0-pre.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +187 -133
- package/helpers.d.ts +17 -20
- package/helpers.js +50 -65
- package/hooks.d.ts +31 -24
- package/hooks.js +121 -140
- package/index.d.ts +2 -2
- package/index.js +2 -6
- package/module/helpers.js +48 -66
- package/module/hooks.js +123 -142
- package/module/index.js +1 -1
- package/package.json +7 -3
- package/base.d.ts +0 -17
- package/base.js +0 -112
- package/module/base.js +0 -107
package/module/hooks.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js';
|
|
2
|
-
import {
|
|
3
|
-
import { useRef, useState,
|
|
2
|
+
import { getScope, parseListCommand, reportSubmission, getFormData, parse, VALIDATION_UNDEFINED, getFormAttributes, getPaths, getName, isFieldElement, requestIntent, validate, FORM_ERROR_ELEMENT_NAME, getErrors, getFormElement, getValidationMessage, updateList } from '@conform-to/dom';
|
|
3
|
+
import { useRef, useState, useMemo, useEffect, useLayoutEffect } from 'react';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Returns properties required to hook into form events.
|
|
@@ -9,43 +9,40 @@ import { useRef, useState, useEffect, useMemo, useLayoutEffect } from 'react';
|
|
|
9
9
|
* @see https://conform.guide/api/react#useform
|
|
10
10
|
*/
|
|
11
11
|
function useForm() {
|
|
12
|
-
var _config$
|
|
12
|
+
var _config$lastSubmissio, _config$ref, _ref2, _config$lastSubmissio2;
|
|
13
13
|
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
14
14
|
var configRef = useRef(config);
|
|
15
|
-
var
|
|
16
|
-
var [lastSubmission, setLastSubmission] = useState((_config$
|
|
17
|
-
var [
|
|
18
|
-
if (!config.
|
|
19
|
-
return
|
|
15
|
+
var formRef = useRef(null);
|
|
16
|
+
var [lastSubmission, setLastSubmission] = useState((_config$lastSubmissio = config.lastSubmission) !== null && _config$lastSubmissio !== void 0 ? _config$lastSubmissio : null);
|
|
17
|
+
var [errors, setErrors] = useState(() => {
|
|
18
|
+
if (!config.lastSubmission) {
|
|
19
|
+
return [];
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
return getValidationMessage(message);
|
|
21
|
+
return [].concat(config.lastSubmission.error['']);
|
|
23
22
|
});
|
|
24
|
-
var
|
|
25
|
-
var submission = config.
|
|
23
|
+
var initialError = useMemo(() => {
|
|
24
|
+
var submission = config.lastSubmission;
|
|
26
25
|
if (!submission) {
|
|
27
|
-
return {
|
|
28
|
-
defaultValue: config.defaultValue
|
|
29
|
-
};
|
|
26
|
+
return {};
|
|
30
27
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
28
|
+
var scope = getScope(submission.intent);
|
|
29
|
+
return Object.entries(submission.error).reduce((result, _ref) => {
|
|
30
|
+
var [name, message] = _ref;
|
|
31
|
+
if (name !== '' && (scope === null || scope === name)) {
|
|
32
|
+
result[name] = message;
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}, {});
|
|
36
|
+
}, [config.lastSubmission]);
|
|
37
|
+
var ref = (_config$ref = config.ref) !== null && _config$ref !== void 0 ? _config$ref : formRef;
|
|
38
|
+
var fieldset = useFieldset(ref, {
|
|
39
|
+
defaultValue: (_ref2 = (_config$lastSubmissio2 = config.lastSubmission) === null || _config$lastSubmissio2 === void 0 ? void 0 : _config$lastSubmissio2.payload) !== null && _ref2 !== void 0 ? _ref2 : config.defaultValue,
|
|
40
|
+
initialError,
|
|
43
41
|
constraint: config.constraint,
|
|
44
42
|
form: config.id
|
|
45
43
|
});
|
|
46
|
-
var fieldset = useFieldset(ref, fieldsetConfig);
|
|
47
44
|
var [noValidate, setNoValidate] = useState(config.noValidate || !config.fallbackNative);
|
|
48
|
-
|
|
45
|
+
useSafeLayoutEffect(() => {
|
|
49
46
|
configRef.current = config;
|
|
50
47
|
});
|
|
51
48
|
useEffect(() => {
|
|
@@ -53,7 +50,7 @@ function useForm() {
|
|
|
53
50
|
}, []);
|
|
54
51
|
useEffect(() => {
|
|
55
52
|
var form = ref.current;
|
|
56
|
-
var submission = config.
|
|
53
|
+
var submission = config.lastSubmission;
|
|
57
54
|
if (!form || !submission) {
|
|
58
55
|
return;
|
|
59
56
|
}
|
|
@@ -64,24 +61,29 @@ function useForm() {
|
|
|
64
61
|
}));
|
|
65
62
|
}
|
|
66
63
|
setLastSubmission(submission);
|
|
67
|
-
}, [config.
|
|
64
|
+
}, [ref, config.lastSubmission]);
|
|
68
65
|
useEffect(() => {
|
|
69
66
|
var form = ref.current;
|
|
70
67
|
if (!form || !lastSubmission) {
|
|
71
68
|
return;
|
|
72
69
|
}
|
|
73
|
-
reportSubmission(
|
|
74
|
-
}, [lastSubmission]);
|
|
70
|
+
reportSubmission(form, lastSubmission);
|
|
71
|
+
}, [ref, lastSubmission]);
|
|
75
72
|
useEffect(() => {
|
|
76
73
|
// Revalidate the form when input value is changed
|
|
77
74
|
var handleInput = event => {
|
|
78
75
|
var field = event.target;
|
|
79
76
|
var form = ref.current;
|
|
80
77
|
var formConfig = configRef.current;
|
|
78
|
+
var {
|
|
79
|
+
initialReport = 'onSubmit',
|
|
80
|
+
shouldValidate = initialReport === 'onChange' ? 'onInput' : initialReport,
|
|
81
|
+
shouldRevalidate = 'onInput'
|
|
82
|
+
} = formConfig;
|
|
81
83
|
if (!form || !isFieldElement(field) || field.form !== form) {
|
|
82
84
|
return;
|
|
83
85
|
}
|
|
84
|
-
if (field.dataset.conformTouched
|
|
86
|
+
if (field.dataset.conformTouched ? shouldRevalidate === 'onInput' : shouldValidate === 'onInput') {
|
|
85
87
|
requestIntent(form, validate(field.name));
|
|
86
88
|
}
|
|
87
89
|
};
|
|
@@ -89,27 +91,31 @@ function useForm() {
|
|
|
89
91
|
var field = event.target;
|
|
90
92
|
var form = ref.current;
|
|
91
93
|
var formConfig = configRef.current;
|
|
94
|
+
var {
|
|
95
|
+
initialReport = 'onSubmit',
|
|
96
|
+
shouldValidate = initialReport === 'onChange' ? 'onInput' : initialReport,
|
|
97
|
+
shouldRevalidate = 'onInput'
|
|
98
|
+
} = formConfig;
|
|
92
99
|
if (!form || !isFieldElement(field) || field.form !== form) {
|
|
93
100
|
return;
|
|
94
101
|
}
|
|
95
|
-
if (
|
|
102
|
+
if (field.dataset.conformTouched ? shouldRevalidate === 'onBlur' : shouldValidate === 'onBlur') {
|
|
96
103
|
requestIntent(form, validate(field.name));
|
|
97
104
|
}
|
|
98
105
|
};
|
|
99
106
|
var handleInvalid = event => {
|
|
100
|
-
var form =
|
|
107
|
+
var form = ref.current;
|
|
101
108
|
var field = event.target;
|
|
102
|
-
if (!form || !isFieldElement(field) || field.form !== form || field.name !==
|
|
109
|
+
if (!form || !isFieldElement(field) || field.form !== form || field.name !== FORM_ERROR_ELEMENT_NAME) {
|
|
103
110
|
return;
|
|
104
111
|
}
|
|
105
112
|
event.preventDefault();
|
|
106
113
|
if (field.dataset.conformTouched) {
|
|
107
|
-
|
|
114
|
+
setErrors(getErrors(field.validationMessage));
|
|
108
115
|
}
|
|
109
116
|
};
|
|
110
117
|
var handleReset = event => {
|
|
111
118
|
var form = ref.current;
|
|
112
|
-
var formConfig = configRef.current;
|
|
113
119
|
if (!form || event.target !== form) {
|
|
114
120
|
return;
|
|
115
121
|
}
|
|
@@ -118,22 +124,11 @@ function useForm() {
|
|
|
118
124
|
for (var field of form.elements) {
|
|
119
125
|
if (isFieldElement(field)) {
|
|
120
126
|
delete field.dataset.conformTouched;
|
|
121
|
-
field.setAttribute('aria-invalid', 'false');
|
|
122
127
|
field.setCustomValidity('');
|
|
123
128
|
}
|
|
124
129
|
}
|
|
125
|
-
|
|
126
|
-
setUncontrolledState({
|
|
127
|
-
defaultValue: formConfig.defaultValue
|
|
128
|
-
});
|
|
130
|
+
setErrors([]);
|
|
129
131
|
};
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* The input event handler will be triggered in capturing phase in order to
|
|
133
|
-
* allow follow-up action in the bubble phase based on the latest validity
|
|
134
|
-
* E.g. `useFieldset` reset the error of valid field after checking the
|
|
135
|
-
* validity in the bubble phase.
|
|
136
|
-
*/
|
|
137
132
|
document.addEventListener('input', handleInput, true);
|
|
138
133
|
document.addEventListener('blur', handleBlur, true);
|
|
139
134
|
document.addEventListener('invalid', handleInvalid, true);
|
|
@@ -144,14 +139,13 @@ function useForm() {
|
|
|
144
139
|
document.removeEventListener('invalid', handleInvalid, true);
|
|
145
140
|
document.removeEventListener('reset', handleReset);
|
|
146
141
|
};
|
|
147
|
-
}, []);
|
|
142
|
+
}, [ref]);
|
|
148
143
|
var form = {
|
|
149
|
-
id: config.id,
|
|
150
144
|
ref,
|
|
151
|
-
error,
|
|
145
|
+
error: errors[0],
|
|
146
|
+
errors,
|
|
152
147
|
props: {
|
|
153
148
|
ref,
|
|
154
|
-
id: config.id,
|
|
155
149
|
noValidate,
|
|
156
150
|
onSubmit(event) {
|
|
157
151
|
var form = event.currentTarget;
|
|
@@ -168,11 +162,11 @@ function useForm() {
|
|
|
168
162
|
form,
|
|
169
163
|
formData
|
|
170
164
|
});
|
|
171
|
-
if (!config.noValidate && !(submitter !== null && submitter !== void 0 && submitter.formNoValidate) && Object.entries(submission.error).some(
|
|
172
|
-
var [, message] = _ref2;
|
|
173
|
-
return message !== '' && ![].concat(message).includes(VALIDATION_UNDEFINED);
|
|
174
|
-
}) || typeof config.onValidate !== 'undefined' && (submission.intent.startsWith('validate') || submission.intent.startsWith('list')) && Object.entries(submission.error).every(_ref3 => {
|
|
165
|
+
if (!config.noValidate && !(submitter !== null && submitter !== void 0 && submitter.formNoValidate) && Object.entries(submission.error).some(_ref3 => {
|
|
175
166
|
var [, message] = _ref3;
|
|
167
|
+
return message !== '' && ![].concat(message).includes(VALIDATION_UNDEFINED);
|
|
168
|
+
}) || typeof config.onValidate !== 'undefined' && (submission.intent.startsWith('validate') || submission.intent.startsWith('list')) && Object.entries(submission.error).every(_ref4 => {
|
|
169
|
+
var [, message] = _ref4;
|
|
176
170
|
return ![].concat(message).includes(VALIDATION_UNDEFINED);
|
|
177
171
|
})) {
|
|
178
172
|
var listCommand = parseListCommand(submission.intent);
|
|
@@ -194,46 +188,41 @@ function useForm() {
|
|
|
194
188
|
console.warn(e);
|
|
195
189
|
}
|
|
196
190
|
}
|
|
197
|
-
}
|
|
198
|
-
config: fieldsetConfig
|
|
191
|
+
}
|
|
199
192
|
};
|
|
193
|
+
if (config.id) {
|
|
194
|
+
form.id = config.id;
|
|
195
|
+
form.errorId = "".concat(config.id, "-error");
|
|
196
|
+
form.props.id = form.id;
|
|
197
|
+
}
|
|
198
|
+
if (form.errorId && form.errors.length > 0) {
|
|
199
|
+
form.props['aria-invalid'] = 'true';
|
|
200
|
+
form.props['aria-describedby'] = form.errorId;
|
|
201
|
+
}
|
|
200
202
|
return [form, fieldset];
|
|
201
203
|
}
|
|
202
204
|
|
|
203
205
|
/**
|
|
204
|
-
*
|
|
206
|
+
* A set of field configuration
|
|
205
207
|
*/
|
|
206
208
|
|
|
207
209
|
function useFieldset(ref, config) {
|
|
208
210
|
var configRef = useRef(config);
|
|
209
|
-
var [uncontrolledState, setUncontrolledState] = useState(
|
|
210
|
-
// @ts-expect-error
|
|
211
|
-
() => {
|
|
212
|
-
var _config$defaultValue;
|
|
213
|
-
var initialError = {};
|
|
214
|
-
for (var [name, message] of Object.entries((_config$initialError = config === null || config === void 0 ? void 0 : config.initialError) !== null && _config$initialError !== void 0 ? _config$initialError : {})) {
|
|
215
|
-
var _config$initialError;
|
|
216
|
-
var [key, ...paths] = getPaths(name);
|
|
217
|
-
if (typeof key === 'string') {
|
|
218
|
-
var scopedName = getName(paths);
|
|
219
|
-
initialError[key] = _objectSpread2(_objectSpread2({}, initialError[key]), {}, {
|
|
220
|
-
[scopedName]: message
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
return {
|
|
225
|
-
defaultValue: (_config$defaultValue = config === null || config === void 0 ? void 0 : config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : {},
|
|
226
|
-
initialError
|
|
227
|
-
};
|
|
228
|
-
});
|
|
229
211
|
var [error, setError] = useState(() => {
|
|
212
|
+
var initialError = config === null || config === void 0 ? void 0 : config.initialError;
|
|
213
|
+
if (!initialError) {
|
|
214
|
+
return {};
|
|
215
|
+
}
|
|
230
216
|
var result = {};
|
|
231
|
-
for (var [
|
|
232
|
-
|
|
217
|
+
for (var [name, message] of Object.entries(initialError)) {
|
|
218
|
+
var [key, ...paths] = getPaths(name);
|
|
219
|
+
if (typeof key === 'string' && paths.length === 0) {
|
|
220
|
+
result[key] = [].concat(message !== null && message !== void 0 ? message : []);
|
|
221
|
+
}
|
|
233
222
|
}
|
|
234
223
|
return result;
|
|
235
224
|
});
|
|
236
|
-
|
|
225
|
+
useSafeLayoutEffect(() => {
|
|
237
226
|
configRef.current = config;
|
|
238
227
|
});
|
|
239
228
|
useEffect(() => {
|
|
@@ -250,10 +239,6 @@ function useFieldset(ref, config) {
|
|
|
250
239
|
// Update the error only if the field belongs to the fieldset
|
|
251
240
|
if (typeof key === 'string' && paths.length === 0) {
|
|
252
241
|
if (field.dataset.conformTouched) {
|
|
253
|
-
// Update the aria attribute only if it is set
|
|
254
|
-
if (field.getAttribute('aria-invalid')) {
|
|
255
|
-
field.setAttribute('aria-invalid', field.validationMessage !== '' ? 'true' : 'false');
|
|
256
|
-
}
|
|
257
242
|
setError(prev => {
|
|
258
243
|
var prevMessage = getValidationMessage(prev === null || prev === void 0 ? void 0 : prev[key]);
|
|
259
244
|
if (prevMessage === field.validationMessage) {
|
|
@@ -268,17 +253,10 @@ function useFieldset(ref, config) {
|
|
|
268
253
|
}
|
|
269
254
|
};
|
|
270
255
|
var resetHandler = event => {
|
|
271
|
-
var _fieldsetConfig$defau;
|
|
272
256
|
var form = getFormElement(ref.current);
|
|
273
257
|
if (!form || event.target !== form) {
|
|
274
258
|
return;
|
|
275
259
|
}
|
|
276
|
-
var fieldsetConfig = configRef.current;
|
|
277
|
-
setUncontrolledState({
|
|
278
|
-
// @ts-expect-error
|
|
279
|
-
defaultValue: (_fieldsetConfig$defau = fieldsetConfig === null || fieldsetConfig === void 0 ? void 0 : fieldsetConfig.defaultValue) !== null && _fieldsetConfig$defau !== void 0 ? _fieldsetConfig$defau : {},
|
|
280
|
-
initialError: {}
|
|
281
|
-
});
|
|
282
260
|
setError({});
|
|
283
261
|
};
|
|
284
262
|
|
|
@@ -298,26 +276,33 @@ function useFieldset(ref, config) {
|
|
|
298
276
|
*/
|
|
299
277
|
return new Proxy({}, {
|
|
300
278
|
get(_target, key) {
|
|
301
|
-
var _fieldsetConfig$const;
|
|
279
|
+
var _fieldsetConfig$const, _fieldsetConfig$initi, _fieldsetConfig$defau;
|
|
302
280
|
if (typeof key !== 'string') {
|
|
303
281
|
return;
|
|
304
282
|
}
|
|
305
|
-
var fieldsetConfig = config
|
|
283
|
+
var fieldsetConfig = config;
|
|
306
284
|
var constraint = (_fieldsetConfig$const = fieldsetConfig.constraint) === null || _fieldsetConfig$const === void 0 ? void 0 : _fieldsetConfig$const[key];
|
|
307
285
|
var errors = error === null || error === void 0 ? void 0 : error[key];
|
|
308
|
-
var
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
}
|
|
286
|
+
var initialError = Object.entries((_fieldsetConfig$initi = fieldsetConfig.initialError) !== null && _fieldsetConfig$initi !== void 0 ? _fieldsetConfig$initi : {}).reduce((result, _ref5) => {
|
|
287
|
+
var [name, message] = _ref5;
|
|
288
|
+
var [field, ...paths] = getPaths(name);
|
|
289
|
+
if (field === key) {
|
|
290
|
+
result[getName(paths)] = message;
|
|
291
|
+
}
|
|
292
|
+
return result;
|
|
293
|
+
}, {});
|
|
294
|
+
var field = _objectSpread2(_objectSpread2({}, constraint), {}, {
|
|
295
|
+
name: fieldsetConfig.name ? "".concat(fieldsetConfig.name, ".").concat(key) : key,
|
|
296
|
+
defaultValue: (_fieldsetConfig$defau = fieldsetConfig.defaultValue) === null || _fieldsetConfig$defau === void 0 ? void 0 : _fieldsetConfig$defau[key],
|
|
297
|
+
initialError,
|
|
314
298
|
error: errors === null || errors === void 0 ? void 0 : errors[0],
|
|
315
299
|
errors
|
|
316
|
-
};
|
|
300
|
+
});
|
|
317
301
|
if (fieldsetConfig.form) {
|
|
318
|
-
field.
|
|
319
|
-
field.
|
|
320
|
-
field.
|
|
302
|
+
field.form = fieldsetConfig.form;
|
|
303
|
+
field.id = "".concat(fieldsetConfig.form, "-").concat(field.name);
|
|
304
|
+
field.errorId = "".concat(field.id, "-error");
|
|
305
|
+
field.descriptionId = "".concat(field.id, "-description");
|
|
321
306
|
}
|
|
322
307
|
return field;
|
|
323
308
|
}
|
|
@@ -332,30 +317,22 @@ function useFieldset(ref, config) {
|
|
|
332
317
|
*/
|
|
333
318
|
function useFieldList(ref, config) {
|
|
334
319
|
var configRef = useRef(config);
|
|
335
|
-
var [
|
|
336
|
-
var _config$defaultValue2;
|
|
320
|
+
var [error, setError] = useState(() => {
|
|
337
321
|
var initialError = [];
|
|
338
|
-
for (var [name, message] of Object.entries((_config$
|
|
339
|
-
var _config$
|
|
322
|
+
for (var [name, message] of Object.entries((_config$initialError = config === null || config === void 0 ? void 0 : config.initialError) !== null && _config$initialError !== void 0 ? _config$initialError : {})) {
|
|
323
|
+
var _config$initialError;
|
|
340
324
|
var [index, ...paths] = getPaths(name);
|
|
341
|
-
if (typeof index === 'number') {
|
|
342
|
-
|
|
343
|
-
initialError[index] = _objectSpread2(_objectSpread2({}, initialError[index]), {}, {
|
|
344
|
-
[scopedName]: message
|
|
345
|
-
});
|
|
325
|
+
if (typeof index === 'number' && paths.length === 0) {
|
|
326
|
+
initialError[index] = [].concat(message !== null && message !== void 0 ? message : []);
|
|
346
327
|
}
|
|
347
328
|
}
|
|
348
|
-
return
|
|
349
|
-
defaultValue: (_config$defaultValue2 = config.defaultValue) !== null && _config$defaultValue2 !== void 0 ? _config$defaultValue2 : [],
|
|
350
|
-
initialError
|
|
351
|
-
};
|
|
329
|
+
return initialError;
|
|
352
330
|
});
|
|
353
|
-
var [error, setError] = useState(() => uncontrolledState.initialError.map(error => getErrors(getValidationMessage(error === null || error === void 0 ? void 0 : error['']))));
|
|
354
331
|
var [entries, setEntries] = useState(() => {
|
|
355
|
-
var _config$
|
|
356
|
-
return Object.entries((_config$
|
|
332
|
+
var _config$defaultValue;
|
|
333
|
+
return Object.entries((_config$defaultValue = config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : [undefined]);
|
|
357
334
|
});
|
|
358
|
-
|
|
335
|
+
useSafeLayoutEffect(() => {
|
|
359
336
|
configRef.current = config;
|
|
360
337
|
});
|
|
361
338
|
useEffect(() => {
|
|
@@ -429,17 +406,12 @@ function useFieldList(ref, config) {
|
|
|
429
406
|
});
|
|
430
407
|
};
|
|
431
408
|
var resetHandler = event => {
|
|
432
|
-
var
|
|
409
|
+
var _configRef$current$de;
|
|
433
410
|
var form = getFormElement(ref.current);
|
|
434
411
|
if (!form || event.target !== form) {
|
|
435
412
|
return;
|
|
436
413
|
}
|
|
437
|
-
|
|
438
|
-
setUncontrolledState({
|
|
439
|
-
defaultValue: (_fieldConfig$defaultV = fieldConfig.defaultValue) !== null && _fieldConfig$defaultV !== void 0 ? _fieldConfig$defaultV : [],
|
|
440
|
-
initialError: []
|
|
441
|
-
});
|
|
442
|
-
setEntries(Object.entries((_fieldConfig$defaultV2 = fieldConfig.defaultValue) !== null && _fieldConfig$defaultV2 !== void 0 ? _fieldConfig$defaultV2 : [undefined]));
|
|
414
|
+
setEntries(Object.entries((_configRef$current$de = configRef.current.defaultValue) !== null && _configRef$current$de !== void 0 ? _configRef$current$de : [undefined]));
|
|
443
415
|
setError([]);
|
|
444
416
|
};
|
|
445
417
|
|
|
@@ -454,25 +426,34 @@ function useFieldList(ref, config) {
|
|
|
454
426
|
document.removeEventListener('reset', resetHandler);
|
|
455
427
|
};
|
|
456
428
|
}, [ref]);
|
|
457
|
-
return entries.map((
|
|
458
|
-
var
|
|
429
|
+
return entries.map((_ref6, index) => {
|
|
430
|
+
var _config$initialError2, _config$defaultValue2;
|
|
431
|
+
var [key, defaultValue] = _ref6;
|
|
459
432
|
var errors = error[index];
|
|
433
|
+
var initialError = Object.entries((_config$initialError2 = config.initialError) !== null && _config$initialError2 !== void 0 ? _config$initialError2 : {}).reduce((result, _ref7) => {
|
|
434
|
+
var [name, message] = _ref7;
|
|
435
|
+
var [field, ...paths] = getPaths(name);
|
|
436
|
+
if (field === index) {
|
|
437
|
+
result[getName(paths)] = message;
|
|
438
|
+
}
|
|
439
|
+
return result;
|
|
440
|
+
}, {});
|
|
460
441
|
var fieldConfig = {
|
|
461
442
|
name: "".concat(config.name, "[").concat(index, "]"),
|
|
462
|
-
defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue :
|
|
463
|
-
initialError
|
|
443
|
+
defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : (_config$defaultValue2 = config.defaultValue) === null || _config$defaultValue2 === void 0 ? void 0 : _config$defaultValue2[index],
|
|
444
|
+
initialError,
|
|
445
|
+
error: errors === null || errors === void 0 ? void 0 : errors[0],
|
|
446
|
+
errors
|
|
464
447
|
};
|
|
465
448
|
if (config.form) {
|
|
466
449
|
fieldConfig.form = config.form;
|
|
467
450
|
fieldConfig.id = "".concat(config.form, "-").concat(config.name);
|
|
468
451
|
fieldConfig.errorId = "".concat(fieldConfig.id, "-error");
|
|
452
|
+
fieldConfig.descriptionId = "".concat(fieldConfig.id, "-description");
|
|
469
453
|
}
|
|
470
|
-
return {
|
|
471
|
-
key
|
|
472
|
-
|
|
473
|
-
errors,
|
|
474
|
-
config: fieldConfig
|
|
475
|
-
};
|
|
454
|
+
return _objectSpread2({
|
|
455
|
+
key
|
|
456
|
+
}, fieldConfig);
|
|
476
457
|
});
|
|
477
458
|
}
|
|
478
459
|
|
package/module/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { isFieldElement, list, parse, requestIntent, validate, validateConstraint } from '@conform-to/dom';
|
|
2
2
|
export { useFieldList, useFieldset, useForm, useInputEvent } from './hooks.js';
|
|
3
3
|
import * as helpers from './helpers.js';
|
|
4
4
|
export { helpers as conform };
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@conform-to/react",
|
|
3
3
|
"description": "Conform view adapter for react",
|
|
4
|
+
"homepage": "https://conform.guide",
|
|
4
5
|
"license": "MIT",
|
|
5
|
-
"version": "0.6.
|
|
6
|
+
"version": "0.6.1",
|
|
6
7
|
"main": "index.js",
|
|
7
8
|
"module": "module/index.js",
|
|
8
9
|
"repository": {
|
|
@@ -19,7 +20,7 @@
|
|
|
19
20
|
"url": "https://github.com/edmundhung/conform/issues"
|
|
20
21
|
},
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"@conform-to/dom": "0.6.
|
|
23
|
+
"@conform-to/dom": "0.6.1"
|
|
23
24
|
},
|
|
24
25
|
"peerDependencies": {
|
|
25
26
|
"react": ">=16.8"
|
|
@@ -28,8 +29,11 @@
|
|
|
28
29
|
"constraint-validation",
|
|
29
30
|
"form",
|
|
30
31
|
"form-validation",
|
|
32
|
+
"html",
|
|
33
|
+
"progressive-enhancement",
|
|
31
34
|
"validation",
|
|
32
|
-
"react"
|
|
35
|
+
"react",
|
|
36
|
+
"remix"
|
|
33
37
|
],
|
|
34
38
|
"sideEffects": false
|
|
35
39
|
}
|
package/base.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { type InputHTMLAttributes, type RefObject } from 'react';
|
|
2
|
-
declare type EventLikeOrString = {
|
|
3
|
-
target: {
|
|
4
|
-
value: string;
|
|
5
|
-
};
|
|
6
|
-
} | string;
|
|
7
|
-
declare type InputControl = {
|
|
8
|
-
onChange: (eventLikeOrString: EventLikeOrString) => void;
|
|
9
|
-
onInput: (eventLikeOrString: EventLikeOrString) => void;
|
|
10
|
-
onFocus: () => void;
|
|
11
|
-
onBlur: () => void;
|
|
12
|
-
};
|
|
13
|
-
export declare function useInputControl(ref: RefObject<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>): InputControl;
|
|
14
|
-
export declare const BaseInput: import("react").ForwardRefExoticComponent<{
|
|
15
|
-
name: string;
|
|
16
|
-
} & Omit<InputHTMLAttributes<HTMLInputElement>, "name" | "type" | "value" | "defaultValue"> & import("react").RefAttributes<HTMLInputElement>>;
|
|
17
|
-
export {};
|
package/base.js
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
|
|
6
|
-
var react = require('react');
|
|
7
|
-
|
|
8
|
-
var _excluded = ["hidden", "className", "style"];
|
|
9
|
-
/**
|
|
10
|
-
* Triggering react custom change event
|
|
11
|
-
* Solution based on dom-testing-library
|
|
12
|
-
* @see https://github.com/facebook/react/issues/10135#issuecomment-401496776
|
|
13
|
-
* @see https://github.com/testing-library/dom-testing-library/blob/main/src/events.js#L104-L123
|
|
14
|
-
*/
|
|
15
|
-
function setNativeValue(element, value) {
|
|
16
|
-
var {
|
|
17
|
-
set: valueSetter
|
|
18
|
-
} = Object.getOwnPropertyDescriptor(element, 'value') || {};
|
|
19
|
-
var prototype = Object.getPrototypeOf(element);
|
|
20
|
-
var {
|
|
21
|
-
set: prototypeValueSetter
|
|
22
|
-
} = Object.getOwnPropertyDescriptor(prototype, 'value') || {};
|
|
23
|
-
if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
|
|
24
|
-
prototypeValueSetter.call(element, value);
|
|
25
|
-
} else {
|
|
26
|
-
if (valueSetter) {
|
|
27
|
-
valueSetter.call(element, value);
|
|
28
|
-
} else {
|
|
29
|
-
throw new Error('The given element does not have a value setter');
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function useInputControl(ref) {
|
|
34
|
-
function getInputElement() {
|
|
35
|
-
var $input = ref.current;
|
|
36
|
-
if (!$input) {
|
|
37
|
-
console.warn('input ref is not available; Maybe you forget to setup the ref?');
|
|
38
|
-
}
|
|
39
|
-
return $input;
|
|
40
|
-
}
|
|
41
|
-
return {
|
|
42
|
-
onChange(eventLikeOrString) {
|
|
43
|
-
var $input = getInputElement();
|
|
44
|
-
var value = typeof eventLikeOrString === 'string' ? eventLikeOrString : eventLikeOrString.target.value;
|
|
45
|
-
if ($input && $input.value !== value) {
|
|
46
|
-
$input.dispatchEvent(new InputEvent('beforeinput', {
|
|
47
|
-
bubbles: true,
|
|
48
|
-
cancelable: true
|
|
49
|
-
}));
|
|
50
|
-
setNativeValue($input, value);
|
|
51
|
-
$input.dispatchEvent(new InputEvent('input', {
|
|
52
|
-
bubbles: true,
|
|
53
|
-
cancelable: true
|
|
54
|
-
}));
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
onInput(eventLikeOrString) {
|
|
58
|
-
this.onChange(eventLikeOrString);
|
|
59
|
-
},
|
|
60
|
-
onFocus() {
|
|
61
|
-
var $input = getInputElement();
|
|
62
|
-
if ($input) {
|
|
63
|
-
$input.dispatchEvent(new FocusEvent('focusin', {
|
|
64
|
-
bubbles: true,
|
|
65
|
-
cancelable: true
|
|
66
|
-
}));
|
|
67
|
-
$input.dispatchEvent(new FocusEvent('focus', {
|
|
68
|
-
cancelable: true
|
|
69
|
-
}));
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
onBlur() {
|
|
73
|
-
var $input = getInputElement();
|
|
74
|
-
if ($input) {
|
|
75
|
-
$input.dispatchEvent(new FocusEvent('focusout', {
|
|
76
|
-
bubbles: true,
|
|
77
|
-
cancelable: true
|
|
78
|
-
}));
|
|
79
|
-
$input.dispatchEvent(new FocusEvent('blur', {
|
|
80
|
-
cancelable: true
|
|
81
|
-
}));
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
function BaseInputImpl(_ref, ref) {
|
|
87
|
-
var {
|
|
88
|
-
hidden = true,
|
|
89
|
-
className,
|
|
90
|
-
style
|
|
91
|
-
} = _ref,
|
|
92
|
-
props = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
|
|
93
|
-
return /*#__PURE__*/react.createElement('input', _rollupPluginBabelHelpers.objectSpread2({
|
|
94
|
-
ref,
|
|
95
|
-
className: hidden ? '' : className,
|
|
96
|
-
style: hidden ? {
|
|
97
|
-
position: 'absolute',
|
|
98
|
-
width: '1px',
|
|
99
|
-
height: '1px',
|
|
100
|
-
padding: 0,
|
|
101
|
-
margin: '-1px',
|
|
102
|
-
overflow: 'hidden',
|
|
103
|
-
clip: 'rect(0,0,0,0)',
|
|
104
|
-
whiteSpace: 'nowrap',
|
|
105
|
-
borderWidth: 0
|
|
106
|
-
} : style
|
|
107
|
-
}, props));
|
|
108
|
-
}
|
|
109
|
-
var BaseInput = /*#__PURE__*/react.forwardRef(BaseInputImpl);
|
|
110
|
-
|
|
111
|
-
exports.BaseInput = BaseInput;
|
|
112
|
-
exports.useInputControl = useInputControl;
|