@react-aria/form 3.0.0-nightly-641446f65-240905

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 ADDED
@@ -0,0 +1,3 @@
1
+ # @react-aria/form
2
+
3
+ This package is part of [react-spectrum](https://github.com/adobe/react-spectrum). See the repo for more details.
@@ -0,0 +1,17 @@
1
+ import {useFormValidation as $e93e671b31057976$export$b8473d3665f3a75a} from "./useFormValidation.mjs";
2
+
3
+ /*
4
+ * Copyright 2023 Adobe. All rights reserved.
5
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License. You may obtain a copy
7
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under
10
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
11
+ * OF ANY KIND, either express or implied. See the License for the specific language
12
+ * governing permissions and limitations under the License.
13
+ */
14
+
15
+
16
+ export {$e93e671b31057976$export$b8473d3665f3a75a as useFormValidation};
17
+ //# sourceMappingURL=module.js.map
package/dist/main.js ADDED
@@ -0,0 +1,22 @@
1
+ var $9da8e3f192aba981$exports = require("./useFormValidation.main.js");
2
+
3
+
4
+ function $parcel$export(e, n, v, s) {
5
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
6
+ }
7
+
8
+ $parcel$export(module.exports, "useFormValidation", () => $9da8e3f192aba981$exports.useFormValidation);
9
+ /*
10
+ * Copyright 2023 Adobe. All rights reserved.
11
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
12
+ * you may not use this file except in compliance with the License. You may obtain a copy
13
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software distributed under
16
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
17
+ * OF ANY KIND, either express or implied. See the License for the specific language
18
+ * governing permissions and limitations under the License.
19
+ */
20
+
21
+
22
+ //# sourceMappingURL=main.js.map
@@ -0,0 +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 './useFormValidation';\n"],"names":[],"version":3,"file":"main.js.map"}
package/dist/module.js ADDED
@@ -0,0 +1,17 @@
1
+ import {useFormValidation as $e93e671b31057976$export$b8473d3665f3a75a} from "./useFormValidation.module.js";
2
+
3
+ /*
4
+ * Copyright 2023 Adobe. All rights reserved.
5
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License. You may obtain a copy
7
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under
10
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
11
+ * OF ANY KIND, either express or implied. See the License for the specific language
12
+ * governing permissions and limitations under the License.
13
+ */
14
+
15
+
16
+ export {$e93e671b31057976$export$b8473d3665f3a75a as useFormValidation};
17
+ //# sourceMappingURL=module.js.map
@@ -0,0 +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 './useFormValidation';\n"],"names":[],"version":3,"file":"module.js.map"}
@@ -0,0 +1,9 @@
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
@@ -0,0 +1 @@
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,QAqEpJ","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"}
@@ -0,0 +1,116 @@
1
+ var $j3sn1$reactariainteractions = require("@react-aria/interactions");
2
+ var $j3sn1$react = require("react");
3
+ var $j3sn1$reactariautils = require("@react-aria/utils");
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)) {
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 onReset = (0, $j3sn1$reactariautils.useEffectEvent)(()=>{
38
+ state.resetValidation();
39
+ });
40
+ let onInvalid = (0, $j3sn1$reactariautils.useEffectEvent)((e)=>{
41
+ var _ref_current;
42
+ // Only commit validation if we are not already displaying one.
43
+ // This avoids clearing server errors that the user didn't actually fix.
44
+ if (!state.displayValidation.isInvalid) state.commitValidation();
45
+ // Auto focus the first invalid input in a form, unless the error already had its default prevented.
46
+ let form = ref === null || ref === void 0 ? void 0 : (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.form;
47
+ if (!e.defaultPrevented && ref && form && $9da8e3f192aba981$var$getFirstInvalidInput(form) === ref.current) {
48
+ var _ref_current1;
49
+ if (focus) focus();
50
+ else (_ref_current1 = ref.current) === null || _ref_current1 === void 0 ? void 0 : _ref_current1.focus();
51
+ // Always show focus ring.
52
+ (0, $j3sn1$reactariainteractions.setInteractionModality)('keyboard');
53
+ }
54
+ // Prevent default browser error UI from appearing.
55
+ e.preventDefault();
56
+ });
57
+ let onChange = (0, $j3sn1$reactariautils.useEffectEvent)(()=>{
58
+ state.commitValidation();
59
+ });
60
+ (0, $j3sn1$react.useEffect)(()=>{
61
+ let input = ref === null || ref === void 0 ? void 0 : ref.current;
62
+ if (!input) return;
63
+ let form = input.form;
64
+ input.addEventListener('invalid', onInvalid);
65
+ input.addEventListener('change', onChange);
66
+ form === null || form === void 0 ? void 0 : form.addEventListener('reset', onReset);
67
+ return ()=>{
68
+ input.removeEventListener('invalid', onInvalid);
69
+ input.removeEventListener('change', onChange);
70
+ form === null || form === void 0 ? void 0 : form.removeEventListener('reset', onReset);
71
+ };
72
+ }, [
73
+ ref,
74
+ onInvalid,
75
+ onChange,
76
+ onReset,
77
+ validationBehavior
78
+ ]);
79
+ }
80
+ function $9da8e3f192aba981$var$getValidity(input) {
81
+ // The native ValidityState object is live, meaning each property is a getter that returns the current state.
82
+ // We need to create a snapshot of the validity state at the time this function is called to avoid unpredictable React renders.
83
+ let validity = input.validity;
84
+ return {
85
+ badInput: validity.badInput,
86
+ customError: validity.customError,
87
+ patternMismatch: validity.patternMismatch,
88
+ rangeOverflow: validity.rangeOverflow,
89
+ rangeUnderflow: validity.rangeUnderflow,
90
+ stepMismatch: validity.stepMismatch,
91
+ tooLong: validity.tooLong,
92
+ tooShort: validity.tooShort,
93
+ typeMismatch: validity.typeMismatch,
94
+ valueMissing: validity.valueMissing,
95
+ valid: validity.valid
96
+ };
97
+ }
98
+ function $9da8e3f192aba981$var$getNativeValidity(input) {
99
+ return {
100
+ isInvalid: !input.validity.valid,
101
+ validationDetails: $9da8e3f192aba981$var$getValidity(input),
102
+ validationErrors: input.validationMessage ? [
103
+ input.validationMessage
104
+ ] : []
105
+ };
106
+ }
107
+ function $9da8e3f192aba981$var$getFirstInvalidInput(form) {
108
+ for(let i = 0; i < form.elements.length; i++){
109
+ let element = form.elements[i];
110
+ if (!element.validity.valid) return element;
111
+ }
112
+ return null;
113
+ }
114
+
115
+
116
+ //# sourceMappingURL=useFormValidation.main.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"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","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 {RefObject, Validation, ValidationResult} from '@react-types/shared';\nimport {setInteractionModality} from '@react-aria/interactions';\nimport {useEffect} from 'react';\nimport {useEffectEvent, useLayoutEffect} from '@react-aria/utils';\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) {\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) {\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 onReset = useEffectEvent(() => {\n state.resetValidation();\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 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 };\n }, [ref, onInvalid, onChange, onReset, 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) {\n return element;\n }\n }\n\n return null;\n}\n"],"names":[],"version":3,"file":"useFormValidation.main.js.map"}
@@ -0,0 +1,111 @@
1
+ import {setInteractionModality as $9Gacy$setInteractionModality} from "@react-aria/interactions";
2
+ import {useEffect as $9Gacy$useEffect} from "react";
3
+ import {useLayoutEffect as $9Gacy$useLayoutEffect, useEffectEvent as $9Gacy$useEffectEvent} from "@react-aria/utils";
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)) {
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 onReset = (0, $9Gacy$useEffectEvent)(()=>{
32
+ state.resetValidation();
33
+ });
34
+ let onInvalid = (0, $9Gacy$useEffectEvent)((e)=>{
35
+ var _ref_current;
36
+ // Only commit validation if we are not already displaying one.
37
+ // This avoids clearing server errors that the user didn't actually fix.
38
+ if (!state.displayValidation.isInvalid) state.commitValidation();
39
+ // Auto focus the first invalid input in a form, unless the error already had its default prevented.
40
+ let form = ref === null || ref === void 0 ? void 0 : (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.form;
41
+ if (!e.defaultPrevented && ref && form && $e93e671b31057976$var$getFirstInvalidInput(form) === ref.current) {
42
+ var _ref_current1;
43
+ if (focus) focus();
44
+ else (_ref_current1 = ref.current) === null || _ref_current1 === void 0 ? void 0 : _ref_current1.focus();
45
+ // Always show focus ring.
46
+ (0, $9Gacy$setInteractionModality)('keyboard');
47
+ }
48
+ // Prevent default browser error UI from appearing.
49
+ e.preventDefault();
50
+ });
51
+ let onChange = (0, $9Gacy$useEffectEvent)(()=>{
52
+ state.commitValidation();
53
+ });
54
+ (0, $9Gacy$useEffect)(()=>{
55
+ let input = ref === null || ref === void 0 ? void 0 : ref.current;
56
+ if (!input) return;
57
+ let form = input.form;
58
+ input.addEventListener('invalid', onInvalid);
59
+ input.addEventListener('change', onChange);
60
+ form === null || form === void 0 ? void 0 : form.addEventListener('reset', onReset);
61
+ return ()=>{
62
+ input.removeEventListener('invalid', onInvalid);
63
+ input.removeEventListener('change', onChange);
64
+ form === null || form === void 0 ? void 0 : form.removeEventListener('reset', onReset);
65
+ };
66
+ }, [
67
+ ref,
68
+ onInvalid,
69
+ onChange,
70
+ onReset,
71
+ validationBehavior
72
+ ]);
73
+ }
74
+ function $e93e671b31057976$var$getValidity(input) {
75
+ // The native ValidityState object is live, meaning each property is a getter that returns the current state.
76
+ // We need to create a snapshot of the validity state at the time this function is called to avoid unpredictable React renders.
77
+ let validity = input.validity;
78
+ return {
79
+ badInput: validity.badInput,
80
+ customError: validity.customError,
81
+ patternMismatch: validity.patternMismatch,
82
+ rangeOverflow: validity.rangeOverflow,
83
+ rangeUnderflow: validity.rangeUnderflow,
84
+ stepMismatch: validity.stepMismatch,
85
+ tooLong: validity.tooLong,
86
+ tooShort: validity.tooShort,
87
+ typeMismatch: validity.typeMismatch,
88
+ valueMissing: validity.valueMissing,
89
+ valid: validity.valid
90
+ };
91
+ }
92
+ function $e93e671b31057976$var$getNativeValidity(input) {
93
+ return {
94
+ isInvalid: !input.validity.valid,
95
+ validationDetails: $e93e671b31057976$var$getValidity(input),
96
+ validationErrors: input.validationMessage ? [
97
+ input.validationMessage
98
+ ] : []
99
+ };
100
+ }
101
+ function $e93e671b31057976$var$getFirstInvalidInput(form) {
102
+ for(let i = 0; i < form.elements.length; i++){
103
+ let element = form.elements[i];
104
+ if (!element.validity.valid) return element;
105
+ }
106
+ return null;
107
+ }
108
+
109
+
110
+ export {$e93e671b31057976$export$b8473d3665f3a75a as useFormValidation};
111
+ //# sourceMappingURL=useFormValidation.module.js.map
@@ -0,0 +1,111 @@
1
+ import {setInteractionModality as $9Gacy$setInteractionModality} from "@react-aria/interactions";
2
+ import {useEffect as $9Gacy$useEffect} from "react";
3
+ import {useLayoutEffect as $9Gacy$useLayoutEffect, useEffectEvent as $9Gacy$useEffectEvent} from "@react-aria/utils";
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)) {
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 onReset = (0, $9Gacy$useEffectEvent)(()=>{
32
+ state.resetValidation();
33
+ });
34
+ let onInvalid = (0, $9Gacy$useEffectEvent)((e)=>{
35
+ var _ref_current;
36
+ // Only commit validation if we are not already displaying one.
37
+ // This avoids clearing server errors that the user didn't actually fix.
38
+ if (!state.displayValidation.isInvalid) state.commitValidation();
39
+ // Auto focus the first invalid input in a form, unless the error already had its default prevented.
40
+ let form = ref === null || ref === void 0 ? void 0 : (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.form;
41
+ if (!e.defaultPrevented && ref && form && $e93e671b31057976$var$getFirstInvalidInput(form) === ref.current) {
42
+ var _ref_current1;
43
+ if (focus) focus();
44
+ else (_ref_current1 = ref.current) === null || _ref_current1 === void 0 ? void 0 : _ref_current1.focus();
45
+ // Always show focus ring.
46
+ (0, $9Gacy$setInteractionModality)('keyboard');
47
+ }
48
+ // Prevent default browser error UI from appearing.
49
+ e.preventDefault();
50
+ });
51
+ let onChange = (0, $9Gacy$useEffectEvent)(()=>{
52
+ state.commitValidation();
53
+ });
54
+ (0, $9Gacy$useEffect)(()=>{
55
+ let input = ref === null || ref === void 0 ? void 0 : ref.current;
56
+ if (!input) return;
57
+ let form = input.form;
58
+ input.addEventListener('invalid', onInvalid);
59
+ input.addEventListener('change', onChange);
60
+ form === null || form === void 0 ? void 0 : form.addEventListener('reset', onReset);
61
+ return ()=>{
62
+ input.removeEventListener('invalid', onInvalid);
63
+ input.removeEventListener('change', onChange);
64
+ form === null || form === void 0 ? void 0 : form.removeEventListener('reset', onReset);
65
+ };
66
+ }, [
67
+ ref,
68
+ onInvalid,
69
+ onChange,
70
+ onReset,
71
+ validationBehavior
72
+ ]);
73
+ }
74
+ function $e93e671b31057976$var$getValidity(input) {
75
+ // The native ValidityState object is live, meaning each property is a getter that returns the current state.
76
+ // We need to create a snapshot of the validity state at the time this function is called to avoid unpredictable React renders.
77
+ let validity = input.validity;
78
+ return {
79
+ badInput: validity.badInput,
80
+ customError: validity.customError,
81
+ patternMismatch: validity.patternMismatch,
82
+ rangeOverflow: validity.rangeOverflow,
83
+ rangeUnderflow: validity.rangeUnderflow,
84
+ stepMismatch: validity.stepMismatch,
85
+ tooLong: validity.tooLong,
86
+ tooShort: validity.tooShort,
87
+ typeMismatch: validity.typeMismatch,
88
+ valueMissing: validity.valueMissing,
89
+ valid: validity.valid
90
+ };
91
+ }
92
+ function $e93e671b31057976$var$getNativeValidity(input) {
93
+ return {
94
+ isInvalid: !input.validity.valid,
95
+ validationDetails: $e93e671b31057976$var$getValidity(input),
96
+ validationErrors: input.validationMessage ? [
97
+ input.validationMessage
98
+ ] : []
99
+ };
100
+ }
101
+ function $e93e671b31057976$var$getFirstInvalidInput(form) {
102
+ for(let i = 0; i < form.elements.length; i++){
103
+ let element = form.elements[i];
104
+ if (!element.validity.valid) return element;
105
+ }
106
+ return null;
107
+ }
108
+
109
+
110
+ export {$e93e671b31057976$export$b8473d3665f3a75a as useFormValidation};
111
+ //# sourceMappingURL=useFormValidation.module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"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","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 {RefObject, Validation, ValidationResult} from '@react-types/shared';\nimport {setInteractionModality} from '@react-aria/interactions';\nimport {useEffect} from 'react';\nimport {useEffectEvent, useLayoutEffect} from '@react-aria/utils';\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) {\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) {\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 onReset = useEffectEvent(() => {\n state.resetValidation();\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 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 };\n }, [ref, onInvalid, onChange, onReset, 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) {\n return element;\n }\n }\n\n return null;\n}\n"],"names":[],"version":3,"file":"useFormValidation.module.js.map"}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@react-aria/form",
3
+ "version": "3.0.0-nightly-641446f65-240905",
4
+ "description": "Spectrum UI components in React",
5
+ "license": "Apache-2.0",
6
+ "main": "dist/main.js",
7
+ "module": "dist/module.js",
8
+ "types": "dist/types.d.ts",
9
+ "exports": {
10
+ "types": "./dist/types.d.ts",
11
+ "import": "./dist/import.mjs",
12
+ "require": "./dist/main.js"
13
+ },
14
+ "source": "src/index.ts",
15
+ "files": [
16
+ "dist",
17
+ "src"
18
+ ],
19
+ "sideEffects": false,
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/adobe/react-spectrum"
23
+ },
24
+ "dependencies": {
25
+ "@react-aria/interactions": "^3.0.0-nightly-641446f65-240905",
26
+ "@react-aria/utils": "^3.0.0-nightly-641446f65-240905",
27
+ "@react-stately/form": "^3.0.0-nightly-641446f65-240905",
28
+ "@react-types/shared": "^3.0.0-nightly-641446f65-240905",
29
+ "@swc/helpers": "^0.5.0"
30
+ },
31
+ "peerDependencies": {
32
+ "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "stableVersion": "3.0.8"
38
+ }
package/src/index.ts ADDED
@@ -0,0 +1,13 @@
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
+ export {useFormValidation} from './useFormValidation';
@@ -0,0 +1,132 @@
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 {RefObject, Validation, ValidationResult} from '@react-types/shared';
15
+ import {setInteractionModality} from '@react-aria/interactions';
16
+ import {useEffect} from 'react';
17
+ import {useEffectEvent, useLayoutEffect} from '@react-aria/utils';
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) {
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) {
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 onReset = useEffectEvent(() => {
47
+ state.resetValidation();
48
+ });
49
+
50
+ let onInvalid = useEffectEvent((e: Event) => {
51
+ // Only commit validation if we are not already displaying one.
52
+ // This avoids clearing server errors that the user didn't actually fix.
53
+ if (!state.displayValidation.isInvalid) {
54
+ state.commitValidation();
55
+ }
56
+
57
+ // Auto focus the first invalid input in a form, unless the error already had its default prevented.
58
+ let form = ref?.current?.form;
59
+ if (!e.defaultPrevented && ref && form && getFirstInvalidInput(form) === ref.current) {
60
+ if (focus) {
61
+ focus();
62
+ } else {
63
+ ref.current?.focus();
64
+ }
65
+
66
+ // Always show focus ring.
67
+ setInteractionModality('keyboard');
68
+ }
69
+
70
+ // Prevent default browser error UI from appearing.
71
+ e.preventDefault();
72
+ });
73
+
74
+ let onChange = useEffectEvent(() => {
75
+ state.commitValidation();
76
+ });
77
+
78
+ useEffect(() => {
79
+ let input = ref?.current;
80
+ if (!input) {
81
+ return;
82
+ }
83
+
84
+ let form = input.form;
85
+ input.addEventListener('invalid', onInvalid);
86
+ input.addEventListener('change', onChange);
87
+ form?.addEventListener('reset', onReset);
88
+ return () => {
89
+ input!.removeEventListener('invalid', onInvalid);
90
+ input!.removeEventListener('change', onChange);
91
+ form?.removeEventListener('reset', onReset);
92
+ };
93
+ }, [ref, onInvalid, onChange, onReset, validationBehavior]);
94
+ }
95
+
96
+ function getValidity(input: ValidatableElement) {
97
+ // The native ValidityState object is live, meaning each property is a getter that returns the current state.
98
+ // We need to create a snapshot of the validity state at the time this function is called to avoid unpredictable React renders.
99
+ let validity = input.validity;
100
+ return {
101
+ badInput: validity.badInput,
102
+ customError: validity.customError,
103
+ patternMismatch: validity.patternMismatch,
104
+ rangeOverflow: validity.rangeOverflow,
105
+ rangeUnderflow: validity.rangeUnderflow,
106
+ stepMismatch: validity.stepMismatch,
107
+ tooLong: validity.tooLong,
108
+ tooShort: validity.tooShort,
109
+ typeMismatch: validity.typeMismatch,
110
+ valueMissing: validity.valueMissing,
111
+ valid: validity.valid
112
+ };
113
+ }
114
+
115
+ function getNativeValidity(input: ValidatableElement): ValidationResult {
116
+ return {
117
+ isInvalid: !input.validity.valid,
118
+ validationDetails: getValidity(input),
119
+ validationErrors: input.validationMessage ? [input.validationMessage] : []
120
+ };
121
+ }
122
+
123
+ function getFirstInvalidInput(form: HTMLFormElement): ValidatableElement | null {
124
+ for (let i = 0; i < form.elements.length; i++) {
125
+ let element = form.elements[i] as ValidatableElement;
126
+ if (!element.validity.valid) {
127
+ return element;
128
+ }
129
+ }
130
+
131
+ return null;
132
+ }