@conform-to/react 0.7.0-pre.2 → 0.7.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 +35 -47
- package/helpers.d.ts +11 -2
- package/helpers.js +38 -31
- package/helpers.mjs +31 -31
- package/hooks.d.ts +11 -18
- package/hooks.js +151 -177
- package/hooks.mjs +151 -175
- package/package.json +2 -2
package/hooks.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
-
import { parseIntent, getFormData, parse, getFormAction, getFormEncType, getFormMethod, getPaths, getName, isFieldElement, getErrors, getFormControls, getFormElement, updateList, getValidationMessage, focusFirstInvalidControl, isFocusableFormControl, requestIntent, validate } from '@conform-to/dom';
|
|
3
|
-
import { useState, useMemo, useEffect,
|
|
2
|
+
import { parseIntent, getFormData, parse, VALIDATION_UNDEFINED, VALIDATION_SKIPPED, getFormAction, getFormEncType, getFormMethod, getPaths, getName, isFieldElement, getErrors, getFormControls, getFormElement, updateList, getValidationMessage, focusFirstInvalidControl, isFocusableFormControl, requestIntent, validate } from '@conform-to/dom';
|
|
3
|
+
import { useState, useMemo, useRef, useEffect, useLayoutEffect, useCallback } from 'react';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Normalize error to an array of string.
|
|
@@ -125,7 +125,7 @@ function useFormError(ref, config) {
|
|
|
125
125
|
* @see https://conform.guide/api/react#useform
|
|
126
126
|
*/
|
|
127
127
|
function useForm() {
|
|
128
|
-
var
|
|
128
|
+
var _config$lastSubmissio2;
|
|
129
129
|
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
130
130
|
var configRef = useConfigRef(config);
|
|
131
131
|
var ref = useFormRef(config.ref);
|
|
@@ -146,26 +146,36 @@ function useForm() {
|
|
|
146
146
|
[scope]: submission.error[scope]
|
|
147
147
|
};
|
|
148
148
|
}, [config.lastSubmission]);
|
|
149
|
+
// This payload from lastSubmission is only useful before hydration
|
|
150
|
+
// After hydration, any new payload on lastSubmission will be ignored
|
|
151
|
+
var [lastSubmissionPayload, setLastSubmissionPayload] = useState((_config$lastSubmissio2 = config.lastSubmission) === null || _config$lastSubmissio2 === void 0 ? void 0 : _config$lastSubmissio2.payload);
|
|
149
152
|
var fieldset = useFieldset(ref, {
|
|
150
|
-
defaultValue:
|
|
153
|
+
defaultValue: lastSubmissionPayload !== null && lastSubmissionPayload !== void 0 ? lastSubmissionPayload : config.defaultValue,
|
|
151
154
|
initialError,
|
|
152
155
|
constraint: config.constraint,
|
|
153
156
|
form: config.id
|
|
154
157
|
});
|
|
158
|
+
var lastSubmissionRef = useRef(config.lastSubmission);
|
|
159
|
+
useSafeLayoutEffect(() => {
|
|
160
|
+
if (lastSubmissionRef.current !== config.lastSubmission && config.lastSubmission === null) {
|
|
161
|
+
var _ref$current;
|
|
162
|
+
(_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.reset();
|
|
163
|
+
}
|
|
164
|
+
lastSubmissionRef.current = config.lastSubmission;
|
|
165
|
+
}, [ref, config.lastSubmission]);
|
|
155
166
|
useEffect(() => {
|
|
156
167
|
// custom validate handler
|
|
157
|
-
var createValidateHandler =
|
|
168
|
+
var createValidateHandler = type => event => {
|
|
158
169
|
var field = event.target;
|
|
159
170
|
var form = ref.current;
|
|
160
171
|
var {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
shouldRevalidate = 'onInput'
|
|
172
|
+
shouldValidate = 'onSubmit',
|
|
173
|
+
shouldRevalidate = shouldValidate
|
|
164
174
|
} = configRef.current;
|
|
165
175
|
if (!form || !isFocusableFormControl(field) || field.form !== form || !field.name) {
|
|
166
176
|
return;
|
|
167
177
|
}
|
|
168
|
-
if (field.dataset.conformTouched ? shouldRevalidate ===
|
|
178
|
+
if (field.dataset.conformTouched ? shouldRevalidate === type : shouldValidate === type) {
|
|
169
179
|
requestIntent(form, validate(field.name));
|
|
170
180
|
}
|
|
171
181
|
};
|
|
@@ -187,11 +197,12 @@ function useForm() {
|
|
|
187
197
|
}
|
|
188
198
|
|
|
189
199
|
// Reset all field state
|
|
190
|
-
for (var
|
|
191
|
-
delete
|
|
192
|
-
|
|
200
|
+
for (var _element of getFormControls(form)) {
|
|
201
|
+
delete _element.dataset.conformTouched;
|
|
202
|
+
_element.setCustomValidity('');
|
|
193
203
|
}
|
|
194
204
|
setErrors([]);
|
|
205
|
+
setLastSubmissionPayload(undefined);
|
|
195
206
|
};
|
|
196
207
|
var handleInput = createValidateHandler('onInput');
|
|
197
208
|
var handleBlur = createValidateHandler('onBlur');
|
|
@@ -230,8 +241,8 @@ function useForm() {
|
|
|
230
241
|
var {
|
|
231
242
|
errors: _errors,
|
|
232
243
|
shouldServerValidate
|
|
233
|
-
} = Object.entries(submission.error).reduce((result,
|
|
234
|
-
var [, error] =
|
|
244
|
+
} = Object.entries(submission.error).reduce((result, _ref) => {
|
|
245
|
+
var [, error] = _ref;
|
|
235
246
|
for (var message of normalizeError(error)) {
|
|
236
247
|
if (message === VALIDATION_UNDEFINED) {
|
|
237
248
|
result.shouldServerValidate = true;
|
|
@@ -305,8 +316,8 @@ function useFieldset(ref, config) {
|
|
|
305
316
|
var fieldsetConfig = config;
|
|
306
317
|
var constraint = (_fieldsetConfig$const = fieldsetConfig.constraint) === null || _fieldsetConfig$const === void 0 ? void 0 : _fieldsetConfig$const[key];
|
|
307
318
|
var errors = error === null || error === void 0 ? void 0 : error[key];
|
|
308
|
-
var initialError = Object.entries((_fieldsetConfig$initi = fieldsetConfig.initialError) !== null && _fieldsetConfig$initi !== void 0 ? _fieldsetConfig$initi : {}).reduce((result,
|
|
309
|
-
var [name, message] =
|
|
319
|
+
var initialError = Object.entries((_fieldsetConfig$initi = fieldsetConfig.initialError) !== null && _fieldsetConfig$initi !== void 0 ? _fieldsetConfig$initi : {}).reduce((result, _ref2) => {
|
|
320
|
+
var [name, message] = _ref2;
|
|
310
321
|
var [field, ...paths] = getPaths(name);
|
|
311
322
|
if (field === key) {
|
|
312
323
|
result[getName(paths)] = message;
|
|
@@ -412,12 +423,12 @@ function useFieldList(ref, config) {
|
|
|
412
423
|
document.removeEventListener('reset', resetHandler);
|
|
413
424
|
};
|
|
414
425
|
}, [ref, configRef, setError]);
|
|
415
|
-
return entries.map((
|
|
426
|
+
return entries.map((_ref3, index) => {
|
|
416
427
|
var _config$initialError, _config$defaultValue2;
|
|
417
|
-
var [key, defaultValue] =
|
|
428
|
+
var [key, defaultValue] = _ref3;
|
|
418
429
|
var errors = error[index];
|
|
419
|
-
var initialError = Object.entries((_config$initialError = config.initialError) !== null && _config$initialError !== void 0 ? _config$initialError : {}).reduce((result,
|
|
420
|
-
var [name, message] =
|
|
430
|
+
var initialError = Object.entries((_config$initialError = config.initialError) !== null && _config$initialError !== void 0 ? _config$initialError : {}).reduce((result, _ref4) => {
|
|
431
|
+
var [name, message] = _ref4;
|
|
421
432
|
var [field, ...paths] = getPaths(name);
|
|
422
433
|
if (field === index) {
|
|
423
434
|
result[getName(paths)] = message;
|
|
@@ -443,179 +454,144 @@ function useFieldList(ref, config) {
|
|
|
443
454
|
});
|
|
444
455
|
}
|
|
445
456
|
|
|
446
|
-
/**
|
|
447
|
-
* Triggering react custom change event
|
|
448
|
-
* Solution based on dom-testing-library
|
|
449
|
-
* @see https://github.com/facebook/react/issues/10135#issuecomment-401496776
|
|
450
|
-
* @see https://github.com/testing-library/dom-testing-library/blob/main/src/events.js#L104-L123
|
|
451
|
-
*/
|
|
452
|
-
function setNativeValue(element, value) {
|
|
453
|
-
if (element.value === value) {
|
|
454
|
-
// It will not trigger a change event if `element.value` is the same as the set value
|
|
455
|
-
return;
|
|
456
|
-
}
|
|
457
|
-
var {
|
|
458
|
-
set: valueSetter
|
|
459
|
-
} = Object.getOwnPropertyDescriptor(element, 'value') || {};
|
|
460
|
-
var prototype = Object.getPrototypeOf(element);
|
|
461
|
-
var {
|
|
462
|
-
set: prototypeValueSetter
|
|
463
|
-
} = Object.getOwnPropertyDescriptor(prototype, 'value') || {};
|
|
464
|
-
if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
|
|
465
|
-
prototypeValueSetter.call(element, value);
|
|
466
|
-
} else {
|
|
467
|
-
if (valueSetter) {
|
|
468
|
-
valueSetter.call(element, value);
|
|
469
|
-
} else {
|
|
470
|
-
throw new Error('The given element does not have a value setter');
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
|
|
475
457
|
/**
|
|
476
458
|
* useLayoutEffect is client-only.
|
|
477
459
|
* This basically makes it a no-op on server
|
|
478
460
|
*/
|
|
479
461
|
var useSafeLayoutEffect = typeof document === 'undefined' ? useEffect : useLayoutEffect;
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Returns a ref object and a set of helpers that dispatch corresponding dom event.
|
|
465
|
+
*
|
|
466
|
+
* @see https://conform.guide/api/react#useinputevent
|
|
467
|
+
*/
|
|
480
468
|
function useInputEvent(options) {
|
|
481
|
-
var ref = useRef(null);
|
|
482
469
|
var optionsRef = useConfigRef(options);
|
|
483
|
-
var
|
|
484
|
-
|
|
485
|
-
|
|
470
|
+
var eventDispatched = useRef({
|
|
471
|
+
onInput: false,
|
|
472
|
+
onFocus: false,
|
|
473
|
+
onBlur: false
|
|
474
|
+
});
|
|
486
475
|
useSafeLayoutEffect(() => {
|
|
487
|
-
var
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
focusDispatched.current = true;
|
|
501
|
-
}
|
|
502
|
-
};
|
|
503
|
-
var blurHandler = event => {
|
|
504
|
-
var input = getInputElement();
|
|
505
|
-
if (input && event.target === input) {
|
|
506
|
-
blurDispatched.current = true;
|
|
507
|
-
}
|
|
508
|
-
};
|
|
509
|
-
var submitHandler = event => {
|
|
510
|
-
var input = getInputElement();
|
|
511
|
-
if (input !== null && input !== void 0 && input.form && event.target === input.form) {
|
|
512
|
-
var _optionsRef$current2, _optionsRef$current2$;
|
|
513
|
-
(_optionsRef$current2 = optionsRef.current) === null || _optionsRef$current2 === void 0 ? void 0 : (_optionsRef$current2$ = _optionsRef$current2.onSubmit) === null || _optionsRef$current2$ === void 0 ? void 0 : _optionsRef$current2$.call(_optionsRef$current2, event);
|
|
514
|
-
}
|
|
515
|
-
};
|
|
516
|
-
var resetHandler = event => {
|
|
517
|
-
var input = getInputElement();
|
|
518
|
-
if (input !== null && input !== void 0 && input.form && event.target === input.form) {
|
|
519
|
-
var _optionsRef$current3, _optionsRef$current3$;
|
|
520
|
-
(_optionsRef$current3 = optionsRef.current) === null || _optionsRef$current3 === void 0 ? void 0 : (_optionsRef$current3$ = _optionsRef$current3.onReset) === null || _optionsRef$current3$ === void 0 ? void 0 : _optionsRef$current3$.call(_optionsRef$current3, event);
|
|
521
|
-
}
|
|
476
|
+
var createEventListner = listener => {
|
|
477
|
+
return event => {
|
|
478
|
+
var _optionsRef$current, _optionsRef$current2, _optionsRef$current3;
|
|
479
|
+
var element = typeof ((_optionsRef$current = optionsRef.current) === null || _optionsRef$current === void 0 ? void 0 : _optionsRef$current.ref) === 'function' ? (_optionsRef$current2 = optionsRef.current) === null || _optionsRef$current2 === void 0 ? void 0 : _optionsRef$current2.ref() : (_optionsRef$current3 = optionsRef.current) === null || _optionsRef$current3 === void 0 ? void 0 : _optionsRef$current3.ref.current;
|
|
480
|
+
if (isFieldElement(element) && (listener === 'onReset' ? event.target === element.form : event.target === element)) {
|
|
481
|
+
var _optionsRef$current4, _optionsRef$current4$;
|
|
482
|
+
if (listener !== 'onReset') {
|
|
483
|
+
console.log(listener, 'dispatched');
|
|
484
|
+
eventDispatched.current[listener] = true;
|
|
485
|
+
}
|
|
486
|
+
(_optionsRef$current4 = optionsRef.current) === null || _optionsRef$current4 === void 0 ? void 0 : (_optionsRef$current4$ = _optionsRef$current4[listener]) === null || _optionsRef$current4$ === void 0 ? void 0 : _optionsRef$current4$.call(_optionsRef$current4, event);
|
|
487
|
+
}
|
|
488
|
+
};
|
|
522
489
|
};
|
|
490
|
+
var inputHandler = createEventListner('onInput');
|
|
491
|
+
var focusHandler = createEventListner('onFocus');
|
|
492
|
+
var blurHandler = createEventListner('onBlur');
|
|
493
|
+
var resetHandler = createEventListner('onReset');
|
|
494
|
+
|
|
495
|
+
// focus/blur event does not bubble
|
|
523
496
|
document.addEventListener('input', inputHandler, true);
|
|
524
497
|
document.addEventListener('focus', focusHandler, true);
|
|
525
498
|
document.addEventListener('blur', blurHandler, true);
|
|
526
|
-
document.addEventListener('submit', submitHandler);
|
|
527
499
|
document.addEventListener('reset', resetHandler);
|
|
528
500
|
return () => {
|
|
529
501
|
document.removeEventListener('input', inputHandler, true);
|
|
530
502
|
document.removeEventListener('focus', focusHandler, true);
|
|
531
503
|
document.removeEventListener('blur', blurHandler, true);
|
|
532
|
-
document.removeEventListener('submit', submitHandler);
|
|
533
504
|
document.removeEventListener('reset', resetHandler);
|
|
534
505
|
};
|
|
535
506
|
}, []);
|
|
536
507
|
var control = useMemo(() => {
|
|
537
|
-
var
|
|
538
|
-
|
|
539
|
-
|
|
508
|
+
var dispatch = (listener, fn) => {
|
|
509
|
+
if (!eventDispatched.current[listener]) {
|
|
510
|
+
var _optionsRef$current5, _optionsRef$current6, _optionsRef$current7;
|
|
511
|
+
var _element2 = typeof ((_optionsRef$current5 = optionsRef.current) === null || _optionsRef$current5 === void 0 ? void 0 : _optionsRef$current5.ref) === 'function' ? (_optionsRef$current6 = optionsRef.current) === null || _optionsRef$current6 === void 0 ? void 0 : _optionsRef$current6.ref() : (_optionsRef$current7 = optionsRef.current) === null || _optionsRef$current7 === void 0 ? void 0 : _optionsRef$current7.ref.current;
|
|
512
|
+
if (!isFieldElement(_element2)) {
|
|
513
|
+
console.warn('Failed to dispatch event; is the input mounted?');
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// To avoid recursion
|
|
518
|
+
eventDispatched.current[listener] = true;
|
|
519
|
+
fn(_element2);
|
|
520
|
+
}
|
|
521
|
+
eventDispatched.current[listener] = false;
|
|
540
522
|
};
|
|
541
523
|
return {
|
|
542
524
|
change(eventOrValue) {
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
525
|
+
dispatch('onInput', element => {
|
|
526
|
+
if (element instanceof HTMLInputElement && (element.type === 'checkbox' || element.type === 'radio')) {
|
|
527
|
+
if (typeof eventOrValue !== 'boolean') {
|
|
528
|
+
throw new Error('You should pass a boolean when changing a checkbox or radio input');
|
|
529
|
+
}
|
|
530
|
+
element.checked = eventOrValue;
|
|
531
|
+
} else {
|
|
532
|
+
if (typeof eventOrValue === 'boolean') {
|
|
533
|
+
throw new Error('You can pass a boolean only when changing a checkbox or radio input');
|
|
534
|
+
}
|
|
535
|
+
var _value = typeof eventOrValue === 'string' ? eventOrValue : eventOrValue.target.value;
|
|
554
536
|
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
537
|
+
// No change event will triggered on React if `element.value` is updated
|
|
538
|
+
// before dispatching the event
|
|
539
|
+
if (element.value !== _value) {
|
|
540
|
+
/**
|
|
541
|
+
* Triggering react custom change event
|
|
542
|
+
* Solution based on dom-testing-library
|
|
543
|
+
* @see https://github.com/facebook/react/issues/10135#issuecomment-401496776
|
|
544
|
+
* @see https://github.com/testing-library/dom-testing-library/blob/main/src/events.js#L104-L123
|
|
545
|
+
*/
|
|
546
|
+
var {
|
|
547
|
+
set: valueSetter
|
|
548
|
+
} = Object.getOwnPropertyDescriptor(element, 'value') || {};
|
|
549
|
+
var prototype = Object.getPrototypeOf(element);
|
|
550
|
+
var {
|
|
551
|
+
set: prototypeValueSetter
|
|
552
|
+
} = Object.getOwnPropertyDescriptor(prototype, 'value') || {};
|
|
553
|
+
if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
|
|
554
|
+
prototypeValueSetter.call(element, _value);
|
|
555
|
+
} else {
|
|
556
|
+
if (valueSetter) {
|
|
557
|
+
valueSetter.call(element, _value);
|
|
558
|
+
} else {
|
|
559
|
+
throw new Error('The given element does not have a value setter');
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
559
564
|
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
}));
|
|
570
|
-
// Reset the dispatched flag
|
|
571
|
-
changeDispatched.current = false;
|
|
565
|
+
// Dispatch input event with the updated input value
|
|
566
|
+
element.dispatchEvent(new InputEvent('input', {
|
|
567
|
+
bubbles: true
|
|
568
|
+
}));
|
|
569
|
+
// Dispatch change event (necessary for select to update the selected option)
|
|
570
|
+
element.dispatchEvent(new Event('change', {
|
|
571
|
+
bubbles: true
|
|
572
|
+
}));
|
|
573
|
+
});
|
|
572
574
|
},
|
|
573
575
|
focus() {
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
if (focusDispatched.current) {
|
|
580
|
-
focusDispatched.current = false;
|
|
581
|
-
return;
|
|
582
|
-
}
|
|
583
|
-
var focusinEvent = new FocusEvent('focusin', {
|
|
584
|
-
bubbles: true
|
|
576
|
+
dispatch('onFocus', element => {
|
|
577
|
+
element.dispatchEvent(new FocusEvent('focusin', {
|
|
578
|
+
bubbles: true
|
|
579
|
+
}));
|
|
580
|
+
element.dispatchEvent(new FocusEvent('focus'));
|
|
585
581
|
});
|
|
586
|
-
var focusEvent = new FocusEvent('focus');
|
|
587
|
-
input.dispatchEvent(focusinEvent);
|
|
588
|
-
input.dispatchEvent(focusEvent);
|
|
589
|
-
|
|
590
|
-
// Reset the dispatched flag
|
|
591
|
-
focusDispatched.current = false;
|
|
592
582
|
},
|
|
593
583
|
blur() {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
if (blurDispatched.current) {
|
|
600
|
-
blurDispatched.current = false;
|
|
601
|
-
return;
|
|
602
|
-
}
|
|
603
|
-
var focusoutEvent = new FocusEvent('focusout', {
|
|
604
|
-
bubbles: true
|
|
584
|
+
dispatch('onBlur', element => {
|
|
585
|
+
element.dispatchEvent(new FocusEvent('focusout', {
|
|
586
|
+
bubbles: true
|
|
587
|
+
}));
|
|
588
|
+
element.dispatchEvent(new FocusEvent('blur'));
|
|
605
589
|
});
|
|
606
|
-
var blurEvent = new FocusEvent('blur');
|
|
607
|
-
input.dispatchEvent(focusoutEvent);
|
|
608
|
-
input.dispatchEvent(blurEvent);
|
|
609
|
-
|
|
610
|
-
// Reset the dispatched flag
|
|
611
|
-
blurDispatched.current = false;
|
|
612
590
|
}
|
|
613
591
|
};
|
|
614
592
|
}, [optionsRef]);
|
|
615
|
-
return
|
|
593
|
+
return control;
|
|
616
594
|
}
|
|
617
|
-
var VALIDATION_UNDEFINED = '__undefined__';
|
|
618
|
-
var VALIDATION_SKIPPED = '__skipped__';
|
|
619
595
|
var FORM_ERROR_ELEMENT_NAME = '__form__';
|
|
620
596
|
|
|
621
597
|
/**
|
|
@@ -642,28 +618,28 @@ function validateConstraint(options) {
|
|
|
642
618
|
}
|
|
643
619
|
return errors;
|
|
644
620
|
};
|
|
645
|
-
var formatMessages = (_options$formatMessag = options === null || options === void 0 ? void 0 : options.formatMessages) !== null && _options$formatMessag !== void 0 ? _options$formatMessag :
|
|
621
|
+
var formatMessages = (_options$formatMessag = options === null || options === void 0 ? void 0 : options.formatMessages) !== null && _options$formatMessag !== void 0 ? _options$formatMessag : _ref5 => {
|
|
646
622
|
var {
|
|
647
623
|
defaultErrors
|
|
648
|
-
} =
|
|
624
|
+
} = _ref5;
|
|
649
625
|
return defaultErrors;
|
|
650
626
|
};
|
|
651
627
|
return parse(formData, {
|
|
652
628
|
resolve(payload, intent) {
|
|
653
629
|
var error = {};
|
|
654
630
|
var constraintPattern = /^constraint[A-Z][^A-Z]*$/;
|
|
655
|
-
var _loop = function _loop(
|
|
656
|
-
if (isFieldElement(
|
|
631
|
+
var _loop = function _loop(_element3) {
|
|
632
|
+
if (isFieldElement(_element3)) {
|
|
657
633
|
var _options$acceptMultip, _options$acceptMultip2;
|
|
658
|
-
var name =
|
|
659
|
-
var constraint = Object.entries(
|
|
660
|
-
var [name, attributeValue = ''] =
|
|
634
|
+
var name = _element3.name !== FORM_ERROR_ELEMENT_NAME ? _element3.name : '';
|
|
635
|
+
var constraint = Object.entries(_element3.dataset).reduce((result, _ref6) => {
|
|
636
|
+
var [name, attributeValue = ''] = _ref6;
|
|
661
637
|
if (constraintPattern.test(name)) {
|
|
662
638
|
var _options$constraint;
|
|
663
639
|
var constraintName = name.slice(10).toLowerCase();
|
|
664
640
|
var _validate = (_options$constraint = options.constraint) === null || _options$constraint === void 0 ? void 0 : _options$constraint[constraintName];
|
|
665
641
|
if (typeof _validate === 'function') {
|
|
666
|
-
result[constraintName] = _validate(
|
|
642
|
+
result[constraintName] = _validate(_element3.value, {
|
|
667
643
|
formData,
|
|
668
644
|
attributeValue
|
|
669
645
|
});
|
|
@@ -675,9 +651,9 @@ function validateConstraint(options) {
|
|
|
675
651
|
}, {});
|
|
676
652
|
var errors = formatMessages({
|
|
677
653
|
name,
|
|
678
|
-
validity:
|
|
654
|
+
validity: _element3.validity,
|
|
679
655
|
constraint,
|
|
680
|
-
defaultErrors: getDefaultErrors(
|
|
656
|
+
defaultErrors: getDefaultErrors(_element3.validity, constraint)
|
|
681
657
|
});
|
|
682
658
|
var shouldAcceptMultipleErrors = (_options$acceptMultip = options === null || options === void 0 ? void 0 : (_options$acceptMultip2 = options.acceptMultipleErrors) === null || _options$acceptMultip2 === void 0 ? void 0 : _options$acceptMultip2.call(options, {
|
|
683
659
|
name,
|
|
@@ -689,8 +665,8 @@ function validateConstraint(options) {
|
|
|
689
665
|
}
|
|
690
666
|
}
|
|
691
667
|
};
|
|
692
|
-
for (var
|
|
693
|
-
_loop(
|
|
668
|
+
for (var _element3 of options.form.elements) {
|
|
669
|
+
_loop(_element3);
|
|
694
670
|
}
|
|
695
671
|
return {
|
|
696
672
|
error
|
|
@@ -728,18 +704,18 @@ function reportSubmission(form, submission) {
|
|
|
728
704
|
}
|
|
729
705
|
var intent = parseIntent(submission.intent);
|
|
730
706
|
var scope = getScope(intent);
|
|
731
|
-
for (var
|
|
732
|
-
var _elementName =
|
|
707
|
+
for (var _element4 of getFormControls(form)) {
|
|
708
|
+
var _elementName = _element4.name !== FORM_ERROR_ELEMENT_NAME ? _element4.name : '';
|
|
733
709
|
var messages = normalizeError(submission.error[_elementName]);
|
|
734
710
|
if (scope === null || scope === _elementName) {
|
|
735
|
-
|
|
711
|
+
_element4.dataset.conformTouched = 'true';
|
|
736
712
|
}
|
|
737
713
|
if (!messages.includes(VALIDATION_SKIPPED) && !messages.includes(VALIDATION_UNDEFINED)) {
|
|
738
714
|
var invalidEvent = new Event('invalid', {
|
|
739
715
|
cancelable: true
|
|
740
716
|
});
|
|
741
|
-
|
|
742
|
-
|
|
717
|
+
_element4.setCustomValidity(getValidationMessage(messages));
|
|
718
|
+
_element4.dispatchEvent(invalidEvent);
|
|
743
719
|
}
|
|
744
720
|
}
|
|
745
721
|
if (!intent) {
|
|
@@ -756,4 +732,4 @@ function getScope(intent) {
|
|
|
756
732
|
return null;
|
|
757
733
|
}
|
|
758
734
|
|
|
759
|
-
export { FORM_ERROR_ELEMENT_NAME,
|
|
735
|
+
export { FORM_ERROR_ELEMENT_NAME, getScope, reportSubmission, useFieldList, useFieldset, useForm, useInputEvent, validateConstraint };
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Conform view adapter for react",
|
|
4
4
|
"homepage": "https://conform.guide",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "0.7.0
|
|
6
|
+
"version": "0.7.0",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"module": "index.mjs",
|
|
9
9
|
"types": "index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"url": "https://github.com/edmundhung/conform/issues"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@conform-to/dom": "0.7.0
|
|
33
|
+
"@conform-to/dom": "0.7.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"react": ">=16.8"
|