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