@react-aria/form 3.1.5 → 3.2.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/dist/import.mjs +2 -2
- package/dist/main.js +2 -2
- package/dist/main.js.map +1 -1
- package/dist/module.js +2 -2
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/package.json +15 -15
- package/src/index.ts +1 -1
- package/dist/types.d.ts +0 -9
- package/dist/types.d.ts.map +0 -1
- package/dist/useFormValidation.main.js +0 -128
- package/dist/useFormValidation.main.js.map +0 -1
- package/dist/useFormValidation.mjs +0 -123
- package/dist/useFormValidation.module.js +0 -123
- package/dist/useFormValidation.module.js.map +0 -1
- package/src/useFormValidation.ts +0 -154
package/dist/import.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useFormValidation as $
|
|
1
|
+
import {useFormValidation as $c607b33eb33e0685$re_export$useFormValidation} from "react-aria/private/form/useFormValidation";
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
* Copyright 2023 Adobe. All rights reserved.
|
|
@@ -13,5 +13,5 @@ import {useFormValidation as $e93e671b31057976$export$b8473d3665f3a75a} from "./
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
export {$
|
|
16
|
+
export {$c607b33eb33e0685$re_export$useFormValidation as useFormValidation};
|
|
17
17
|
//# sourceMappingURL=module.js.map
|
package/dist/main.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
var $
|
|
1
|
+
var $39uVJ$reactariaprivateformuseFormValidation = require("react-aria/private/form/useFormValidation");
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
function $parcel$export(e, n, v, s) {
|
|
5
5
|
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
$parcel$export(module.exports, "useFormValidation", ()
|
|
8
|
+
$parcel$export(module.exports, "useFormValidation", function () { return $39uVJ$reactariaprivateformuseFormValidation.useFormValidation; });
|
|
9
9
|
/*
|
|
10
10
|
* Copyright 2023 Adobe. All rights reserved.
|
|
11
11
|
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-aria/form/src/index.ts"],"sourcesContent":["/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useFormValidation} from '
|
|
1
|
+
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-aria/form/src/index.ts"],"sourcesContent":["/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useFormValidation} from 'react-aria/private/form/useFormValidation';\n"],"names":[],"version":3,"file":"main.js.map"}
|
package/dist/module.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useFormValidation as $
|
|
1
|
+
import {useFormValidation as $c607b33eb33e0685$re_export$useFormValidation} from "react-aria/private/form/useFormValidation";
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
* Copyright 2023 Adobe. All rights reserved.
|
|
@@ -13,5 +13,5 @@ import {useFormValidation as $e93e671b31057976$export$b8473d3665f3a75a} from "./
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
export {$
|
|
16
|
+
export {$c607b33eb33e0685$re_export$useFormValidation as useFormValidation};
|
|
17
17
|
//# sourceMappingURL=module.js.map
|
package/dist/module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-aria/form/src/index.ts"],"sourcesContent":["/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useFormValidation} from '
|
|
1
|
+
{"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-aria/form/src/index.ts"],"sourcesContent":["/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useFormValidation} from 'react-aria/private/form/useFormValidation';\n"],"names":[],"version":3,"file":"module.js.map"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useFormValidation } from 'react-aria/private/form/useFormValidation';
|
package/package.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/form",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
7
7
|
"module": "dist/module.js",
|
|
8
|
-
"types": "dist/types.d.ts",
|
|
8
|
+
"types": "dist/types/src/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
"./dist/types.d.ts",
|
|
13
|
-
"./
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
10
|
+
".": {
|
|
11
|
+
"source": "./src/index.ts",
|
|
12
|
+
"types": "./dist/types/src/index.d.ts",
|
|
13
|
+
"import": "./dist/import.mjs",
|
|
14
|
+
"require": "./dist/main.js"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
17
|
},
|
|
18
18
|
"source": "src/index.ts",
|
|
19
19
|
"files": [
|
|
@@ -26,11 +26,8 @@
|
|
|
26
26
|
"url": "https://github.com/adobe/react-spectrum"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@
|
|
30
|
-
"
|
|
31
|
-
"@react-stately/form": "^3.2.4",
|
|
32
|
-
"@react-types/shared": "^3.33.1",
|
|
33
|
-
"@swc/helpers": "^0.5.0"
|
|
29
|
+
"@swc/helpers": "^0.5.0",
|
|
30
|
+
"react-aria": "3.48.0"
|
|
34
31
|
},
|
|
35
32
|
"peerDependencies": {
|
|
36
33
|
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1",
|
|
@@ -39,5 +36,8 @@
|
|
|
39
36
|
"publishConfig": {
|
|
40
37
|
"access": "public"
|
|
41
38
|
},
|
|
42
|
-
"
|
|
39
|
+
"targets": {
|
|
40
|
+
"types": false
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "a6999bdf494a2e9c0381a5881908328bdd22ddae"
|
|
43
43
|
}
|
package/src/index.ts
CHANGED
package/dist/types.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { FormValidationState } from "@react-stately/form";
|
|
2
|
-
import { RefObject, Validation } from "@react-types/shared";
|
|
3
|
-
type ValidatableElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
|
4
|
-
interface FormValidationProps<T> extends Validation<T> {
|
|
5
|
-
focus?: () => void;
|
|
6
|
-
}
|
|
7
|
-
export function useFormValidation<T>(props: FormValidationProps<T>, state: FormValidationState, ref: RefObject<ValidatableElement | null> | undefined): void;
|
|
8
|
-
|
|
9
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;AAkBA,0BAA0B,gBAAgB,GAAG,mBAAmB,GAAG,iBAAiB,CAAC;AAErF,8BAA8B,CAAC,CAAE,SAAQ,WAAW,CAAC,CAAC;IACpD,KAAK,CAAC,EAAE,MAAM,IAAI,CAAA;CACnB;AAED,kCAAkC,CAAC,EAAE,KAAK,EAAE,oBAAoB,CAAC,CAAC,EAAE,KAAK,EAAE,mBAAmB,EAAE,GAAG,EAAE,UAAU,kBAAkB,GAAG,IAAI,CAAC,GAAG,SAAS,GAAG,IAAI,CA2F3J","sources":["packages/@react-aria/form/src/packages/@react-aria/form/src/useFormValidation.ts","packages/@react-aria/form/src/packages/@react-aria/form/src/index.ts","packages/@react-aria/form/src/index.ts"],"sourcesContent":[null,null,"/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useFormValidation} from './useFormValidation';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
var $j3sn1$reactariautils = require("@react-aria/utils");
|
|
2
|
-
var $j3sn1$reactariainteractions = require("@react-aria/interactions");
|
|
3
|
-
var $j3sn1$react = require("react");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function $parcel$export(e, n, v, s) {
|
|
7
|
-
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
$parcel$export(module.exports, "useFormValidation", () => $9da8e3f192aba981$export$b8473d3665f3a75a);
|
|
11
|
-
/*
|
|
12
|
-
* Copyright 2023 Adobe. All rights reserved.
|
|
13
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
14
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
15
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
-
*
|
|
17
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
18
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
19
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
20
|
-
* governing permissions and limitations under the License.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
function $9da8e3f192aba981$export$b8473d3665f3a75a(props, state, ref) {
|
|
25
|
-
let { validationBehavior: validationBehavior, focus: focus } = props;
|
|
26
|
-
// This is a useLayoutEffect so that it runs before the useEffect in useFormValidationState, which commits the validation change.
|
|
27
|
-
(0, $j3sn1$reactariautils.useLayoutEffect)(()=>{
|
|
28
|
-
if (validationBehavior === 'native' && (ref === null || ref === void 0 ? void 0 : ref.current) && !ref.current.disabled) {
|
|
29
|
-
let errorMessage = state.realtimeValidation.isInvalid ? state.realtimeValidation.validationErrors.join(' ') || 'Invalid value.' : '';
|
|
30
|
-
ref.current.setCustomValidity(errorMessage);
|
|
31
|
-
// Prevent default tooltip for validation message.
|
|
32
|
-
// https://bugzilla.mozilla.org/show_bug.cgi?id=605277
|
|
33
|
-
if (!ref.current.hasAttribute('title')) ref.current.title = '';
|
|
34
|
-
if (!state.realtimeValidation.isInvalid) state.updateValidation($9da8e3f192aba981$var$getNativeValidity(ref.current));
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
let isIgnoredReset = (0, $j3sn1$react.useRef)(false);
|
|
38
|
-
let onReset = (0, $j3sn1$reactariautils.useEffectEvent)(()=>{
|
|
39
|
-
if (!isIgnoredReset.current) state.resetValidation();
|
|
40
|
-
});
|
|
41
|
-
let onInvalid = (0, $j3sn1$reactariautils.useEffectEvent)((e)=>{
|
|
42
|
-
var _ref_current;
|
|
43
|
-
// Only commit validation if we are not already displaying one.
|
|
44
|
-
// This avoids clearing server errors that the user didn't actually fix.
|
|
45
|
-
if (!state.displayValidation.isInvalid) state.commitValidation();
|
|
46
|
-
// Auto focus the first invalid input in a form, unless the error already had its default prevented.
|
|
47
|
-
let form = ref === null || ref === void 0 ? void 0 : (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.form;
|
|
48
|
-
if (!e.defaultPrevented && ref && form && $9da8e3f192aba981$var$getFirstInvalidInput(form) === ref.current) {
|
|
49
|
-
var _ref_current1;
|
|
50
|
-
if (focus) focus();
|
|
51
|
-
else (_ref_current1 = ref.current) === null || _ref_current1 === void 0 ? void 0 : _ref_current1.focus();
|
|
52
|
-
// Always show focus ring.
|
|
53
|
-
(0, $j3sn1$reactariainteractions.setInteractionModality)('keyboard');
|
|
54
|
-
}
|
|
55
|
-
// Prevent default browser error UI from appearing.
|
|
56
|
-
e.preventDefault();
|
|
57
|
-
});
|
|
58
|
-
let onChange = (0, $j3sn1$reactariautils.useEffectEvent)(()=>{
|
|
59
|
-
state.commitValidation();
|
|
60
|
-
});
|
|
61
|
-
(0, $j3sn1$react.useEffect)(()=>{
|
|
62
|
-
let input = ref === null || ref === void 0 ? void 0 : ref.current;
|
|
63
|
-
if (!input) return;
|
|
64
|
-
let form = input.form;
|
|
65
|
-
let reset = form === null || form === void 0 ? void 0 : form.reset;
|
|
66
|
-
if (form) // Try to detect React's automatic form reset behavior so we don't clear
|
|
67
|
-
// validation errors that are returned by server actions.
|
|
68
|
-
// To do this, we ignore programmatic form resets that occur outside a user event.
|
|
69
|
-
// This is best-effort. There may be false positives, e.g. setTimeout.
|
|
70
|
-
form.reset = ()=>{
|
|
71
|
-
// React uses MessageChannel for scheduling, so ignore 'message' events.
|
|
72
|
-
isIgnoredReset.current = !window.event || window.event.type === 'message' && (0, $j3sn1$reactariautils.getEventTarget)(window.event) instanceof MessagePort;
|
|
73
|
-
reset === null || reset === void 0 ? void 0 : reset.call(form);
|
|
74
|
-
isIgnoredReset.current = false;
|
|
75
|
-
};
|
|
76
|
-
input.addEventListener('invalid', onInvalid);
|
|
77
|
-
input.addEventListener('change', onChange);
|
|
78
|
-
form === null || form === void 0 ? void 0 : form.addEventListener('reset', onReset);
|
|
79
|
-
return ()=>{
|
|
80
|
-
input.removeEventListener('invalid', onInvalid);
|
|
81
|
-
input.removeEventListener('change', onChange);
|
|
82
|
-
form === null || form === void 0 ? void 0 : form.removeEventListener('reset', onReset);
|
|
83
|
-
if (form) // @ts-ignore
|
|
84
|
-
form.reset = reset;
|
|
85
|
-
};
|
|
86
|
-
}, [
|
|
87
|
-
ref,
|
|
88
|
-
validationBehavior
|
|
89
|
-
]);
|
|
90
|
-
}
|
|
91
|
-
function $9da8e3f192aba981$var$getValidity(input) {
|
|
92
|
-
// The native ValidityState object is live, meaning each property is a getter that returns the current state.
|
|
93
|
-
// We need to create a snapshot of the validity state at the time this function is called to avoid unpredictable React renders.
|
|
94
|
-
let validity = input.validity;
|
|
95
|
-
return {
|
|
96
|
-
badInput: validity.badInput,
|
|
97
|
-
customError: validity.customError,
|
|
98
|
-
patternMismatch: validity.patternMismatch,
|
|
99
|
-
rangeOverflow: validity.rangeOverflow,
|
|
100
|
-
rangeUnderflow: validity.rangeUnderflow,
|
|
101
|
-
stepMismatch: validity.stepMismatch,
|
|
102
|
-
tooLong: validity.tooLong,
|
|
103
|
-
tooShort: validity.tooShort,
|
|
104
|
-
typeMismatch: validity.typeMismatch,
|
|
105
|
-
valueMissing: validity.valueMissing,
|
|
106
|
-
valid: validity.valid
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
function $9da8e3f192aba981$var$getNativeValidity(input) {
|
|
110
|
-
return {
|
|
111
|
-
isInvalid: !input.validity.valid,
|
|
112
|
-
validationDetails: $9da8e3f192aba981$var$getValidity(input),
|
|
113
|
-
validationErrors: input.validationMessage ? [
|
|
114
|
-
input.validationMessage
|
|
115
|
-
] : []
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
function $9da8e3f192aba981$var$getFirstInvalidInput(form) {
|
|
119
|
-
for(let i = 0; i < form.elements.length; i++){
|
|
120
|
-
var _element_validity;
|
|
121
|
-
let element = form.elements[i];
|
|
122
|
-
if (((_element_validity = element.validity) === null || _element_validity === void 0 ? void 0 : _element_validity.valid) === false) return element;
|
|
123
|
-
}
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
//# sourceMappingURL=useFormValidation.main.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;AAcM,SAAS,0CAAqB,KAA6B,EAAE,KAA0B,EAAE,GAAqD;IACnJ,IAAI,sBAAC,kBAAkB,SAAE,KAAK,EAAC,GAAG;IAElC,iIAAiI;IACjI,CAAA,GAAA,qCAAc,EAAE;QACd,IAAI,uBAAuB,aAAY,gBAAA,0BAAA,IAAK,OAAO,KAAI,CAAC,IAAI,OAAO,CAAC,QAAQ,EAAE;YAC5E,IAAI,eAAe,MAAM,kBAAkB,CAAC,SAAS,GAAG,MAAM,kBAAkB,CAAC,gBAAgB,CAAC,IAAI,CAAC,QAAQ,mBAAmB;YAClI,IAAI,OAAO,CAA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AK,QAAQ,CAAC,EAAE;QAC9B,IAAI,EAAA,oBAAA,QAAQ,QAAQ,cAAhB,wCAAA,kBAAkB,KAAK,MAAK,OAC9B,OAAO;IAEX;IAEA,OAAO;AACT","sources":["packages/@react-aria/form/src/useFormValidation.ts"],"sourcesContent":["/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {FormValidationState} from '@react-stately/form';\nimport {getEventTarget, useEffectEvent, useLayoutEffect} from '@react-aria/utils';\nimport {RefObject, Validation, ValidationResult} from '@react-types/shared';\nimport {setInteractionModality} from '@react-aria/interactions';\nimport {useEffect, useRef} from 'react';\n\ntype ValidatableElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;\n\ninterface FormValidationProps<T> extends Validation<T> {\n focus?: () => void\n}\n\nexport function useFormValidation<T>(props: FormValidationProps<T>, state: FormValidationState, ref: RefObject<ValidatableElement | null> | undefined): void {\n let {validationBehavior, focus} = props;\n\n // This is a useLayoutEffect so that it runs before the useEffect in useFormValidationState, which commits the validation change.\n useLayoutEffect(() => {\n if (validationBehavior === 'native' && ref?.current && !ref.current.disabled) {\n let errorMessage = state.realtimeValidation.isInvalid ? state.realtimeValidation.validationErrors.join(' ') || 'Invalid value.' : '';\n ref.current.setCustomValidity(errorMessage);\n\n // Prevent default tooltip for validation message.\n // https://bugzilla.mozilla.org/show_bug.cgi?id=605277\n if (!ref.current.hasAttribute('title')) {\n ref.current.title = '';\n }\n\n if (!state.realtimeValidation.isInvalid) {\n state.updateValidation(getNativeValidity(ref.current));\n }\n }\n });\n\n let isIgnoredReset = useRef(false);\n let onReset = useEffectEvent(() => {\n if (!isIgnoredReset.current) {\n state.resetValidation();\n }\n });\n\n let onInvalid = useEffectEvent((e: Event) => {\n // Only commit validation if we are not already displaying one.\n // This avoids clearing server errors that the user didn't actually fix.\n if (!state.displayValidation.isInvalid) {\n state.commitValidation();\n }\n\n // Auto focus the first invalid input in a form, unless the error already had its default prevented.\n let form = ref?.current?.form;\n if (!e.defaultPrevented && ref && form && getFirstInvalidInput(form) === ref.current) {\n if (focus) {\n focus();\n } else {\n ref.current?.focus();\n }\n\n // Always show focus ring.\n setInteractionModality('keyboard');\n }\n\n // Prevent default browser error UI from appearing.\n e.preventDefault();\n });\n\n let onChange = useEffectEvent(() => {\n state.commitValidation();\n });\n\n useEffect(() => {\n let input = ref?.current;\n if (!input) {\n return;\n }\n\n let form = input.form;\n\n let reset = form?.reset;\n if (form) {\n // Try to detect React's automatic form reset behavior so we don't clear\n // validation errors that are returned by server actions.\n // To do this, we ignore programmatic form resets that occur outside a user event.\n // This is best-effort. There may be false positives, e.g. setTimeout.\n form.reset = () => {\n // React uses MessageChannel for scheduling, so ignore 'message' events.\n isIgnoredReset.current = !window.event || (window.event.type === 'message' && getEventTarget(window.event) instanceof MessagePort);\n reset?.call(form);\n isIgnoredReset.current = false;\n };\n }\n\n input.addEventListener('invalid', onInvalid);\n input.addEventListener('change', onChange);\n form?.addEventListener('reset', onReset);\n return () => {\n input!.removeEventListener('invalid', onInvalid);\n input!.removeEventListener('change', onChange);\n form?.removeEventListener('reset', onReset);\n if (form) {\n // @ts-ignore\n form.reset = reset;\n }\n };\n }, [ref, validationBehavior]);\n}\n\nfunction getValidity(input: ValidatableElement) {\n // The native ValidityState object is live, meaning each property is a getter that returns the current state.\n // We need to create a snapshot of the validity state at the time this function is called to avoid unpredictable React renders.\n let validity = input.validity;\n return {\n badInput: validity.badInput,\n customError: validity.customError,\n patternMismatch: validity.patternMismatch,\n rangeOverflow: validity.rangeOverflow,\n rangeUnderflow: validity.rangeUnderflow,\n stepMismatch: validity.stepMismatch,\n tooLong: validity.tooLong,\n tooShort: validity.tooShort,\n typeMismatch: validity.typeMismatch,\n valueMissing: validity.valueMissing,\n valid: validity.valid\n };\n}\n\nfunction getNativeValidity(input: ValidatableElement): ValidationResult {\n return {\n isInvalid: !input.validity.valid,\n validationDetails: getValidity(input),\n validationErrors: input.validationMessage ? [input.validationMessage] : []\n };\n}\n\nfunction getFirstInvalidInput(form: HTMLFormElement): ValidatableElement | null {\n for (let i = 0; i < form.elements.length; i++) {\n let element = form.elements[i] as ValidatableElement;\n if (element.validity?.valid === false) {\n return element;\n }\n }\n\n return null;\n}\n"],"names":[],"version":3,"file":"useFormValidation.main.js.map"}
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import {useLayoutEffect as $9Gacy$useLayoutEffect, useEffectEvent as $9Gacy$useEffectEvent, getEventTarget as $9Gacy$getEventTarget} from "@react-aria/utils";
|
|
2
|
-
import {setInteractionModality as $9Gacy$setInteractionModality} from "@react-aria/interactions";
|
|
3
|
-
import {useRef as $9Gacy$useRef, useEffect as $9Gacy$useEffect} from "react";
|
|
4
|
-
|
|
5
|
-
/*
|
|
6
|
-
* Copyright 2023 Adobe. All rights reserved.
|
|
7
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
8
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
9
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
12
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
13
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
14
|
-
* governing permissions and limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function $e93e671b31057976$export$b8473d3665f3a75a(props, state, ref) {
|
|
19
|
-
let { validationBehavior: validationBehavior, focus: focus } = props;
|
|
20
|
-
// This is a useLayoutEffect so that it runs before the useEffect in useFormValidationState, which commits the validation change.
|
|
21
|
-
(0, $9Gacy$useLayoutEffect)(()=>{
|
|
22
|
-
if (validationBehavior === 'native' && (ref === null || ref === void 0 ? void 0 : ref.current) && !ref.current.disabled) {
|
|
23
|
-
let errorMessage = state.realtimeValidation.isInvalid ? state.realtimeValidation.validationErrors.join(' ') || 'Invalid value.' : '';
|
|
24
|
-
ref.current.setCustomValidity(errorMessage);
|
|
25
|
-
// Prevent default tooltip for validation message.
|
|
26
|
-
// https://bugzilla.mozilla.org/show_bug.cgi?id=605277
|
|
27
|
-
if (!ref.current.hasAttribute('title')) ref.current.title = '';
|
|
28
|
-
if (!state.realtimeValidation.isInvalid) state.updateValidation($e93e671b31057976$var$getNativeValidity(ref.current));
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
let isIgnoredReset = (0, $9Gacy$useRef)(false);
|
|
32
|
-
let onReset = (0, $9Gacy$useEffectEvent)(()=>{
|
|
33
|
-
if (!isIgnoredReset.current) state.resetValidation();
|
|
34
|
-
});
|
|
35
|
-
let onInvalid = (0, $9Gacy$useEffectEvent)((e)=>{
|
|
36
|
-
var _ref_current;
|
|
37
|
-
// Only commit validation if we are not already displaying one.
|
|
38
|
-
// This avoids clearing server errors that the user didn't actually fix.
|
|
39
|
-
if (!state.displayValidation.isInvalid) state.commitValidation();
|
|
40
|
-
// Auto focus the first invalid input in a form, unless the error already had its default prevented.
|
|
41
|
-
let form = ref === null || ref === void 0 ? void 0 : (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.form;
|
|
42
|
-
if (!e.defaultPrevented && ref && form && $e93e671b31057976$var$getFirstInvalidInput(form) === ref.current) {
|
|
43
|
-
var _ref_current1;
|
|
44
|
-
if (focus) focus();
|
|
45
|
-
else (_ref_current1 = ref.current) === null || _ref_current1 === void 0 ? void 0 : _ref_current1.focus();
|
|
46
|
-
// Always show focus ring.
|
|
47
|
-
(0, $9Gacy$setInteractionModality)('keyboard');
|
|
48
|
-
}
|
|
49
|
-
// Prevent default browser error UI from appearing.
|
|
50
|
-
e.preventDefault();
|
|
51
|
-
});
|
|
52
|
-
let onChange = (0, $9Gacy$useEffectEvent)(()=>{
|
|
53
|
-
state.commitValidation();
|
|
54
|
-
});
|
|
55
|
-
(0, $9Gacy$useEffect)(()=>{
|
|
56
|
-
let input = ref === null || ref === void 0 ? void 0 : ref.current;
|
|
57
|
-
if (!input) return;
|
|
58
|
-
let form = input.form;
|
|
59
|
-
let reset = form === null || form === void 0 ? void 0 : form.reset;
|
|
60
|
-
if (form) // Try to detect React's automatic form reset behavior so we don't clear
|
|
61
|
-
// validation errors that are returned by server actions.
|
|
62
|
-
// To do this, we ignore programmatic form resets that occur outside a user event.
|
|
63
|
-
// This is best-effort. There may be false positives, e.g. setTimeout.
|
|
64
|
-
form.reset = ()=>{
|
|
65
|
-
// React uses MessageChannel for scheduling, so ignore 'message' events.
|
|
66
|
-
isIgnoredReset.current = !window.event || window.event.type === 'message' && (0, $9Gacy$getEventTarget)(window.event) instanceof MessagePort;
|
|
67
|
-
reset === null || reset === void 0 ? void 0 : reset.call(form);
|
|
68
|
-
isIgnoredReset.current = false;
|
|
69
|
-
};
|
|
70
|
-
input.addEventListener('invalid', onInvalid);
|
|
71
|
-
input.addEventListener('change', onChange);
|
|
72
|
-
form === null || form === void 0 ? void 0 : form.addEventListener('reset', onReset);
|
|
73
|
-
return ()=>{
|
|
74
|
-
input.removeEventListener('invalid', onInvalid);
|
|
75
|
-
input.removeEventListener('change', onChange);
|
|
76
|
-
form === null || form === void 0 ? void 0 : form.removeEventListener('reset', onReset);
|
|
77
|
-
if (form) // @ts-ignore
|
|
78
|
-
form.reset = reset;
|
|
79
|
-
};
|
|
80
|
-
}, [
|
|
81
|
-
ref,
|
|
82
|
-
validationBehavior
|
|
83
|
-
]);
|
|
84
|
-
}
|
|
85
|
-
function $e93e671b31057976$var$getValidity(input) {
|
|
86
|
-
// The native ValidityState object is live, meaning each property is a getter that returns the current state.
|
|
87
|
-
// We need to create a snapshot of the validity state at the time this function is called to avoid unpredictable React renders.
|
|
88
|
-
let validity = input.validity;
|
|
89
|
-
return {
|
|
90
|
-
badInput: validity.badInput,
|
|
91
|
-
customError: validity.customError,
|
|
92
|
-
patternMismatch: validity.patternMismatch,
|
|
93
|
-
rangeOverflow: validity.rangeOverflow,
|
|
94
|
-
rangeUnderflow: validity.rangeUnderflow,
|
|
95
|
-
stepMismatch: validity.stepMismatch,
|
|
96
|
-
tooLong: validity.tooLong,
|
|
97
|
-
tooShort: validity.tooShort,
|
|
98
|
-
typeMismatch: validity.typeMismatch,
|
|
99
|
-
valueMissing: validity.valueMissing,
|
|
100
|
-
valid: validity.valid
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
function $e93e671b31057976$var$getNativeValidity(input) {
|
|
104
|
-
return {
|
|
105
|
-
isInvalid: !input.validity.valid,
|
|
106
|
-
validationDetails: $e93e671b31057976$var$getValidity(input),
|
|
107
|
-
validationErrors: input.validationMessage ? [
|
|
108
|
-
input.validationMessage
|
|
109
|
-
] : []
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
function $e93e671b31057976$var$getFirstInvalidInput(form) {
|
|
113
|
-
for(let i = 0; i < form.elements.length; i++){
|
|
114
|
-
var _element_validity;
|
|
115
|
-
let element = form.elements[i];
|
|
116
|
-
if (((_element_validity = element.validity) === null || _element_validity === void 0 ? void 0 : _element_validity.valid) === false) return element;
|
|
117
|
-
}
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
export {$e93e671b31057976$export$b8473d3665f3a75a as useFormValidation};
|
|
123
|
-
//# sourceMappingURL=useFormValidation.module.js.map
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import {useLayoutEffect as $9Gacy$useLayoutEffect, useEffectEvent as $9Gacy$useEffectEvent, getEventTarget as $9Gacy$getEventTarget} from "@react-aria/utils";
|
|
2
|
-
import {setInteractionModality as $9Gacy$setInteractionModality} from "@react-aria/interactions";
|
|
3
|
-
import {useRef as $9Gacy$useRef, useEffect as $9Gacy$useEffect} from "react";
|
|
4
|
-
|
|
5
|
-
/*
|
|
6
|
-
* Copyright 2023 Adobe. All rights reserved.
|
|
7
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
8
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
9
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
12
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
13
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
14
|
-
* governing permissions and limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function $e93e671b31057976$export$b8473d3665f3a75a(props, state, ref) {
|
|
19
|
-
let { validationBehavior: validationBehavior, focus: focus } = props;
|
|
20
|
-
// This is a useLayoutEffect so that it runs before the useEffect in useFormValidationState, which commits the validation change.
|
|
21
|
-
(0, $9Gacy$useLayoutEffect)(()=>{
|
|
22
|
-
if (validationBehavior === 'native' && (ref === null || ref === void 0 ? void 0 : ref.current) && !ref.current.disabled) {
|
|
23
|
-
let errorMessage = state.realtimeValidation.isInvalid ? state.realtimeValidation.validationErrors.join(' ') || 'Invalid value.' : '';
|
|
24
|
-
ref.current.setCustomValidity(errorMessage);
|
|
25
|
-
// Prevent default tooltip for validation message.
|
|
26
|
-
// https://bugzilla.mozilla.org/show_bug.cgi?id=605277
|
|
27
|
-
if (!ref.current.hasAttribute('title')) ref.current.title = '';
|
|
28
|
-
if (!state.realtimeValidation.isInvalid) state.updateValidation($e93e671b31057976$var$getNativeValidity(ref.current));
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
let isIgnoredReset = (0, $9Gacy$useRef)(false);
|
|
32
|
-
let onReset = (0, $9Gacy$useEffectEvent)(()=>{
|
|
33
|
-
if (!isIgnoredReset.current) state.resetValidation();
|
|
34
|
-
});
|
|
35
|
-
let onInvalid = (0, $9Gacy$useEffectEvent)((e)=>{
|
|
36
|
-
var _ref_current;
|
|
37
|
-
// Only commit validation if we are not already displaying one.
|
|
38
|
-
// This avoids clearing server errors that the user didn't actually fix.
|
|
39
|
-
if (!state.displayValidation.isInvalid) state.commitValidation();
|
|
40
|
-
// Auto focus the first invalid input in a form, unless the error already had its default prevented.
|
|
41
|
-
let form = ref === null || ref === void 0 ? void 0 : (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.form;
|
|
42
|
-
if (!e.defaultPrevented && ref && form && $e93e671b31057976$var$getFirstInvalidInput(form) === ref.current) {
|
|
43
|
-
var _ref_current1;
|
|
44
|
-
if (focus) focus();
|
|
45
|
-
else (_ref_current1 = ref.current) === null || _ref_current1 === void 0 ? void 0 : _ref_current1.focus();
|
|
46
|
-
// Always show focus ring.
|
|
47
|
-
(0, $9Gacy$setInteractionModality)('keyboard');
|
|
48
|
-
}
|
|
49
|
-
// Prevent default browser error UI from appearing.
|
|
50
|
-
e.preventDefault();
|
|
51
|
-
});
|
|
52
|
-
let onChange = (0, $9Gacy$useEffectEvent)(()=>{
|
|
53
|
-
state.commitValidation();
|
|
54
|
-
});
|
|
55
|
-
(0, $9Gacy$useEffect)(()=>{
|
|
56
|
-
let input = ref === null || ref === void 0 ? void 0 : ref.current;
|
|
57
|
-
if (!input) return;
|
|
58
|
-
let form = input.form;
|
|
59
|
-
let reset = form === null || form === void 0 ? void 0 : form.reset;
|
|
60
|
-
if (form) // Try to detect React's automatic form reset behavior so we don't clear
|
|
61
|
-
// validation errors that are returned by server actions.
|
|
62
|
-
// To do this, we ignore programmatic form resets that occur outside a user event.
|
|
63
|
-
// This is best-effort. There may be false positives, e.g. setTimeout.
|
|
64
|
-
form.reset = ()=>{
|
|
65
|
-
// React uses MessageChannel for scheduling, so ignore 'message' events.
|
|
66
|
-
isIgnoredReset.current = !window.event || window.event.type === 'message' && (0, $9Gacy$getEventTarget)(window.event) instanceof MessagePort;
|
|
67
|
-
reset === null || reset === void 0 ? void 0 : reset.call(form);
|
|
68
|
-
isIgnoredReset.current = false;
|
|
69
|
-
};
|
|
70
|
-
input.addEventListener('invalid', onInvalid);
|
|
71
|
-
input.addEventListener('change', onChange);
|
|
72
|
-
form === null || form === void 0 ? void 0 : form.addEventListener('reset', onReset);
|
|
73
|
-
return ()=>{
|
|
74
|
-
input.removeEventListener('invalid', onInvalid);
|
|
75
|
-
input.removeEventListener('change', onChange);
|
|
76
|
-
form === null || form === void 0 ? void 0 : form.removeEventListener('reset', onReset);
|
|
77
|
-
if (form) // @ts-ignore
|
|
78
|
-
form.reset = reset;
|
|
79
|
-
};
|
|
80
|
-
}, [
|
|
81
|
-
ref,
|
|
82
|
-
validationBehavior
|
|
83
|
-
]);
|
|
84
|
-
}
|
|
85
|
-
function $e93e671b31057976$var$getValidity(input) {
|
|
86
|
-
// The native ValidityState object is live, meaning each property is a getter that returns the current state.
|
|
87
|
-
// We need to create a snapshot of the validity state at the time this function is called to avoid unpredictable React renders.
|
|
88
|
-
let validity = input.validity;
|
|
89
|
-
return {
|
|
90
|
-
badInput: validity.badInput,
|
|
91
|
-
customError: validity.customError,
|
|
92
|
-
patternMismatch: validity.patternMismatch,
|
|
93
|
-
rangeOverflow: validity.rangeOverflow,
|
|
94
|
-
rangeUnderflow: validity.rangeUnderflow,
|
|
95
|
-
stepMismatch: validity.stepMismatch,
|
|
96
|
-
tooLong: validity.tooLong,
|
|
97
|
-
tooShort: validity.tooShort,
|
|
98
|
-
typeMismatch: validity.typeMismatch,
|
|
99
|
-
valueMissing: validity.valueMissing,
|
|
100
|
-
valid: validity.valid
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
function $e93e671b31057976$var$getNativeValidity(input) {
|
|
104
|
-
return {
|
|
105
|
-
isInvalid: !input.validity.valid,
|
|
106
|
-
validationDetails: $e93e671b31057976$var$getValidity(input),
|
|
107
|
-
validationErrors: input.validationMessage ? [
|
|
108
|
-
input.validationMessage
|
|
109
|
-
] : []
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
function $e93e671b31057976$var$getFirstInvalidInput(form) {
|
|
113
|
-
for(let i = 0; i < form.elements.length; i++){
|
|
114
|
-
var _element_validity;
|
|
115
|
-
let element = form.elements[i];
|
|
116
|
-
if (((_element_validity = element.validity) === null || _element_validity === void 0 ? void 0 : _element_validity.valid) === false) return element;
|
|
117
|
-
}
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
export {$e93e671b31057976$export$b8473d3665f3a75a as useFormValidation};
|
|
123
|
-
//# sourceMappingURL=useFormValidation.module.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;AAAA;;;;;;;;;;CAUC;;;AAcM,SAAS,0CAAqB,KAA6B,EAAE,KAA0B,EAAE,GAAqD;IACnJ,IAAI,sBAAC,kBAAkB,SAAE,KAAK,EAAC,GAAG;IAElC,iIAAiI;IACjI,CAAA,GAAA,sBAAc,EAAE;QACd,IAAI,uBAAuB,aAAY,gBAAA,0BAAA,IAAK,OAAO,KAAI,CAAC,IAAI,OAAO,CAAC,QAAQ,EAAE;YAC5E,IAAI,eAAe,MAAM,kBAAkB,CAAC,SAAS,GAAG,MAAM,kBAAkB,CAAC,gBAAgB,CAAC,IAAI,CAAC,QAAQ,mBAAmB;YAClI,IAAI,OAAO,CAAC,iBAAiB,CAAC;YAE9B,kDAAkD;YAClD,sDAAsD;YACtD,IAAI,CAAC,IAAI,OAAO,CAAC,YAAY,CAAC,UAC5B,IAAI,OAAO,CAAC,KAAK,GAAG;YAGtB,IAAI,CAAC,MAAM,kBAAkB,CAAC,SAAS,EACrC,MAAM,gBAAgB,CAAC,wCAAkB,IAAI,OAAO;QAExD;IACF;IAEA,IAAI,iBAAiB,CAAA,GAAA,aAAK,EAAE;IAC5B,IAAI,UAAU,CAAA,GAAA,q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AChC,OAAO;YACL,MAAO,mBAAmB,CAAC,WAAW;YACtC,MAAO,mBAAmB,CAAC,UAAU;YACrC,iBAAA,2BAAA,KAAM,mBAAmB,CAAC,SAAS;YACnC,IAAI,MACF,aAAa;YACb,KAAK,KAAK,GAAG;QAEjB;IACF,GAAG;QAAC;QAAK;KAAmB;AAC9B;AAEA,SAAS,kCAAY,KAAyB;IAC5C,6GAA6G;IAC7G,+HAA+H;IAC/H,IAAI,WAAW,MAAM,QAAQ;IAC7B,OAAO;QACL,UAAU,SAAS,QAAQ;QAC3B,aAAa,SAAS,WAAW;QACjC,iBAAiB,SAAS,eAAe;QACzC,eAAe,SAAS,aAAa;QACrC,gBAAgB,SAAS,cAAc;QACvC,cAAc,SAAS,YAAY;QACnC,SAAS,SAAS,OAAO;QACzB,UAAU,SAAS,QAAQ;QAC3B,cAAc,SAAS,YAAY;QACnC,cAAc,SAAS,YAAY;QACnC,OAAO,SAAS,KAAK;IACvB;AACF;AAEA,SAAS,wCAAkB,KAAyB;IAClD,OAAO;QACL,WAAW,CAAC,MAAM,QAAQ,CAAC,KAAK;QAChC,mBAAmB,kCAAY;QAC/B,kBAAkB,MAAM,iBAAiB,GAAG;YAAC,MAAM,iBAAiB;SAAC,GAAG,EAAE;IAC5E;AACF;AAEA,SAAS,2CAAqB,IAAqB;IACjD,IAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,CAAC,MAAM,EAAE,IAAK;YAEzC;QADJ,IAAI,UAAU,KAAK,QAAQ,CAAC,EAAE;QAC9B,IAAI,EAAA,oBAAA,QAAQ,QAAQ,cAAhB,wCAAA,kBAAkB,KAAK,MAAK,OAC9B,OAAO;IAEX;IAEA,OAAO;AACT","sources":["packages/@react-aria/form/src/useFormValidation.ts"],"sourcesContent":["/*\n * Copyright 2023 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {FormValidationState} from '@react-stately/form';\nimport {getEventTarget, useEffectEvent, useLayoutEffect} from '@react-aria/utils';\nimport {RefObject, Validation, ValidationResult} from '@react-types/shared';\nimport {setInteractionModality} from '@react-aria/interactions';\nimport {useEffect, useRef} from 'react';\n\ntype ValidatableElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;\n\ninterface FormValidationProps<T> extends Validation<T> {\n focus?: () => void\n}\n\nexport function useFormValidation<T>(props: FormValidationProps<T>, state: FormValidationState, ref: RefObject<ValidatableElement | null> | undefined): void {\n let {validationBehavior, focus} = props;\n\n // This is a useLayoutEffect so that it runs before the useEffect in useFormValidationState, which commits the validation change.\n useLayoutEffect(() => {\n if (validationBehavior === 'native' && ref?.current && !ref.current.disabled) {\n let errorMessage = state.realtimeValidation.isInvalid ? state.realtimeValidation.validationErrors.join(' ') || 'Invalid value.' : '';\n ref.current.setCustomValidity(errorMessage);\n\n // Prevent default tooltip for validation message.\n // https://bugzilla.mozilla.org/show_bug.cgi?id=605277\n if (!ref.current.hasAttribute('title')) {\n ref.current.title = '';\n }\n\n if (!state.realtimeValidation.isInvalid) {\n state.updateValidation(getNativeValidity(ref.current));\n }\n }\n });\n\n let isIgnoredReset = useRef(false);\n let onReset = useEffectEvent(() => {\n if (!isIgnoredReset.current) {\n state.resetValidation();\n }\n });\n\n let onInvalid = useEffectEvent((e: Event) => {\n // Only commit validation if we are not already displaying one.\n // This avoids clearing server errors that the user didn't actually fix.\n if (!state.displayValidation.isInvalid) {\n state.commitValidation();\n }\n\n // Auto focus the first invalid input in a form, unless the error already had its default prevented.\n let form = ref?.current?.form;\n if (!e.defaultPrevented && ref && form && getFirstInvalidInput(form) === ref.current) {\n if (focus) {\n focus();\n } else {\n ref.current?.focus();\n }\n\n // Always show focus ring.\n setInteractionModality('keyboard');\n }\n\n // Prevent default browser error UI from appearing.\n e.preventDefault();\n });\n\n let onChange = useEffectEvent(() => {\n state.commitValidation();\n });\n\n useEffect(() => {\n let input = ref?.current;\n if (!input) {\n return;\n }\n\n let form = input.form;\n\n let reset = form?.reset;\n if (form) {\n // Try to detect React's automatic form reset behavior so we don't clear\n // validation errors that are returned by server actions.\n // To do this, we ignore programmatic form resets that occur outside a user event.\n // This is best-effort. There may be false positives, e.g. setTimeout.\n form.reset = () => {\n // React uses MessageChannel for scheduling, so ignore 'message' events.\n isIgnoredReset.current = !window.event || (window.event.type === 'message' && getEventTarget(window.event) instanceof MessagePort);\n reset?.call(form);\n isIgnoredReset.current = false;\n };\n }\n\n input.addEventListener('invalid', onInvalid);\n input.addEventListener('change', onChange);\n form?.addEventListener('reset', onReset);\n return () => {\n input!.removeEventListener('invalid', onInvalid);\n input!.removeEventListener('change', onChange);\n form?.removeEventListener('reset', onReset);\n if (form) {\n // @ts-ignore\n form.reset = reset;\n }\n };\n }, [ref, validationBehavior]);\n}\n\nfunction getValidity(input: ValidatableElement) {\n // The native ValidityState object is live, meaning each property is a getter that returns the current state.\n // We need to create a snapshot of the validity state at the time this function is called to avoid unpredictable React renders.\n let validity = input.validity;\n return {\n badInput: validity.badInput,\n customError: validity.customError,\n patternMismatch: validity.patternMismatch,\n rangeOverflow: validity.rangeOverflow,\n rangeUnderflow: validity.rangeUnderflow,\n stepMismatch: validity.stepMismatch,\n tooLong: validity.tooLong,\n tooShort: validity.tooShort,\n typeMismatch: validity.typeMismatch,\n valueMissing: validity.valueMissing,\n valid: validity.valid\n };\n}\n\nfunction getNativeValidity(input: ValidatableElement): ValidationResult {\n return {\n isInvalid: !input.validity.valid,\n validationDetails: getValidity(input),\n validationErrors: input.validationMessage ? [input.validationMessage] : []\n };\n}\n\nfunction getFirstInvalidInput(form: HTMLFormElement): ValidatableElement | null {\n for (let i = 0; i < form.elements.length; i++) {\n let element = form.elements[i] as ValidatableElement;\n if (element.validity?.valid === false) {\n return element;\n }\n }\n\n return null;\n}\n"],"names":[],"version":3,"file":"useFormValidation.module.js.map"}
|
package/src/useFormValidation.ts
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2023 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {FormValidationState} from '@react-stately/form';
|
|
14
|
-
import {getEventTarget, useEffectEvent, useLayoutEffect} from '@react-aria/utils';
|
|
15
|
-
import {RefObject, Validation, ValidationResult} from '@react-types/shared';
|
|
16
|
-
import {setInteractionModality} from '@react-aria/interactions';
|
|
17
|
-
import {useEffect, useRef} from 'react';
|
|
18
|
-
|
|
19
|
-
type ValidatableElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
|
20
|
-
|
|
21
|
-
interface FormValidationProps<T> extends Validation<T> {
|
|
22
|
-
focus?: () => void
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function useFormValidation<T>(props: FormValidationProps<T>, state: FormValidationState, ref: RefObject<ValidatableElement | null> | undefined): void {
|
|
26
|
-
let {validationBehavior, focus} = props;
|
|
27
|
-
|
|
28
|
-
// This is a useLayoutEffect so that it runs before the useEffect in useFormValidationState, which commits the validation change.
|
|
29
|
-
useLayoutEffect(() => {
|
|
30
|
-
if (validationBehavior === 'native' && ref?.current && !ref.current.disabled) {
|
|
31
|
-
let errorMessage = state.realtimeValidation.isInvalid ? state.realtimeValidation.validationErrors.join(' ') || 'Invalid value.' : '';
|
|
32
|
-
ref.current.setCustomValidity(errorMessage);
|
|
33
|
-
|
|
34
|
-
// Prevent default tooltip for validation message.
|
|
35
|
-
// https://bugzilla.mozilla.org/show_bug.cgi?id=605277
|
|
36
|
-
if (!ref.current.hasAttribute('title')) {
|
|
37
|
-
ref.current.title = '';
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (!state.realtimeValidation.isInvalid) {
|
|
41
|
-
state.updateValidation(getNativeValidity(ref.current));
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
let isIgnoredReset = useRef(false);
|
|
47
|
-
let onReset = useEffectEvent(() => {
|
|
48
|
-
if (!isIgnoredReset.current) {
|
|
49
|
-
state.resetValidation();
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
let onInvalid = useEffectEvent((e: Event) => {
|
|
54
|
-
// Only commit validation if we are not already displaying one.
|
|
55
|
-
// This avoids clearing server errors that the user didn't actually fix.
|
|
56
|
-
if (!state.displayValidation.isInvalid) {
|
|
57
|
-
state.commitValidation();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Auto focus the first invalid input in a form, unless the error already had its default prevented.
|
|
61
|
-
let form = ref?.current?.form;
|
|
62
|
-
if (!e.defaultPrevented && ref && form && getFirstInvalidInput(form) === ref.current) {
|
|
63
|
-
if (focus) {
|
|
64
|
-
focus();
|
|
65
|
-
} else {
|
|
66
|
-
ref.current?.focus();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Always show focus ring.
|
|
70
|
-
setInteractionModality('keyboard');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Prevent default browser error UI from appearing.
|
|
74
|
-
e.preventDefault();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
let onChange = useEffectEvent(() => {
|
|
78
|
-
state.commitValidation();
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
useEffect(() => {
|
|
82
|
-
let input = ref?.current;
|
|
83
|
-
if (!input) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
let form = input.form;
|
|
88
|
-
|
|
89
|
-
let reset = form?.reset;
|
|
90
|
-
if (form) {
|
|
91
|
-
// Try to detect React's automatic form reset behavior so we don't clear
|
|
92
|
-
// validation errors that are returned by server actions.
|
|
93
|
-
// To do this, we ignore programmatic form resets that occur outside a user event.
|
|
94
|
-
// This is best-effort. There may be false positives, e.g. setTimeout.
|
|
95
|
-
form.reset = () => {
|
|
96
|
-
// React uses MessageChannel for scheduling, so ignore 'message' events.
|
|
97
|
-
isIgnoredReset.current = !window.event || (window.event.type === 'message' && getEventTarget(window.event) instanceof MessagePort);
|
|
98
|
-
reset?.call(form);
|
|
99
|
-
isIgnoredReset.current = false;
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
input.addEventListener('invalid', onInvalid);
|
|
104
|
-
input.addEventListener('change', onChange);
|
|
105
|
-
form?.addEventListener('reset', onReset);
|
|
106
|
-
return () => {
|
|
107
|
-
input!.removeEventListener('invalid', onInvalid);
|
|
108
|
-
input!.removeEventListener('change', onChange);
|
|
109
|
-
form?.removeEventListener('reset', onReset);
|
|
110
|
-
if (form) {
|
|
111
|
-
// @ts-ignore
|
|
112
|
-
form.reset = reset;
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
}, [ref, validationBehavior]);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function getValidity(input: ValidatableElement) {
|
|
119
|
-
// The native ValidityState object is live, meaning each property is a getter that returns the current state.
|
|
120
|
-
// We need to create a snapshot of the validity state at the time this function is called to avoid unpredictable React renders.
|
|
121
|
-
let validity = input.validity;
|
|
122
|
-
return {
|
|
123
|
-
badInput: validity.badInput,
|
|
124
|
-
customError: validity.customError,
|
|
125
|
-
patternMismatch: validity.patternMismatch,
|
|
126
|
-
rangeOverflow: validity.rangeOverflow,
|
|
127
|
-
rangeUnderflow: validity.rangeUnderflow,
|
|
128
|
-
stepMismatch: validity.stepMismatch,
|
|
129
|
-
tooLong: validity.tooLong,
|
|
130
|
-
tooShort: validity.tooShort,
|
|
131
|
-
typeMismatch: validity.typeMismatch,
|
|
132
|
-
valueMissing: validity.valueMissing,
|
|
133
|
-
valid: validity.valid
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function getNativeValidity(input: ValidatableElement): ValidationResult {
|
|
138
|
-
return {
|
|
139
|
-
isInvalid: !input.validity.valid,
|
|
140
|
-
validationDetails: getValidity(input),
|
|
141
|
-
validationErrors: input.validationMessage ? [input.validationMessage] : []
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function getFirstInvalidInput(form: HTMLFormElement): ValidatableElement | null {
|
|
146
|
-
for (let i = 0; i < form.elements.length; i++) {
|
|
147
|
-
let element = form.elements[i] as ValidatableElement;
|
|
148
|
-
if (element.validity?.valid === false) {
|
|
149
|
-
return element;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return null;
|
|
154
|
-
}
|