@conform-to/react 0.4.0-pre.2 → 0.4.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.md +56 -120
- package/_virtual/_rollupPluginBabelHelpers.js +0 -6
- package/helpers.js +0 -9
- package/hooks.d.ts +23 -26
- package/hooks.js +57 -145
- package/index.d.ts +1 -1
- package/index.js +2 -6
- package/module/_virtual/_rollupPluginBabelHelpers.js +0 -6
- package/module/helpers.js +0 -9
- package/module/hooks.js +58 -146
- package/module/index.js +1 -1
- package/package.json +2 -2
package/hooks.js
CHANGED
|
@@ -11,7 +11,7 @@ var helpers = require('./helpers.js');
|
|
|
11
11
|
* Returns properties required to hook into form events.
|
|
12
12
|
* Applied custom validation and define when error should be reported.
|
|
13
13
|
*
|
|
14
|
-
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.
|
|
14
|
+
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.3/packages/conform-react/README.md#useform
|
|
15
15
|
*/
|
|
16
16
|
function useForm() {
|
|
17
17
|
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
@@ -19,7 +19,6 @@ function useForm() {
|
|
|
19
19
|
var ref = react.useRef(null);
|
|
20
20
|
var [error, setError] = react.useState(() => {
|
|
21
21
|
var _config$state$error$f, _config$state, _config$state$error;
|
|
22
|
-
|
|
23
22
|
var [, message] = (_config$state$error$f = (_config$state = config.state) === null || _config$state === void 0 ? void 0 : (_config$state$error = _config$state.error) === null || _config$state$error === void 0 ? void 0 : _config$state$error.find(_ref => {
|
|
24
23
|
var [key] = _ref;
|
|
25
24
|
return key === '';
|
|
@@ -28,7 +27,6 @@ function useForm() {
|
|
|
28
27
|
});
|
|
29
28
|
var [fieldsetConfig, setFieldsetConfig] = react.useState(() => {
|
|
30
29
|
var _config$state$error2, _config$state2, _config$state$value, _config$state3;
|
|
31
|
-
|
|
32
30
|
var error = (_config$state$error2 = (_config$state2 = config.state) === null || _config$state2 === void 0 ? void 0 : _config$state2.error) !== null && _config$state$error2 !== void 0 ? _config$state$error2 : [];
|
|
33
31
|
return {
|
|
34
32
|
defaultValue: (_config$state$value = (_config$state3 = config.state) === null || _config$state3 === void 0 ? void 0 : _config$state3.value) !== null && _config$state$value !== void 0 ? _config$state$value : config.defaultValue,
|
|
@@ -47,17 +45,13 @@ function useForm() {
|
|
|
47
45
|
}, []);
|
|
48
46
|
react.useEffect(() => {
|
|
49
47
|
var form = ref.current;
|
|
50
|
-
|
|
51
48
|
if (!form || !config.state) {
|
|
52
49
|
return;
|
|
53
50
|
}
|
|
54
|
-
|
|
55
51
|
dom.setFormError(form, config.state);
|
|
56
|
-
|
|
57
52
|
if (!form.reportValidity()) {
|
|
58
53
|
dom.focusFirstInvalidField(form);
|
|
59
54
|
}
|
|
60
|
-
|
|
61
55
|
dom.requestSubmit(form);
|
|
62
56
|
}, [config.state]);
|
|
63
57
|
react.useEffect(() => {
|
|
@@ -66,80 +60,66 @@ function useForm() {
|
|
|
66
60
|
var field = event.target;
|
|
67
61
|
var form = ref.current;
|
|
68
62
|
var formConfig = configRef.current;
|
|
69
|
-
|
|
70
63
|
if (!form || !dom.isFieldElement(field) || field.form !== form) {
|
|
71
64
|
return;
|
|
72
65
|
}
|
|
73
|
-
|
|
74
66
|
if (formConfig.initialReport === 'onChange') {
|
|
75
67
|
field.dataset.conformTouched = 'true';
|
|
76
68
|
}
|
|
77
|
-
|
|
78
69
|
if (field.dataset.conformTouched) {
|
|
79
70
|
dom.requestValidate(form, field.name);
|
|
80
71
|
}
|
|
81
72
|
};
|
|
82
|
-
|
|
83
73
|
var handleBlur = event => {
|
|
84
74
|
var field = event.target;
|
|
85
75
|
var form = ref.current;
|
|
86
76
|
var formConfig = configRef.current;
|
|
87
|
-
|
|
88
77
|
if (!form || !dom.isFieldElement(field) || field.form !== form) {
|
|
89
78
|
return;
|
|
90
79
|
}
|
|
91
|
-
|
|
92
80
|
if (formConfig.initialReport === 'onBlur' && !field.dataset.conformTouched) {
|
|
93
81
|
field.dataset.conformTouched = 'true';
|
|
94
82
|
dom.requestValidate(form, field.name);
|
|
95
83
|
}
|
|
96
84
|
};
|
|
97
|
-
|
|
98
85
|
var handleInvalid = event => {
|
|
99
86
|
var form = dom.getFormElement(ref.current);
|
|
100
87
|
var field = event.target;
|
|
101
|
-
|
|
102
88
|
if (!form || !dom.isFieldElement(field) || field.form !== form || field.name !== '') {
|
|
103
89
|
return;
|
|
104
90
|
}
|
|
105
|
-
|
|
106
91
|
event.preventDefault();
|
|
107
|
-
|
|
108
92
|
if (field.dataset.conformTouched) {
|
|
109
93
|
setError(field.validationMessage);
|
|
110
94
|
}
|
|
111
95
|
};
|
|
112
|
-
|
|
113
96
|
var handleReset = event => {
|
|
114
97
|
var form = ref.current;
|
|
115
98
|
var formConfig = configRef.current;
|
|
116
|
-
|
|
117
99
|
if (!form || event.target !== form) {
|
|
118
100
|
return;
|
|
119
|
-
}
|
|
120
|
-
|
|
101
|
+
}
|
|
121
102
|
|
|
103
|
+
// Reset all field state
|
|
122
104
|
for (var field of form.elements) {
|
|
123
105
|
if (dom.isFieldElement(field)) {
|
|
124
106
|
delete field.dataset.conformTouched;
|
|
125
107
|
field.setCustomValidity('');
|
|
126
108
|
}
|
|
127
109
|
}
|
|
128
|
-
|
|
129
110
|
setError('');
|
|
130
111
|
setFieldsetConfig({
|
|
131
112
|
defaultValue: formConfig.defaultValue,
|
|
132
113
|
initialError: []
|
|
133
114
|
});
|
|
134
115
|
};
|
|
116
|
+
|
|
135
117
|
/**
|
|
136
118
|
* The input event handler will be triggered in capturing phase in order to
|
|
137
119
|
* allow follow-up action in the bubble phase based on the latest validity
|
|
138
120
|
* E.g. `useFieldset` reset the error of valid field after checking the
|
|
139
121
|
* validity in the bubble phase.
|
|
140
122
|
*/
|
|
141
|
-
|
|
142
|
-
|
|
143
123
|
document.addEventListener('input', handleInput, true);
|
|
144
124
|
document.addEventListener('blur', handleBlur, true);
|
|
145
125
|
document.addEventListener('invalid', handleInvalid, true);
|
|
@@ -157,81 +137,78 @@ function useForm() {
|
|
|
157
137
|
props: {
|
|
158
138
|
ref,
|
|
159
139
|
noValidate,
|
|
160
|
-
|
|
161
140
|
onSubmit(event) {
|
|
162
141
|
var form = event.currentTarget;
|
|
163
142
|
var nativeEvent = event.nativeEvent;
|
|
164
143
|
var submitter = nativeEvent.submitter;
|
|
165
|
-
|
|
166
144
|
for (var element of form.elements) {
|
|
167
145
|
if (dom.isFieldElement(element) && element.name === '') {
|
|
168
146
|
setError(element.validationMessage);
|
|
169
147
|
break;
|
|
170
148
|
}
|
|
171
149
|
}
|
|
150
|
+
|
|
172
151
|
/**
|
|
173
152
|
* It checks defaultPrevented to confirm if the submission is intentional
|
|
174
153
|
* This is utilized by `useFieldList` to modify the list state when the submit
|
|
175
154
|
* event is captured and revalidate the form with new fields without triggering
|
|
176
155
|
* a form submission at the same time.
|
|
177
156
|
*/
|
|
178
|
-
|
|
179
|
-
|
|
180
157
|
if (!submitter || event.defaultPrevented) {
|
|
181
158
|
event.preventDefault();
|
|
182
159
|
return;
|
|
183
160
|
}
|
|
184
|
-
|
|
185
161
|
try {
|
|
162
|
+
var submission;
|
|
186
163
|
var formData = dom.getFormData(form, submitter);
|
|
187
|
-
var submission = dom.parse(formData);
|
|
188
|
-
var _context = {
|
|
189
|
-
form,
|
|
190
|
-
formData,
|
|
191
|
-
submission
|
|
192
|
-
}; // Touch all fields only if the submitter is not a command button
|
|
193
|
-
|
|
194
|
-
if (submission.context === 'submit') {
|
|
195
|
-
for (var field of form.elements) {
|
|
196
|
-
if (dom.isFieldElement(field)) {
|
|
197
|
-
// Mark the field as touched
|
|
198
|
-
field.dataset.conformTouched = 'true';
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
var _error;
|
|
204
|
-
|
|
205
164
|
if (typeof config.onValidate === 'function') {
|
|
206
|
-
|
|
165
|
+
submission = config.onValidate({
|
|
166
|
+
form,
|
|
167
|
+
formData
|
|
168
|
+
});
|
|
207
169
|
} else {
|
|
170
|
+
submission = dom.parse(formData);
|
|
208
171
|
if (config.mode !== 'server-validation') {
|
|
209
|
-
|
|
172
|
+
/**
|
|
173
|
+
* As there is no custom logic defined,
|
|
174
|
+
* removing the custom validity state will allow us
|
|
175
|
+
* finding the latest validation message.
|
|
176
|
+
*
|
|
177
|
+
* This is mainly used to showcase the constraint validation API.
|
|
178
|
+
*/
|
|
210
179
|
dom.setFormError(form, {
|
|
211
|
-
|
|
180
|
+
type: 'submit',
|
|
212
181
|
value: {},
|
|
213
182
|
error: []
|
|
214
183
|
});
|
|
184
|
+
for (var _element of form.elements) {
|
|
185
|
+
if (dom.isFieldElement(_element) && _element.willValidate) {
|
|
186
|
+
submission.error.push([_element.name, _element.validationMessage]);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
215
189
|
}
|
|
216
|
-
|
|
217
|
-
_error = dom.getFormError(form);
|
|
218
190
|
}
|
|
219
191
|
|
|
220
|
-
if
|
|
221
|
-
|
|
192
|
+
// Touch all fields only if the submitter is not a command button
|
|
193
|
+
if (submission.type === 'submit') {
|
|
194
|
+
for (var field of form.elements) {
|
|
195
|
+
if (dom.isFieldElement(field)) {
|
|
196
|
+
// Mark the field as touched
|
|
197
|
+
field.dataset.conformTouched = 'true';
|
|
198
|
+
}
|
|
199
|
+
}
|
|
222
200
|
}
|
|
223
|
-
|
|
224
|
-
if (!config.noValidate && !submitter.formNoValidate && dom.hasError(submission.error) || submission.context === 'validate' && config.mode !== 'server-validation') {
|
|
201
|
+
if (!config.noValidate && !submitter.formNoValidate && dom.hasError(submission.error) || submission.type === 'validate' && config.mode !== 'server-validation') {
|
|
225
202
|
event.preventDefault();
|
|
226
203
|
} else {
|
|
227
204
|
var _config$onSubmit;
|
|
228
|
-
|
|
229
|
-
|
|
205
|
+
(_config$onSubmit = config.onSubmit) === null || _config$onSubmit === void 0 ? void 0 : _config$onSubmit.call(config, event, {
|
|
206
|
+
formData,
|
|
207
|
+
submission
|
|
208
|
+
});
|
|
230
209
|
}
|
|
231
|
-
|
|
232
210
|
if (event.defaultPrevented) {
|
|
233
211
|
dom.setFormError(form, submission);
|
|
234
|
-
|
|
235
212
|
if (!form.reportValidity()) {
|
|
236
213
|
dom.focusFirstInvalidField(form);
|
|
237
214
|
}
|
|
@@ -240,34 +217,29 @@ function useForm() {
|
|
|
240
217
|
console.warn(e);
|
|
241
218
|
}
|
|
242
219
|
}
|
|
243
|
-
|
|
244
220
|
},
|
|
245
221
|
config: fieldsetConfig
|
|
246
222
|
};
|
|
247
223
|
}
|
|
224
|
+
|
|
248
225
|
/**
|
|
249
226
|
* All the information of the field, including state and config.
|
|
250
227
|
*/
|
|
251
228
|
|
|
252
229
|
function useFieldset(ref, config) {
|
|
253
230
|
var configRef = react.useRef(config);
|
|
254
|
-
var [uncontrolledState, setUncontrolledState] = react.useState(
|
|
231
|
+
var [uncontrolledState, setUncontrolledState] = react.useState(
|
|
232
|
+
// @ts-expect-error
|
|
255
233
|
() => {
|
|
256
234
|
var _config$defaultValue;
|
|
257
|
-
|
|
258
235
|
var initialError = {};
|
|
259
|
-
|
|
260
236
|
for (var [name, message] of (_config$initialError = config === null || config === void 0 ? void 0 : config.initialError) !== null && _config$initialError !== void 0 ? _config$initialError : []) {
|
|
261
237
|
var _config$initialError;
|
|
262
|
-
|
|
263
238
|
var [key, ...paths] = dom.getPaths(name);
|
|
264
|
-
|
|
265
239
|
if (typeof key === 'string') {
|
|
266
240
|
var _initialError$key;
|
|
267
|
-
|
|
268
241
|
var scopedName = dom.getName(paths);
|
|
269
242
|
var entries = (_initialError$key = initialError[key]) !== null && _initialError$key !== void 0 ? _initialError$key : [];
|
|
270
|
-
|
|
271
243
|
if (scopedName === '' && entries.length > 0 && entries[0][0] !== '') {
|
|
272
244
|
initialError[key] = [[scopedName, message], ...entries];
|
|
273
245
|
} else {
|
|
@@ -275,7 +247,6 @@ function useFieldset(ref, config) {
|
|
|
275
247
|
}
|
|
276
248
|
}
|
|
277
249
|
}
|
|
278
|
-
|
|
279
250
|
return {
|
|
280
251
|
defaultValue: (_config$defaultValue = config === null || config === void 0 ? void 0 : config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : {},
|
|
281
252
|
initialError
|
|
@@ -283,17 +254,13 @@ function useFieldset(ref, config) {
|
|
|
283
254
|
});
|
|
284
255
|
var [error, setError] = react.useState(() => {
|
|
285
256
|
var result = {};
|
|
286
|
-
|
|
287
257
|
for (var [key, entries] of Object.entries(uncontrolledState.initialError)) {
|
|
288
258
|
var _entries$;
|
|
289
|
-
|
|
290
259
|
var [name, message] = (_entries$ = entries === null || entries === void 0 ? void 0 : entries[0]) !== null && _entries$ !== void 0 ? _entries$ : [];
|
|
291
|
-
|
|
292
260
|
if (name === '') {
|
|
293
261
|
result[key] = message !== null && message !== void 0 ? message : '';
|
|
294
262
|
}
|
|
295
263
|
}
|
|
296
|
-
|
|
297
264
|
return result;
|
|
298
265
|
});
|
|
299
266
|
react.useEffect(() => {
|
|
@@ -301,67 +268,54 @@ function useFieldset(ref, config) {
|
|
|
301
268
|
});
|
|
302
269
|
react.useEffect(() => {
|
|
303
270
|
var invalidHandler = event => {
|
|
304
|
-
var _configRef$current$na
|
|
305
|
-
|
|
271
|
+
var _configRef$current$na;
|
|
306
272
|
var form = dom.getFormElement(ref.current);
|
|
307
273
|
var field = event.target;
|
|
308
|
-
var fieldsetName = (_configRef$current$na =
|
|
309
|
-
|
|
274
|
+
var fieldsetName = (_configRef$current$na = configRef.current.name) !== null && _configRef$current$na !== void 0 ? _configRef$current$na : '';
|
|
310
275
|
if (!form || !dom.isFieldElement(field) || field.form !== form || !field.name.startsWith(fieldsetName)) {
|
|
311
276
|
return;
|
|
312
277
|
}
|
|
278
|
+
var [key, ...paths] = dom.getPaths(fieldsetName.length > 0 ? field.name.slice(fieldsetName.length + 1) : field.name);
|
|
313
279
|
|
|
314
|
-
|
|
315
|
-
|
|
280
|
+
// Update the error only if the field belongs to the fieldset
|
|
316
281
|
if (typeof key === 'string' && paths.length === 0) {
|
|
317
282
|
if (field.dataset.conformTouched) {
|
|
318
283
|
setError(prev => {
|
|
319
284
|
var _prev$key;
|
|
320
|
-
|
|
321
285
|
var prevMessage = (_prev$key = prev === null || prev === void 0 ? void 0 : prev[key]) !== null && _prev$key !== void 0 ? _prev$key : '';
|
|
322
|
-
|
|
323
286
|
if (prevMessage === field.validationMessage) {
|
|
324
287
|
return prev;
|
|
325
288
|
}
|
|
326
|
-
|
|
327
289
|
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, prev), {}, {
|
|
328
290
|
[key]: field.validationMessage
|
|
329
291
|
});
|
|
330
292
|
});
|
|
331
293
|
}
|
|
332
|
-
|
|
333
294
|
event.preventDefault();
|
|
334
295
|
}
|
|
335
296
|
};
|
|
336
|
-
|
|
337
297
|
var submitHandler = event => {
|
|
338
298
|
var form = dom.getFormElement(ref.current);
|
|
339
|
-
|
|
340
299
|
if (!form || event.target !== form) {
|
|
341
300
|
return;
|
|
342
301
|
}
|
|
302
|
+
|
|
343
303
|
/**
|
|
344
304
|
* Reset the error state of each field if its validity is changed.
|
|
345
305
|
*
|
|
346
306
|
* This is a workaround as no official way is provided to notify
|
|
347
307
|
* when the validity of the field is changed from `invalid` to `valid`.
|
|
348
308
|
*/
|
|
349
|
-
|
|
350
|
-
|
|
351
309
|
setError(prev => {
|
|
352
|
-
var _configRef$current$na2
|
|
353
|
-
|
|
310
|
+
var _configRef$current$na2;
|
|
354
311
|
var next = prev;
|
|
355
|
-
var fieldsetName = (_configRef$current$na2 =
|
|
356
|
-
|
|
312
|
+
var fieldsetName = (_configRef$current$na2 = configRef.current.name) !== null && _configRef$current$na2 !== void 0 ? _configRef$current$na2 : '';
|
|
357
313
|
for (var field of form.elements) {
|
|
358
314
|
if (dom.isFieldElement(field) && field.name.startsWith(fieldsetName)) {
|
|
359
315
|
var _next$key, _next;
|
|
360
|
-
|
|
361
316
|
var key = fieldsetName ? field.name.slice(fieldsetName.length + 1) : field.name;
|
|
362
317
|
var prevMessage = (_next$key = (_next = next) === null || _next === void 0 ? void 0 : _next[key]) !== null && _next$key !== void 0 ? _next$key : '';
|
|
363
318
|
var nextMessage = field.validationMessage;
|
|
364
|
-
|
|
365
319
|
if (prevMessage !== '' && nextMessage === '') {
|
|
366
320
|
next = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, next), {}, {
|
|
367
321
|
[key]: ''
|
|
@@ -369,20 +323,15 @@ function useFieldset(ref, config) {
|
|
|
369
323
|
}
|
|
370
324
|
}
|
|
371
325
|
}
|
|
372
|
-
|
|
373
326
|
return next;
|
|
374
327
|
});
|
|
375
328
|
};
|
|
376
|
-
|
|
377
329
|
var resetHandler = event => {
|
|
378
330
|
var _fieldsetConfig$defau;
|
|
379
|
-
|
|
380
331
|
var form = dom.getFormElement(ref.current);
|
|
381
|
-
|
|
382
332
|
if (!form || event.target !== form) {
|
|
383
333
|
return;
|
|
384
334
|
}
|
|
385
|
-
|
|
386
335
|
var fieldsetConfig = configRef.current;
|
|
387
336
|
setUncontrolledState({
|
|
388
337
|
// @ts-expect-error
|
|
@@ -390,9 +339,9 @@ function useFieldset(ref, config) {
|
|
|
390
339
|
initialError: {}
|
|
391
340
|
});
|
|
392
341
|
setError({});
|
|
393
|
-
};
|
|
394
|
-
|
|
342
|
+
};
|
|
395
343
|
|
|
344
|
+
// The invalid event does not bubble and so listening on the capturing pharse is needed
|
|
396
345
|
document.addEventListener('invalid', invalidHandler, true);
|
|
397
346
|
document.addEventListener('submit', submitHandler);
|
|
398
347
|
document.addEventListener('reset', resetHandler);
|
|
@@ -402,20 +351,18 @@ function useFieldset(ref, config) {
|
|
|
402
351
|
document.removeEventListener('reset', resetHandler);
|
|
403
352
|
};
|
|
404
353
|
}, [ref]);
|
|
354
|
+
|
|
405
355
|
/**
|
|
406
356
|
* This allows us constructing the field at runtime as we have no information
|
|
407
357
|
* about which fields would be available. The proxy will also help tracking
|
|
408
358
|
* the usage of each field for optimization in the future.
|
|
409
359
|
*/
|
|
410
|
-
|
|
411
360
|
return new Proxy({}, {
|
|
412
361
|
get(_target, key) {
|
|
413
362
|
var _fieldsetConfig$const, _error$key;
|
|
414
|
-
|
|
415
363
|
if (typeof key !== 'string') {
|
|
416
364
|
return;
|
|
417
365
|
}
|
|
418
|
-
|
|
419
366
|
var fieldsetConfig = config !== null && config !== void 0 ? config : {};
|
|
420
367
|
var constraint = (_fieldsetConfig$const = fieldsetConfig.constraint) === null || _fieldsetConfig$const === void 0 ? void 0 : _fieldsetConfig$const[key];
|
|
421
368
|
var field = {
|
|
@@ -429,35 +376,26 @@ function useFieldset(ref, config) {
|
|
|
429
376
|
};
|
|
430
377
|
return field;
|
|
431
378
|
}
|
|
432
|
-
|
|
433
379
|
});
|
|
434
380
|
}
|
|
435
|
-
|
|
436
381
|
/**
|
|
437
382
|
* Returns a list of key and config, with a group of helpers
|
|
438
383
|
* configuring buttons for list manipulation
|
|
439
384
|
*
|
|
440
|
-
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.
|
|
385
|
+
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.3/packages/conform-react/README.md#usefieldlist
|
|
441
386
|
*/
|
|
442
387
|
function useFieldList(ref, config) {
|
|
443
388
|
var configRef = react.useRef(config);
|
|
444
389
|
var [uncontrolledState, setUncontrolledState] = react.useState(() => {
|
|
445
390
|
var _config$defaultValue2;
|
|
446
|
-
|
|
447
391
|
var initialError = [];
|
|
448
|
-
|
|
449
392
|
for (var [name, message] of (_config$initialError2 = config === null || config === void 0 ? void 0 : config.initialError) !== null && _config$initialError2 !== void 0 ? _config$initialError2 : []) {
|
|
450
393
|
var _config$initialError2;
|
|
451
|
-
|
|
452
394
|
var [index, ...paths] = dom.getPaths(name);
|
|
453
|
-
|
|
454
395
|
if (typeof index === 'number') {
|
|
455
396
|
var _initialError$index;
|
|
456
|
-
|
|
457
397
|
var scopedName = dom.getName(paths);
|
|
458
|
-
|
|
459
398
|
var _entries = (_initialError$index = initialError[index]) !== null && _initialError$index !== void 0 ? _initialError$index : [];
|
|
460
|
-
|
|
461
399
|
if (scopedName === '' && _entries.length > 0 && _entries[0][0] !== '') {
|
|
462
400
|
initialError[index] = [[scopedName, message], ..._entries];
|
|
463
401
|
} else {
|
|
@@ -465,7 +403,6 @@ function useFieldList(ref, config) {
|
|
|
465
403
|
}
|
|
466
404
|
}
|
|
467
405
|
}
|
|
468
|
-
|
|
469
406
|
return {
|
|
470
407
|
defaultValue: (_config$defaultValue2 = config.defaultValue) !== null && _config$defaultValue2 !== void 0 ? _config$defaultValue2 : [],
|
|
471
408
|
initialError
|
|
@@ -473,7 +410,6 @@ function useFieldList(ref, config) {
|
|
|
473
410
|
});
|
|
474
411
|
var [entries, setEntries] = react.useState(() => {
|
|
475
412
|
var _config$defaultValue3;
|
|
476
|
-
|
|
477
413
|
return Object.entries((_config$defaultValue3 = config.defaultValue) !== null && _config$defaultValue3 !== void 0 ? _config$defaultValue3 : [undefined]);
|
|
478
414
|
});
|
|
479
415
|
var list = entries.map((_ref3, index) => {
|
|
@@ -488,12 +424,12 @@ function useFieldList(ref, config) {
|
|
|
488
424
|
}
|
|
489
425
|
};
|
|
490
426
|
});
|
|
427
|
+
|
|
491
428
|
/***
|
|
492
429
|
* This use proxy to capture all information about the command and
|
|
493
430
|
* have it encoded in the value.
|
|
494
431
|
*/
|
|
495
|
-
|
|
496
|
-
var control = new Proxy({}, {
|
|
432
|
+
var command = new Proxy({}, {
|
|
497
433
|
get(_target, type) {
|
|
498
434
|
return function () {
|
|
499
435
|
var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
@@ -509,7 +445,6 @@ function useFieldList(ref, config) {
|
|
|
509
445
|
};
|
|
510
446
|
};
|
|
511
447
|
}
|
|
512
|
-
|
|
513
448
|
});
|
|
514
449
|
react.useEffect(() => {
|
|
515
450
|
configRef.current = config;
|
|
@@ -517,18 +452,14 @@ function useFieldList(ref, config) {
|
|
|
517
452
|
react.useEffect(() => {
|
|
518
453
|
var submitHandler = event => {
|
|
519
454
|
var form = dom.getFormElement(ref.current);
|
|
520
|
-
|
|
521
455
|
if (!form || event.target !== form || !(event.submitter instanceof HTMLButtonElement) || event.submitter.name !== 'conform/list') {
|
|
522
456
|
return;
|
|
523
457
|
}
|
|
524
|
-
|
|
525
458
|
var command = dom.parseListCommand(event.submitter.value);
|
|
526
|
-
|
|
527
459
|
if (command.scope !== configRef.current.name) {
|
|
528
460
|
// Ensure the scope of the listener are limited to specific field name
|
|
529
461
|
return;
|
|
530
462
|
}
|
|
531
|
-
|
|
532
463
|
setEntries(entries => {
|
|
533
464
|
switch (command.type) {
|
|
534
465
|
case 'append':
|
|
@@ -539,7 +470,6 @@ function useFieldList(ref, config) {
|
|
|
539
470
|
defaultValue: ["".concat(Date.now()), command.payload.defaultValue]
|
|
540
471
|
})
|
|
541
472
|
}));
|
|
542
|
-
|
|
543
473
|
default:
|
|
544
474
|
{
|
|
545
475
|
return dom.updateList([...(entries !== null && entries !== void 0 ? entries : [])], command);
|
|
@@ -548,16 +478,12 @@ function useFieldList(ref, config) {
|
|
|
548
478
|
});
|
|
549
479
|
event.preventDefault();
|
|
550
480
|
};
|
|
551
|
-
|
|
552
481
|
var resetHandler = event => {
|
|
553
482
|
var _fieldConfig$defaultV, _fieldConfig$defaultV2;
|
|
554
|
-
|
|
555
483
|
var form = dom.getFormElement(ref.current);
|
|
556
|
-
|
|
557
484
|
if (!form || event.target !== form) {
|
|
558
485
|
return;
|
|
559
486
|
}
|
|
560
|
-
|
|
561
487
|
var fieldConfig = configRef.current;
|
|
562
488
|
setUncontrolledState({
|
|
563
489
|
defaultValue: (_fieldConfig$defaultV = fieldConfig.defaultValue) !== null && _fieldConfig$defaultV !== void 0 ? _fieldConfig$defaultV : [],
|
|
@@ -565,7 +491,6 @@ function useFieldList(ref, config) {
|
|
|
565
491
|
});
|
|
566
492
|
setEntries(Object.entries((_fieldConfig$defaultV2 = fieldConfig.defaultValue) !== null && _fieldConfig$defaultV2 !== void 0 ? _fieldConfig$defaultV2 : [undefined]));
|
|
567
493
|
};
|
|
568
|
-
|
|
569
494
|
document.addEventListener('submit', submitHandler, true);
|
|
570
495
|
document.addEventListener('reset', resetHandler);
|
|
571
496
|
return () => {
|
|
@@ -573,19 +498,19 @@ function useFieldList(ref, config) {
|
|
|
573
498
|
document.removeEventListener('reset', resetHandler);
|
|
574
499
|
};
|
|
575
500
|
}, [ref]);
|
|
576
|
-
return [list,
|
|
501
|
+
return [list,
|
|
502
|
+
// @ts-expect-error proxy type
|
|
503
|
+
command];
|
|
577
504
|
}
|
|
578
|
-
|
|
579
505
|
/**
|
|
580
506
|
* Returns the properties required to configure a shadow input for validation.
|
|
581
507
|
* This is particular useful when integrating dropdown and datepicker whichs
|
|
582
508
|
* introduces custom input mode.
|
|
583
509
|
*
|
|
584
|
-
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.
|
|
510
|
+
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.3/packages/conform-react/README.md#usecontrolledinput
|
|
585
511
|
*/
|
|
586
512
|
function useControlledInput(config) {
|
|
587
513
|
var _config$defaultValue4;
|
|
588
|
-
|
|
589
514
|
var ref = react.useRef(null);
|
|
590
515
|
var inputRef = react.useRef(null);
|
|
591
516
|
var configRef = react.useRef(config);
|
|
@@ -594,12 +519,10 @@ function useControlledInput(config) {
|
|
|
594
519
|
initialError: config.initialError
|
|
595
520
|
});
|
|
596
521
|
var [value, setValue] = react.useState("".concat((_config$defaultValue4 = config.defaultValue) !== null && _config$defaultValue4 !== void 0 ? _config$defaultValue4 : ''));
|
|
597
|
-
|
|
598
522
|
var handleChange = eventOrValue => {
|
|
599
523
|
if (!ref.current) {
|
|
600
524
|
return;
|
|
601
525
|
}
|
|
602
|
-
|
|
603
526
|
var newValue = typeof eventOrValue === 'string' ? eventOrValue : eventOrValue.target.value;
|
|
604
527
|
ref.current.value = newValue;
|
|
605
528
|
ref.current.dispatchEvent(new InputEvent('input', {
|
|
@@ -607,39 +530,31 @@ function useControlledInput(config) {
|
|
|
607
530
|
}));
|
|
608
531
|
setValue(newValue);
|
|
609
532
|
};
|
|
610
|
-
|
|
611
533
|
var handleBlur = () => {
|
|
612
534
|
var _ref$current;
|
|
613
|
-
|
|
614
535
|
(_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.dispatchEvent(new FocusEvent('blur', {
|
|
615
536
|
bubbles: true
|
|
616
537
|
}));
|
|
617
538
|
};
|
|
618
|
-
|
|
619
539
|
var handleInvalid = event => {
|
|
620
540
|
event.preventDefault();
|
|
621
541
|
};
|
|
622
|
-
|
|
623
542
|
react.useEffect(() => {
|
|
624
543
|
configRef.current = config;
|
|
625
544
|
});
|
|
626
545
|
react.useEffect(() => {
|
|
627
546
|
var resetHandler = event => {
|
|
628
547
|
var _configRef$current$de;
|
|
629
|
-
|
|
630
548
|
var form = dom.getFormElement(ref.current);
|
|
631
|
-
|
|
632
549
|
if (!form || event.target !== form) {
|
|
633
550
|
return;
|
|
634
551
|
}
|
|
635
|
-
|
|
636
552
|
setUncontrolledState({
|
|
637
553
|
defaultValue: configRef.current.defaultValue,
|
|
638
554
|
initialError: configRef.current.initialError
|
|
639
555
|
});
|
|
640
556
|
setValue("".concat((_configRef$current$de = configRef.current.defaultValue) !== null && _configRef$current$de !== void 0 ? _configRef$current$de : ''));
|
|
641
557
|
};
|
|
642
|
-
|
|
643
558
|
document.addEventListener('reset', resetHandler);
|
|
644
559
|
return () => {
|
|
645
560
|
document.removeEventListener('reset', resetHandler);
|
|
@@ -658,13 +573,10 @@ function useControlledInput(config) {
|
|
|
658
573
|
whiteSpace: 'nowrap',
|
|
659
574
|
borderWidth: 0
|
|
660
575
|
},
|
|
661
|
-
|
|
662
576
|
onFocus() {
|
|
663
577
|
var _inputRef$current;
|
|
664
|
-
|
|
665
578
|
(_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.focus();
|
|
666
579
|
}
|
|
667
|
-
|
|
668
580
|
}, helpers.input(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, config), uncontrolledState), {
|
|
669
581
|
type: 'text'
|
|
670
582
|
})), {
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { type FieldsetConstraint, type Submission,
|
|
1
|
+
export { type FieldsetConstraint, type Submission, getFormElements, hasError, parse, shouldValidate, } from '@conform-to/dom';
|
|
2
2
|
export * from './hooks';
|
|
3
3
|
export * as conform from './helpers';
|
package/index.js
CHANGED
|
@@ -8,18 +8,14 @@ var helpers = require('./helpers.js');
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
Object.defineProperty(exports, '
|
|
11
|
+
Object.defineProperty(exports, 'getFormElements', {
|
|
12
12
|
enumerable: true,
|
|
13
|
-
get: function () { return dom.
|
|
13
|
+
get: function () { return dom.getFormElements; }
|
|
14
14
|
});
|
|
15
15
|
Object.defineProperty(exports, 'hasError', {
|
|
16
16
|
enumerable: true,
|
|
17
17
|
get: function () { return dom.hasError; }
|
|
18
18
|
});
|
|
19
|
-
Object.defineProperty(exports, 'isFieldElement', {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
get: function () { return dom.isFieldElement; }
|
|
22
|
-
});
|
|
23
19
|
Object.defineProperty(exports, 'parse', {
|
|
24
20
|
enumerable: true,
|
|
25
21
|
get: function () { return dom.parse; }
|