@conform-to/react 0.4.0-pre.3 → 0.4.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/_virtual/_rollupPluginBabelHelpers.js +0 -6
- package/base.d.ts +17 -0
- package/base.js +112 -0
- package/helpers.d.ts +34 -5
- package/helpers.js +0 -9
- package/hooks.d.ts +4 -4
- package/hooks.js +25 -122
- package/index.d.ts +1 -1
- package/module/_virtual/_rollupPluginBabelHelpers.js +0 -6
- package/module/base.js +107 -0
- package/module/helpers.js +0 -9
- package/module/hooks.js +25 -122
- 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.
|
|
14
|
+
* @see https://github.com/edmundhung/conform/tree/v0.4.1/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,35 +137,30 @@ 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
|
-
if (dom.isFieldElement(element) && element.name === '') {
|
|
145
|
+
if (dom.isFieldElement(element) && element.name === '' && element.willValidate) {
|
|
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 {
|
|
186
162
|
var submission;
|
|
187
163
|
var formData = dom.getFormData(form, submitter);
|
|
188
|
-
|
|
189
164
|
if (typeof config.onValidate === 'function') {
|
|
190
165
|
submission = config.onValidate({
|
|
191
166
|
form,
|
|
@@ -193,7 +168,6 @@ function useForm() {
|
|
|
193
168
|
});
|
|
194
169
|
} else {
|
|
195
170
|
submission = dom.parse(formData);
|
|
196
|
-
|
|
197
171
|
if (config.mode !== 'server-validation') {
|
|
198
172
|
/**
|
|
199
173
|
* As there is no custom logic defined,
|
|
@@ -207,16 +181,15 @@ function useForm() {
|
|
|
207
181
|
value: {},
|
|
208
182
|
error: []
|
|
209
183
|
});
|
|
210
|
-
|
|
211
184
|
for (var _element of form.elements) {
|
|
212
185
|
if (dom.isFieldElement(_element) && _element.willValidate) {
|
|
213
186
|
submission.error.push([_element.name, _element.validationMessage]);
|
|
214
187
|
}
|
|
215
188
|
}
|
|
216
189
|
}
|
|
217
|
-
}
|
|
218
|
-
|
|
190
|
+
}
|
|
219
191
|
|
|
192
|
+
// Touch all fields only if the submitter is not a command button
|
|
220
193
|
if (submission.type === 'submit') {
|
|
221
194
|
for (var field of form.elements) {
|
|
222
195
|
if (dom.isFieldElement(field)) {
|
|
@@ -225,21 +198,17 @@ function useForm() {
|
|
|
225
198
|
}
|
|
226
199
|
}
|
|
227
200
|
}
|
|
228
|
-
|
|
229
201
|
if (!config.noValidate && !submitter.formNoValidate && dom.hasError(submission.error) || submission.type === 'validate' && config.mode !== 'server-validation') {
|
|
230
202
|
event.preventDefault();
|
|
231
203
|
} else {
|
|
232
204
|
var _config$onSubmit;
|
|
233
|
-
|
|
234
205
|
(_config$onSubmit = config.onSubmit) === null || _config$onSubmit === void 0 ? void 0 : _config$onSubmit.call(config, event, {
|
|
235
206
|
formData,
|
|
236
207
|
submission
|
|
237
208
|
});
|
|
238
209
|
}
|
|
239
|
-
|
|
240
210
|
if (event.defaultPrevented) {
|
|
241
211
|
dom.setFormError(form, submission);
|
|
242
|
-
|
|
243
212
|
if (!form.reportValidity()) {
|
|
244
213
|
dom.focusFirstInvalidField(form);
|
|
245
214
|
}
|
|
@@ -248,34 +217,29 @@ function useForm() {
|
|
|
248
217
|
console.warn(e);
|
|
249
218
|
}
|
|
250
219
|
}
|
|
251
|
-
|
|
252
220
|
},
|
|
253
221
|
config: fieldsetConfig
|
|
254
222
|
};
|
|
255
223
|
}
|
|
224
|
+
|
|
256
225
|
/**
|
|
257
226
|
* All the information of the field, including state and config.
|
|
258
227
|
*/
|
|
259
228
|
|
|
260
229
|
function useFieldset(ref, config) {
|
|
261
230
|
var configRef = react.useRef(config);
|
|
262
|
-
var [uncontrolledState, setUncontrolledState] = react.useState(
|
|
231
|
+
var [uncontrolledState, setUncontrolledState] = react.useState(
|
|
232
|
+
// @ts-expect-error
|
|
263
233
|
() => {
|
|
264
234
|
var _config$defaultValue;
|
|
265
|
-
|
|
266
235
|
var initialError = {};
|
|
267
|
-
|
|
268
236
|
for (var [name, message] of (_config$initialError = config === null || config === void 0 ? void 0 : config.initialError) !== null && _config$initialError !== void 0 ? _config$initialError : []) {
|
|
269
237
|
var _config$initialError;
|
|
270
|
-
|
|
271
238
|
var [key, ...paths] = dom.getPaths(name);
|
|
272
|
-
|
|
273
239
|
if (typeof key === 'string') {
|
|
274
240
|
var _initialError$key;
|
|
275
|
-
|
|
276
241
|
var scopedName = dom.getName(paths);
|
|
277
242
|
var entries = (_initialError$key = initialError[key]) !== null && _initialError$key !== void 0 ? _initialError$key : [];
|
|
278
|
-
|
|
279
243
|
if (scopedName === '' && entries.length > 0 && entries[0][0] !== '') {
|
|
280
244
|
initialError[key] = [[scopedName, message], ...entries];
|
|
281
245
|
} else {
|
|
@@ -283,7 +247,6 @@ function useFieldset(ref, config) {
|
|
|
283
247
|
}
|
|
284
248
|
}
|
|
285
249
|
}
|
|
286
|
-
|
|
287
250
|
return {
|
|
288
251
|
defaultValue: (_config$defaultValue = config === null || config === void 0 ? void 0 : config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : {},
|
|
289
252
|
initialError
|
|
@@ -291,17 +254,13 @@ function useFieldset(ref, config) {
|
|
|
291
254
|
});
|
|
292
255
|
var [error, setError] = react.useState(() => {
|
|
293
256
|
var result = {};
|
|
294
|
-
|
|
295
257
|
for (var [key, entries] of Object.entries(uncontrolledState.initialError)) {
|
|
296
258
|
var _entries$;
|
|
297
|
-
|
|
298
259
|
var [name, message] = (_entries$ = entries === null || entries === void 0 ? void 0 : entries[0]) !== null && _entries$ !== void 0 ? _entries$ : [];
|
|
299
|
-
|
|
300
260
|
if (name === '') {
|
|
301
261
|
result[key] = message !== null && message !== void 0 ? message : '';
|
|
302
262
|
}
|
|
303
263
|
}
|
|
304
|
-
|
|
305
264
|
return result;
|
|
306
265
|
});
|
|
307
266
|
react.useEffect(() => {
|
|
@@ -310,66 +269,53 @@ function useFieldset(ref, config) {
|
|
|
310
269
|
react.useEffect(() => {
|
|
311
270
|
var invalidHandler = event => {
|
|
312
271
|
var _configRef$current$na;
|
|
313
|
-
|
|
314
272
|
var form = dom.getFormElement(ref.current);
|
|
315
273
|
var field = event.target;
|
|
316
274
|
var fieldsetName = (_configRef$current$na = configRef.current.name) !== null && _configRef$current$na !== void 0 ? _configRef$current$na : '';
|
|
317
|
-
|
|
318
275
|
if (!form || !dom.isFieldElement(field) || field.form !== form || !field.name.startsWith(fieldsetName)) {
|
|
319
276
|
return;
|
|
320
277
|
}
|
|
278
|
+
var [key, ...paths] = dom.getPaths(fieldsetName.length > 0 ? field.name.slice(fieldsetName.length + 1) : field.name);
|
|
321
279
|
|
|
322
|
-
|
|
323
|
-
|
|
280
|
+
// Update the error only if the field belongs to the fieldset
|
|
324
281
|
if (typeof key === 'string' && paths.length === 0) {
|
|
325
282
|
if (field.dataset.conformTouched) {
|
|
326
283
|
setError(prev => {
|
|
327
284
|
var _prev$key;
|
|
328
|
-
|
|
329
285
|
var prevMessage = (_prev$key = prev === null || prev === void 0 ? void 0 : prev[key]) !== null && _prev$key !== void 0 ? _prev$key : '';
|
|
330
|
-
|
|
331
286
|
if (prevMessage === field.validationMessage) {
|
|
332
287
|
return prev;
|
|
333
288
|
}
|
|
334
|
-
|
|
335
289
|
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, prev), {}, {
|
|
336
290
|
[key]: field.validationMessage
|
|
337
291
|
});
|
|
338
292
|
});
|
|
339
293
|
}
|
|
340
|
-
|
|
341
294
|
event.preventDefault();
|
|
342
295
|
}
|
|
343
296
|
};
|
|
344
|
-
|
|
345
297
|
var submitHandler = event => {
|
|
346
298
|
var form = dom.getFormElement(ref.current);
|
|
347
|
-
|
|
348
299
|
if (!form || event.target !== form) {
|
|
349
300
|
return;
|
|
350
301
|
}
|
|
302
|
+
|
|
351
303
|
/**
|
|
352
304
|
* Reset the error state of each field if its validity is changed.
|
|
353
305
|
*
|
|
354
306
|
* This is a workaround as no official way is provided to notify
|
|
355
307
|
* when the validity of the field is changed from `invalid` to `valid`.
|
|
356
308
|
*/
|
|
357
|
-
|
|
358
|
-
|
|
359
309
|
setError(prev => {
|
|
360
310
|
var _configRef$current$na2;
|
|
361
|
-
|
|
362
311
|
var next = prev;
|
|
363
312
|
var fieldsetName = (_configRef$current$na2 = configRef.current.name) !== null && _configRef$current$na2 !== void 0 ? _configRef$current$na2 : '';
|
|
364
|
-
|
|
365
313
|
for (var field of form.elements) {
|
|
366
314
|
if (dom.isFieldElement(field) && field.name.startsWith(fieldsetName)) {
|
|
367
315
|
var _next$key, _next;
|
|
368
|
-
|
|
369
316
|
var key = fieldsetName ? field.name.slice(fieldsetName.length + 1) : field.name;
|
|
370
317
|
var prevMessage = (_next$key = (_next = next) === null || _next === void 0 ? void 0 : _next[key]) !== null && _next$key !== void 0 ? _next$key : '';
|
|
371
318
|
var nextMessage = field.validationMessage;
|
|
372
|
-
|
|
373
319
|
if (prevMessage !== '' && nextMessage === '') {
|
|
374
320
|
next = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, next), {}, {
|
|
375
321
|
[key]: ''
|
|
@@ -377,20 +323,15 @@ function useFieldset(ref, config) {
|
|
|
377
323
|
}
|
|
378
324
|
}
|
|
379
325
|
}
|
|
380
|
-
|
|
381
326
|
return next;
|
|
382
327
|
});
|
|
383
328
|
};
|
|
384
|
-
|
|
385
329
|
var resetHandler = event => {
|
|
386
330
|
var _fieldsetConfig$defau;
|
|
387
|
-
|
|
388
331
|
var form = dom.getFormElement(ref.current);
|
|
389
|
-
|
|
390
332
|
if (!form || event.target !== form) {
|
|
391
333
|
return;
|
|
392
334
|
}
|
|
393
|
-
|
|
394
335
|
var fieldsetConfig = configRef.current;
|
|
395
336
|
setUncontrolledState({
|
|
396
337
|
// @ts-expect-error
|
|
@@ -398,9 +339,9 @@ function useFieldset(ref, config) {
|
|
|
398
339
|
initialError: {}
|
|
399
340
|
});
|
|
400
341
|
setError({});
|
|
401
|
-
};
|
|
402
|
-
|
|
342
|
+
};
|
|
403
343
|
|
|
344
|
+
// The invalid event does not bubble and so listening on the capturing pharse is needed
|
|
404
345
|
document.addEventListener('invalid', invalidHandler, true);
|
|
405
346
|
document.addEventListener('submit', submitHandler);
|
|
406
347
|
document.addEventListener('reset', resetHandler);
|
|
@@ -410,20 +351,18 @@ function useFieldset(ref, config) {
|
|
|
410
351
|
document.removeEventListener('reset', resetHandler);
|
|
411
352
|
};
|
|
412
353
|
}, [ref]);
|
|
354
|
+
|
|
413
355
|
/**
|
|
414
356
|
* This allows us constructing the field at runtime as we have no information
|
|
415
357
|
* about which fields would be available. The proxy will also help tracking
|
|
416
358
|
* the usage of each field for optimization in the future.
|
|
417
359
|
*/
|
|
418
|
-
|
|
419
360
|
return new Proxy({}, {
|
|
420
361
|
get(_target, key) {
|
|
421
362
|
var _fieldsetConfig$const, _error$key;
|
|
422
|
-
|
|
423
363
|
if (typeof key !== 'string') {
|
|
424
364
|
return;
|
|
425
365
|
}
|
|
426
|
-
|
|
427
366
|
var fieldsetConfig = config !== null && config !== void 0 ? config : {};
|
|
428
367
|
var constraint = (_fieldsetConfig$const = fieldsetConfig.constraint) === null || _fieldsetConfig$const === void 0 ? void 0 : _fieldsetConfig$const[key];
|
|
429
368
|
var field = {
|
|
@@ -437,35 +376,26 @@ function useFieldset(ref, config) {
|
|
|
437
376
|
};
|
|
438
377
|
return field;
|
|
439
378
|
}
|
|
440
|
-
|
|
441
379
|
});
|
|
442
380
|
}
|
|
443
|
-
|
|
444
381
|
/**
|
|
445
382
|
* Returns a list of key and config, with a group of helpers
|
|
446
383
|
* configuring buttons for list manipulation
|
|
447
384
|
*
|
|
448
|
-
* @see https://github.com/edmundhung/conform/tree/v0.4.
|
|
385
|
+
* @see https://github.com/edmundhung/conform/tree/v0.4.1/packages/conform-react/README.md#usefieldlist
|
|
449
386
|
*/
|
|
450
387
|
function useFieldList(ref, config) {
|
|
451
388
|
var configRef = react.useRef(config);
|
|
452
389
|
var [uncontrolledState, setUncontrolledState] = react.useState(() => {
|
|
453
390
|
var _config$defaultValue2;
|
|
454
|
-
|
|
455
391
|
var initialError = [];
|
|
456
|
-
|
|
457
392
|
for (var [name, message] of (_config$initialError2 = config === null || config === void 0 ? void 0 : config.initialError) !== null && _config$initialError2 !== void 0 ? _config$initialError2 : []) {
|
|
458
393
|
var _config$initialError2;
|
|
459
|
-
|
|
460
394
|
var [index, ...paths] = dom.getPaths(name);
|
|
461
|
-
|
|
462
395
|
if (typeof index === 'number') {
|
|
463
396
|
var _initialError$index;
|
|
464
|
-
|
|
465
397
|
var scopedName = dom.getName(paths);
|
|
466
|
-
|
|
467
398
|
var _entries = (_initialError$index = initialError[index]) !== null && _initialError$index !== void 0 ? _initialError$index : [];
|
|
468
|
-
|
|
469
399
|
if (scopedName === '' && _entries.length > 0 && _entries[0][0] !== '') {
|
|
470
400
|
initialError[index] = [[scopedName, message], ..._entries];
|
|
471
401
|
} else {
|
|
@@ -473,7 +403,6 @@ function useFieldList(ref, config) {
|
|
|
473
403
|
}
|
|
474
404
|
}
|
|
475
405
|
}
|
|
476
|
-
|
|
477
406
|
return {
|
|
478
407
|
defaultValue: (_config$defaultValue2 = config.defaultValue) !== null && _config$defaultValue2 !== void 0 ? _config$defaultValue2 : [],
|
|
479
408
|
initialError
|
|
@@ -481,7 +410,6 @@ function useFieldList(ref, config) {
|
|
|
481
410
|
});
|
|
482
411
|
var [entries, setEntries] = react.useState(() => {
|
|
483
412
|
var _config$defaultValue3;
|
|
484
|
-
|
|
485
413
|
return Object.entries((_config$defaultValue3 = config.defaultValue) !== null && _config$defaultValue3 !== void 0 ? _config$defaultValue3 : [undefined]);
|
|
486
414
|
});
|
|
487
415
|
var list = entries.map((_ref3, index) => {
|
|
@@ -496,11 +424,11 @@ function useFieldList(ref, config) {
|
|
|
496
424
|
}
|
|
497
425
|
};
|
|
498
426
|
});
|
|
427
|
+
|
|
499
428
|
/***
|
|
500
429
|
* This use proxy to capture all information about the command and
|
|
501
430
|
* have it encoded in the value.
|
|
502
431
|
*/
|
|
503
|
-
|
|
504
432
|
var command = new Proxy({}, {
|
|
505
433
|
get(_target, type) {
|
|
506
434
|
return function () {
|
|
@@ -517,7 +445,6 @@ function useFieldList(ref, config) {
|
|
|
517
445
|
};
|
|
518
446
|
};
|
|
519
447
|
}
|
|
520
|
-
|
|
521
448
|
});
|
|
522
449
|
react.useEffect(() => {
|
|
523
450
|
configRef.current = config;
|
|
@@ -525,18 +452,14 @@ function useFieldList(ref, config) {
|
|
|
525
452
|
react.useEffect(() => {
|
|
526
453
|
var submitHandler = event => {
|
|
527
454
|
var form = dom.getFormElement(ref.current);
|
|
528
|
-
|
|
529
455
|
if (!form || event.target !== form || !(event.submitter instanceof HTMLButtonElement) || event.submitter.name !== 'conform/list') {
|
|
530
456
|
return;
|
|
531
457
|
}
|
|
532
|
-
|
|
533
458
|
var command = dom.parseListCommand(event.submitter.value);
|
|
534
|
-
|
|
535
459
|
if (command.scope !== configRef.current.name) {
|
|
536
460
|
// Ensure the scope of the listener are limited to specific field name
|
|
537
461
|
return;
|
|
538
462
|
}
|
|
539
|
-
|
|
540
463
|
setEntries(entries => {
|
|
541
464
|
switch (command.type) {
|
|
542
465
|
case 'append':
|
|
@@ -547,7 +470,6 @@ function useFieldList(ref, config) {
|
|
|
547
470
|
defaultValue: ["".concat(Date.now()), command.payload.defaultValue]
|
|
548
471
|
})
|
|
549
472
|
}));
|
|
550
|
-
|
|
551
473
|
default:
|
|
552
474
|
{
|
|
553
475
|
return dom.updateList([...(entries !== null && entries !== void 0 ? entries : [])], command);
|
|
@@ -556,16 +478,12 @@ function useFieldList(ref, config) {
|
|
|
556
478
|
});
|
|
557
479
|
event.preventDefault();
|
|
558
480
|
};
|
|
559
|
-
|
|
560
481
|
var resetHandler = event => {
|
|
561
482
|
var _fieldConfig$defaultV, _fieldConfig$defaultV2;
|
|
562
|
-
|
|
563
483
|
var form = dom.getFormElement(ref.current);
|
|
564
|
-
|
|
565
484
|
if (!form || event.target !== form) {
|
|
566
485
|
return;
|
|
567
486
|
}
|
|
568
|
-
|
|
569
487
|
var fieldConfig = configRef.current;
|
|
570
488
|
setUncontrolledState({
|
|
571
489
|
defaultValue: (_fieldConfig$defaultV = fieldConfig.defaultValue) !== null && _fieldConfig$defaultV !== void 0 ? _fieldConfig$defaultV : [],
|
|
@@ -573,7 +491,6 @@ function useFieldList(ref, config) {
|
|
|
573
491
|
});
|
|
574
492
|
setEntries(Object.entries((_fieldConfig$defaultV2 = fieldConfig.defaultValue) !== null && _fieldConfig$defaultV2 !== void 0 ? _fieldConfig$defaultV2 : [undefined]));
|
|
575
493
|
};
|
|
576
|
-
|
|
577
494
|
document.addEventListener('submit', submitHandler, true);
|
|
578
495
|
document.addEventListener('reset', resetHandler);
|
|
579
496
|
return () => {
|
|
@@ -581,20 +498,19 @@ function useFieldList(ref, config) {
|
|
|
581
498
|
document.removeEventListener('reset', resetHandler);
|
|
582
499
|
};
|
|
583
500
|
}, [ref]);
|
|
584
|
-
return [list,
|
|
501
|
+
return [list,
|
|
502
|
+
// @ts-expect-error proxy type
|
|
585
503
|
command];
|
|
586
504
|
}
|
|
587
|
-
|
|
588
505
|
/**
|
|
589
506
|
* Returns the properties required to configure a shadow input for validation.
|
|
590
507
|
* This is particular useful when integrating dropdown and datepicker whichs
|
|
591
508
|
* introduces custom input mode.
|
|
592
509
|
*
|
|
593
|
-
* @see https://github.com/edmundhung/conform/tree/v0.4.
|
|
510
|
+
* @see https://github.com/edmundhung/conform/tree/v0.4.1/packages/conform-react/README.md#usecontrolledinput
|
|
594
511
|
*/
|
|
595
512
|
function useControlledInput(config) {
|
|
596
513
|
var _config$defaultValue4;
|
|
597
|
-
|
|
598
514
|
var ref = react.useRef(null);
|
|
599
515
|
var inputRef = react.useRef(null);
|
|
600
516
|
var configRef = react.useRef(config);
|
|
@@ -603,12 +519,10 @@ function useControlledInput(config) {
|
|
|
603
519
|
initialError: config.initialError
|
|
604
520
|
});
|
|
605
521
|
var [value, setValue] = react.useState("".concat((_config$defaultValue4 = config.defaultValue) !== null && _config$defaultValue4 !== void 0 ? _config$defaultValue4 : ''));
|
|
606
|
-
|
|
607
522
|
var handleChange = eventOrValue => {
|
|
608
523
|
if (!ref.current) {
|
|
609
524
|
return;
|
|
610
525
|
}
|
|
611
|
-
|
|
612
526
|
var newValue = typeof eventOrValue === 'string' ? eventOrValue : eventOrValue.target.value;
|
|
613
527
|
ref.current.value = newValue;
|
|
614
528
|
ref.current.dispatchEvent(new InputEvent('input', {
|
|
@@ -616,39 +530,31 @@ function useControlledInput(config) {
|
|
|
616
530
|
}));
|
|
617
531
|
setValue(newValue);
|
|
618
532
|
};
|
|
619
|
-
|
|
620
533
|
var handleBlur = () => {
|
|
621
534
|
var _ref$current;
|
|
622
|
-
|
|
623
535
|
(_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.dispatchEvent(new FocusEvent('blur', {
|
|
624
536
|
bubbles: true
|
|
625
537
|
}));
|
|
626
538
|
};
|
|
627
|
-
|
|
628
539
|
var handleInvalid = event => {
|
|
629
540
|
event.preventDefault();
|
|
630
541
|
};
|
|
631
|
-
|
|
632
542
|
react.useEffect(() => {
|
|
633
543
|
configRef.current = config;
|
|
634
544
|
});
|
|
635
545
|
react.useEffect(() => {
|
|
636
546
|
var resetHandler = event => {
|
|
637
547
|
var _configRef$current$de;
|
|
638
|
-
|
|
639
548
|
var form = dom.getFormElement(ref.current);
|
|
640
|
-
|
|
641
549
|
if (!form || event.target !== form) {
|
|
642
550
|
return;
|
|
643
551
|
}
|
|
644
|
-
|
|
645
552
|
setUncontrolledState({
|
|
646
553
|
defaultValue: configRef.current.defaultValue,
|
|
647
554
|
initialError: configRef.current.initialError
|
|
648
555
|
});
|
|
649
556
|
setValue("".concat((_configRef$current$de = configRef.current.defaultValue) !== null && _configRef$current$de !== void 0 ? _configRef$current$de : ''));
|
|
650
557
|
};
|
|
651
|
-
|
|
652
558
|
document.addEventListener('reset', resetHandler);
|
|
653
559
|
return () => {
|
|
654
560
|
document.removeEventListener('reset', resetHandler);
|
|
@@ -667,16 +573,13 @@ function useControlledInput(config) {
|
|
|
667
573
|
whiteSpace: 'nowrap',
|
|
668
574
|
borderWidth: 0
|
|
669
575
|
},
|
|
670
|
-
|
|
576
|
+
tabIndex: -1,
|
|
577
|
+
'aria-hidden': true,
|
|
671
578
|
onFocus() {
|
|
672
579
|
var _inputRef$current;
|
|
673
|
-
|
|
674
580
|
(_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.focus();
|
|
675
581
|
}
|
|
676
|
-
|
|
677
|
-
}, helpers.input(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, config), uncontrolledState), {
|
|
678
|
-
type: 'text'
|
|
679
|
-
})), {
|
|
582
|
+
}, helpers.input(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, config), uncontrolledState))), {
|
|
680
583
|
ref: inputRef,
|
|
681
584
|
value,
|
|
682
585
|
onChange: handleChange,
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { type FieldsetConstraint, type Submission, getFormElements, hasError, parse, shouldValidate, } from '@conform-to/dom';
|
|
1
|
+
export { type FieldConfig, type FieldsetConstraint, type Submission, getFormElements, hasError, parse, shouldValidate, } from '@conform-to/dom';
|
|
2
2
|
export * from './hooks';
|
|
3
3
|
export * as conform from './helpers';
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
function ownKeys(object, enumerableOnly) {
|
|
2
2
|
var keys = Object.keys(object);
|
|
3
|
-
|
|
4
3
|
if (Object.getOwnPropertySymbols) {
|
|
5
4
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
6
5
|
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
7
6
|
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
8
7
|
})), keys.push.apply(keys, symbols);
|
|
9
8
|
}
|
|
10
|
-
|
|
11
9
|
return keys;
|
|
12
10
|
}
|
|
13
|
-
|
|
14
11
|
function _objectSpread2(target) {
|
|
15
12
|
for (var i = 1; i < arguments.length; i++) {
|
|
16
13
|
var source = null != arguments[i] ? arguments[i] : {};
|
|
@@ -20,10 +17,8 @@ function _objectSpread2(target) {
|
|
|
20
17
|
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
21
18
|
});
|
|
22
19
|
}
|
|
23
|
-
|
|
24
20
|
return target;
|
|
25
21
|
}
|
|
26
|
-
|
|
27
22
|
function _defineProperty(obj, key, value) {
|
|
28
23
|
if (key in obj) {
|
|
29
24
|
Object.defineProperty(obj, key, {
|
|
@@ -35,7 +30,6 @@ function _defineProperty(obj, key, value) {
|
|
|
35
30
|
} else {
|
|
36
31
|
obj[key] = value;
|
|
37
32
|
}
|
|
38
|
-
|
|
39
33
|
return obj;
|
|
40
34
|
}
|
|
41
35
|
|