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