@react-stately/form 3.2.4 → 3.3.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 CHANGED
@@ -1,4 +1,4 @@
1
- import {DEFAULT_VALIDATION_RESULT as $e5be200c675c3b3a$export$dad6ae84456c676a, FormValidationContext as $e5be200c675c3b3a$export$571b5131b7e65c11, mergeValidation as $e5be200c675c3b3a$export$75ee7c75d68f5b0e, privateValidationStateProp as $e5be200c675c3b3a$export$a763b9476acd3eb, useFormValidationState as $e5be200c675c3b3a$export$fc1a364ae1f3ff10, VALID_VALIDITY_STATE as $e5be200c675c3b3a$export$aca958c65c314e6c} from "./useFormValidationState.mjs";
1
+ import {FormValidationContext as $bbcb1671647795c4$re_export$FormValidationContext, useFormValidationState as $bbcb1671647795c4$re_export$useFormValidationState, DEFAULT_VALIDATION_RESULT as $bbcb1671647795c4$re_export$DEFAULT_VALIDATION_RESULT, VALID_VALIDITY_STATE as $bbcb1671647795c4$re_export$VALID_VALIDITY_STATE, privateValidationStateProp as $bbcb1671647795c4$re_export$privateValidationStateProp, mergeValidation as $bbcb1671647795c4$re_export$mergeValidation} from "react-stately/private/form/useFormValidationState";
2
2
 
3
3
  /*
4
4
  * Copyright 2023 Adobe. All rights reserved.
@@ -13,5 +13,5 @@ import {DEFAULT_VALIDATION_RESULT as $e5be200c675c3b3a$export$dad6ae84456c676a,
13
13
  */
14
14
 
15
15
 
16
- export {$e5be200c675c3b3a$export$571b5131b7e65c11 as FormValidationContext, $e5be200c675c3b3a$export$fc1a364ae1f3ff10 as useFormValidationState, $e5be200c675c3b3a$export$dad6ae84456c676a as DEFAULT_VALIDATION_RESULT, $e5be200c675c3b3a$export$aca958c65c314e6c as VALID_VALIDITY_STATE, $e5be200c675c3b3a$export$a763b9476acd3eb as privateValidationStateProp, $e5be200c675c3b3a$export$75ee7c75d68f5b0e as mergeValidation};
16
+ export {$bbcb1671647795c4$re_export$FormValidationContext as FormValidationContext, $bbcb1671647795c4$re_export$useFormValidationState as useFormValidationState, $bbcb1671647795c4$re_export$DEFAULT_VALIDATION_RESULT as DEFAULT_VALIDATION_RESULT, $bbcb1671647795c4$re_export$VALID_VALIDITY_STATE as VALID_VALIDITY_STATE, $bbcb1671647795c4$re_export$privateValidationStateProp as privateValidationStateProp, $bbcb1671647795c4$re_export$mergeValidation as mergeValidation};
17
17
  //# sourceMappingURL=module.js.map
package/dist/main.js CHANGED
@@ -1,16 +1,16 @@
1
- var $5d10129a174740d4$exports = require("./useFormValidationState.main.js");
1
+ var $i9NFl$reactstatelyprivateformuseFormValidationState = require("react-stately/private/form/useFormValidationState");
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, "FormValidationContext", () => $5d10129a174740d4$exports.FormValidationContext);
9
- $parcel$export(module.exports, "useFormValidationState", () => $5d10129a174740d4$exports.useFormValidationState);
10
- $parcel$export(module.exports, "DEFAULT_VALIDATION_RESULT", () => $5d10129a174740d4$exports.DEFAULT_VALIDATION_RESULT);
11
- $parcel$export(module.exports, "VALID_VALIDITY_STATE", () => $5d10129a174740d4$exports.VALID_VALIDITY_STATE);
12
- $parcel$export(module.exports, "privateValidationStateProp", () => $5d10129a174740d4$exports.privateValidationStateProp);
13
- $parcel$export(module.exports, "mergeValidation", () => $5d10129a174740d4$exports.mergeValidation);
8
+ $parcel$export(module.exports, "FormValidationContext", function () { return $i9NFl$reactstatelyprivateformuseFormValidationState.FormValidationContext; });
9
+ $parcel$export(module.exports, "useFormValidationState", function () { return $i9NFl$reactstatelyprivateformuseFormValidationState.useFormValidationState; });
10
+ $parcel$export(module.exports, "DEFAULT_VALIDATION_RESULT", function () { return $i9NFl$reactstatelyprivateformuseFormValidationState.DEFAULT_VALIDATION_RESULT; });
11
+ $parcel$export(module.exports, "VALID_VALIDITY_STATE", function () { return $i9NFl$reactstatelyprivateformuseFormValidationState.VALID_VALIDITY_STATE; });
12
+ $parcel$export(module.exports, "privateValidationStateProp", function () { return $i9NFl$reactstatelyprivateformuseFormValidationState.privateValidationStateProp; });
13
+ $parcel$export(module.exports, "mergeValidation", function () { return $i9NFl$reactstatelyprivateformuseFormValidationState.mergeValidation; });
14
14
  /*
15
15
  * Copyright 2023 Adobe. All rights reserved.
16
16
  * 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-stately/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 {FormValidationContext, useFormValidationState, DEFAULT_VALIDATION_RESULT, VALID_VALIDITY_STATE, privateValidationStateProp, mergeValidation} from './useFormValidationState';\nexport type {FormValidationState} from './useFormValidationState';\n"],"names":[],"version":3,"file":"main.js.map"}
1
+ {"mappings":";;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/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 {FormValidationContext, useFormValidationState, DEFAULT_VALIDATION_RESULT, VALID_VALIDITY_STATE, privateValidationStateProp, mergeValidation} from 'react-stately/private/form/useFormValidationState';\n\nexport type {FormValidationState} from 'react-stately/private/form/useFormValidationState';\n"],"names":[],"version":3,"file":"main.js.map"}
package/dist/module.js CHANGED
@@ -1,4 +1,4 @@
1
- import {DEFAULT_VALIDATION_RESULT as $e5be200c675c3b3a$export$dad6ae84456c676a, FormValidationContext as $e5be200c675c3b3a$export$571b5131b7e65c11, mergeValidation as $e5be200c675c3b3a$export$75ee7c75d68f5b0e, privateValidationStateProp as $e5be200c675c3b3a$export$a763b9476acd3eb, useFormValidationState as $e5be200c675c3b3a$export$fc1a364ae1f3ff10, VALID_VALIDITY_STATE as $e5be200c675c3b3a$export$aca958c65c314e6c} from "./useFormValidationState.module.js";
1
+ import {FormValidationContext as $bbcb1671647795c4$re_export$FormValidationContext, useFormValidationState as $bbcb1671647795c4$re_export$useFormValidationState, DEFAULT_VALIDATION_RESULT as $bbcb1671647795c4$re_export$DEFAULT_VALIDATION_RESULT, VALID_VALIDITY_STATE as $bbcb1671647795c4$re_export$VALID_VALIDITY_STATE, privateValidationStateProp as $bbcb1671647795c4$re_export$privateValidationStateProp, mergeValidation as $bbcb1671647795c4$re_export$mergeValidation} from "react-stately/private/form/useFormValidationState";
2
2
 
3
3
  /*
4
4
  * Copyright 2023 Adobe. All rights reserved.
@@ -13,5 +13,5 @@ import {DEFAULT_VALIDATION_RESULT as $e5be200c675c3b3a$export$dad6ae84456c676a,
13
13
  */
14
14
 
15
15
 
16
- export {$e5be200c675c3b3a$export$571b5131b7e65c11 as FormValidationContext, $e5be200c675c3b3a$export$fc1a364ae1f3ff10 as useFormValidationState, $e5be200c675c3b3a$export$dad6ae84456c676a as DEFAULT_VALIDATION_RESULT, $e5be200c675c3b3a$export$aca958c65c314e6c as VALID_VALIDITY_STATE, $e5be200c675c3b3a$export$a763b9476acd3eb as privateValidationStateProp, $e5be200c675c3b3a$export$75ee7c75d68f5b0e as mergeValidation};
16
+ export {$bbcb1671647795c4$re_export$FormValidationContext as FormValidationContext, $bbcb1671647795c4$re_export$useFormValidationState as useFormValidationState, $bbcb1671647795c4$re_export$DEFAULT_VALIDATION_RESULT as DEFAULT_VALIDATION_RESULT, $bbcb1671647795c4$re_export$VALID_VALIDITY_STATE as VALID_VALIDITY_STATE, $bbcb1671647795c4$re_export$privateValidationStateProp as privateValidationStateProp, $bbcb1671647795c4$re_export$mergeValidation as mergeValidation};
17
17
  //# sourceMappingURL=module.js.map
@@ -1 +1 @@
1
- {"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/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 {FormValidationContext, useFormValidationState, DEFAULT_VALIDATION_RESULT, VALID_VALIDITY_STATE, privateValidationStateProp, mergeValidation} from './useFormValidationState';\nexport type {FormValidationState} from './useFormValidationState';\n"],"names":[],"version":3,"file":"module.js.map"}
1
+ {"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/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 {FormValidationContext, useFormValidationState, DEFAULT_VALIDATION_RESULT, VALID_VALIDITY_STATE, privateValidationStateProp, mergeValidation} from 'react-stately/private/form/useFormValidationState';\n\nexport type {FormValidationState} from 'react-stately/private/form/useFormValidationState';\n"],"names":[],"version":3,"file":"module.js.map"}
@@ -0,0 +1,2 @@
1
+ export { FormValidationContext, useFormValidationState, DEFAULT_VALIDATION_RESULT, VALID_VALIDITY_STATE, privateValidationStateProp, mergeValidation } from 'react-stately/private/form/useFormValidationState';
2
+ export type { FormValidationState } from 'react-stately/private/form/useFormValidationState';
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@react-stately/form",
3
- "version": "3.2.4",
3
+ "version": "3.3.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
- "source": "./src/index.ts",
11
- "types": [
12
- "./dist/types.d.ts",
13
- "./src/index.ts"
14
- ],
15
- "import": "./dist/import.mjs",
16
- "require": "./dist/main.js"
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,14 +26,18 @@
26
26
  "url": "https://github.com/adobe/react-spectrum"
27
27
  },
28
28
  "dependencies": {
29
- "@react-types/shared": "^3.33.1",
30
- "@swc/helpers": "^0.5.0"
29
+ "@swc/helpers": "^0.5.0",
30
+ "react-stately": "3.46.0"
31
31
  },
32
32
  "peerDependencies": {
33
- "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
33
+ "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1",
34
+ "react-dom": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
34
35
  },
35
36
  "publishConfig": {
36
37
  "access": "public"
37
38
  },
38
- "gitHead": "8df187370053aa35f553cb388ad670f65e1ab371"
39
+ "targets": {
40
+ "types": false
41
+ },
42
+ "gitHead": "a6999bdf494a2e9c0381a5881908328bdd22ddae"
39
43
  }
package/src/index.ts CHANGED
@@ -10,5 +10,6 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export {FormValidationContext, useFormValidationState, DEFAULT_VALIDATION_RESULT, VALID_VALIDITY_STATE, privateValidationStateProp, mergeValidation} from './useFormValidationState';
14
- export type {FormValidationState} from './useFormValidationState';
13
+ export {FormValidationContext, useFormValidationState, DEFAULT_VALIDATION_RESULT, VALID_VALIDITY_STATE, privateValidationStateProp, mergeValidation} from 'react-stately/private/form/useFormValidationState';
14
+
15
+ export type {FormValidationState} from 'react-stately/private/form/useFormValidationState';
package/dist/types.d.ts DELETED
@@ -1,27 +0,0 @@
1
- import { Context } from "react";
2
- import { Validation, ValidationErrors, ValidationResult } from "@react-types/shared";
3
- export const VALID_VALIDITY_STATE: ValidityState;
4
- export const DEFAULT_VALIDATION_RESULT: ValidationResult;
5
- export const FormValidationContext: Context<ValidationErrors>;
6
- export const privateValidationStateProp: string;
7
- interface FormValidationProps<T> extends Validation<T> {
8
- builtinValidation?: ValidationResult;
9
- name?: string | string[];
10
- value: T | null;
11
- }
12
- export interface FormValidationState {
13
- /** Realtime validation results, updated as the user edits the value. */
14
- realtimeValidation: ValidationResult;
15
- /** Currently displayed validation results, updated when the user commits their changes. */
16
- displayValidation: ValidationResult;
17
- /** Updates the current validation result. Not displayed to the user until `commitValidation` is called. */
18
- updateValidation(result: ValidationResult): void;
19
- /** Resets the displayed validation state to valid when the user resets the form. */
20
- resetValidation(): void;
21
- /** Commits the realtime validation so it is displayed to the user. */
22
- commitValidation(): void;
23
- }
24
- export function useFormValidationState<T>(props: FormValidationProps<T>): FormValidationState;
25
- export function mergeValidation(...results: ValidationResult[]): ValidationResult;
26
-
27
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"mappings":";;AAeA,OAAO,MAAM,sBAAsB,aAYlC,CAAC;AAQF,OAAO,MAAM,2BAA2B,gBAIvC,CAAC;AAEF,OAAO,MAAM,uBAAuB,QAAQ,gBAAgB,CAAuC,CAAC;AAEpG,OAAO,MAAM,4BAA4B,MAA6C,CAAC;AAEvF,8BAA8B,CAAC,CAAE,SAAQ,WAAW,CAAC,CAAC;IACpD,iBAAiB,CAAC,EAAE,gBAAgB,CAAC;IACrC,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzB,KAAK,EAAE,CAAC,GAAG,IAAI,CAAA;CAChB;AAED;IACE,wEAAwE;IACxE,kBAAkB,EAAE,gBAAgB,CAAC;IACrC,2FAA2F;IAC3F,iBAAiB,EAAE,gBAAgB,CAAC;IACpC,2GAA2G;IAC3G,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,GAAG,IAAI,CAAC;IACjD,oFAAoF;IACpF,eAAe,IAAI,IAAI,CAAC;IACxB,sEAAsE;IACtE,gBAAgB,IAAI,IAAI,CAAA;CACzB;AAED,uCAAuC,CAAC,EAAE,KAAK,EAAE,oBAAoB,CAAC,CAAC,GAAG,mBAAmB,CAS5F;AA+JD,gCAAgC,GAAG,OAAO,EAAE,gBAAgB,EAAE,GAAG,gBAAgB,CAyBhF","sources":["packages/@react-stately/form/src/packages/@react-stately/form/src/useFormValidationState.ts","packages/@react-stately/form/src/packages/@react-stately/form/src/index.ts","packages/@react-stately/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 {FormValidationContext, useFormValidationState, DEFAULT_VALIDATION_RESULT, VALID_VALIDITY_STATE, privateValidationStateProp, mergeValidation} from './useFormValidationState';\nexport type {FormValidationState} from './useFormValidationState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
@@ -1,202 +0,0 @@
1
- var $kFCAu$react = require("react");
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, "VALID_VALIDITY_STATE", () => $5d10129a174740d4$export$aca958c65c314e6c);
9
- $parcel$export(module.exports, "DEFAULT_VALIDATION_RESULT", () => $5d10129a174740d4$export$dad6ae84456c676a);
10
- $parcel$export(module.exports, "FormValidationContext", () => $5d10129a174740d4$export$571b5131b7e65c11);
11
- $parcel$export(module.exports, "privateValidationStateProp", () => $5d10129a174740d4$export$a763b9476acd3eb);
12
- $parcel$export(module.exports, "useFormValidationState", () => $5d10129a174740d4$export$fc1a364ae1f3ff10);
13
- $parcel$export(module.exports, "mergeValidation", () => $5d10129a174740d4$export$75ee7c75d68f5b0e);
14
- /*
15
- * Copyright 2023 Adobe. All rights reserved.
16
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
17
- * you may not use this file except in compliance with the License. You may obtain a copy
18
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
19
- *
20
- * Unless required by applicable law or agreed to in writing, software distributed under
21
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
22
- * OF ANY KIND, either express or implied. See the License for the specific language
23
- * governing permissions and limitations under the License.
24
- */
25
- const $5d10129a174740d4$export$aca958c65c314e6c = {
26
- badInput: false,
27
- customError: false,
28
- patternMismatch: false,
29
- rangeOverflow: false,
30
- rangeUnderflow: false,
31
- stepMismatch: false,
32
- tooLong: false,
33
- tooShort: false,
34
- typeMismatch: false,
35
- valueMissing: false,
36
- valid: true
37
- };
38
- const $5d10129a174740d4$var$CUSTOM_VALIDITY_STATE = {
39
- ...$5d10129a174740d4$export$aca958c65c314e6c,
40
- customError: true,
41
- valid: false
42
- };
43
- const $5d10129a174740d4$export$dad6ae84456c676a = {
44
- isInvalid: false,
45
- validationDetails: $5d10129a174740d4$export$aca958c65c314e6c,
46
- validationErrors: []
47
- };
48
- const $5d10129a174740d4$export$571b5131b7e65c11 = (0, $kFCAu$react.createContext)({});
49
- const $5d10129a174740d4$export$a763b9476acd3eb = '__formValidationState' + Date.now();
50
- function $5d10129a174740d4$export$fc1a364ae1f3ff10(props) {
51
- // Private prop for parent components to pass state to children.
52
- if (props[$5d10129a174740d4$export$a763b9476acd3eb]) {
53
- let { realtimeValidation: realtimeValidation, displayValidation: displayValidation, updateValidation: updateValidation, resetValidation: resetValidation, commitValidation: commitValidation } = props[$5d10129a174740d4$export$a763b9476acd3eb];
54
- return {
55
- realtimeValidation: realtimeValidation,
56
- displayValidation: displayValidation,
57
- updateValidation: updateValidation,
58
- resetValidation: resetValidation,
59
- commitValidation: commitValidation
60
- };
61
- }
62
- // eslint-disable-next-line react-hooks/rules-of-hooks
63
- return $5d10129a174740d4$var$useFormValidationStateImpl(props);
64
- }
65
- function $5d10129a174740d4$var$useFormValidationStateImpl(props) {
66
- let { isInvalid: isInvalid, validationState: validationState, name: name, value: value, builtinValidation: builtinValidation, validate: validate, validationBehavior: validationBehavior = 'aria' } = props;
67
- // backward compatibility.
68
- if (validationState) isInvalid || (isInvalid = validationState === 'invalid');
69
- // If the isInvalid prop is controlled, update validation result in realtime.
70
- let controlledError = isInvalid !== undefined ? {
71
- isInvalid: isInvalid,
72
- validationErrors: [],
73
- validationDetails: $5d10129a174740d4$var$CUSTOM_VALIDITY_STATE
74
- } : null;
75
- // Perform custom client side validation.
76
- let clientError = (0, $kFCAu$react.useMemo)(()=>{
77
- if (!validate || value == null) return null;
78
- let validateErrors = $5d10129a174740d4$var$runValidate(validate, value);
79
- return $5d10129a174740d4$var$getValidationResult(validateErrors);
80
- }, [
81
- validate,
82
- value
83
- ]);
84
- if (builtinValidation === null || builtinValidation === void 0 ? void 0 : builtinValidation.validationDetails.valid) builtinValidation = undefined;
85
- // Get relevant server errors from the form.
86
- let serverErrors = (0, $kFCAu$react.useContext)($5d10129a174740d4$export$571b5131b7e65c11);
87
- let serverErrorMessages = (0, $kFCAu$react.useMemo)(()=>{
88
- if (name) return Array.isArray(name) ? name.flatMap((name)=>$5d10129a174740d4$var$asArray(serverErrors[name])) : $5d10129a174740d4$var$asArray(serverErrors[name]);
89
- return [];
90
- }, [
91
- serverErrors,
92
- name
93
- ]);
94
- // Show server errors when the form gets a new value, and clear when the user changes the value.
95
- let [lastServerErrors, setLastServerErrors] = (0, $kFCAu$react.useState)(serverErrors);
96
- let [isServerErrorCleared, setServerErrorCleared] = (0, $kFCAu$react.useState)(false);
97
- if (serverErrors !== lastServerErrors) {
98
- setLastServerErrors(serverErrors);
99
- setServerErrorCleared(false);
100
- }
101
- let serverError = (0, $kFCAu$react.useMemo)(()=>$5d10129a174740d4$var$getValidationResult(isServerErrorCleared ? [] : serverErrorMessages), [
102
- isServerErrorCleared,
103
- serverErrorMessages
104
- ]);
105
- // Track the next validation state in a ref until commitValidation is called.
106
- let nextValidation = (0, $kFCAu$react.useRef)($5d10129a174740d4$export$dad6ae84456c676a);
107
- let [currentValidity, setCurrentValidity] = (0, $kFCAu$react.useState)($5d10129a174740d4$export$dad6ae84456c676a);
108
- let lastError = (0, $kFCAu$react.useRef)($5d10129a174740d4$export$dad6ae84456c676a);
109
- let commitValidation = ()=>{
110
- if (!commitQueued) return;
111
- setCommitQueued(false);
112
- let error = clientError || builtinValidation || nextValidation.current;
113
- if (!$5d10129a174740d4$var$isEqualValidation(error, lastError.current)) {
114
- lastError.current = error;
115
- setCurrentValidity(error);
116
- }
117
- };
118
- let [commitQueued, setCommitQueued] = (0, $kFCAu$react.useState)(false);
119
- (0, $kFCAu$react.useEffect)(commitValidation);
120
- // realtimeValidation is used to update the native input element's state based on custom validation logic.
121
- // displayValidation is the currently displayed validation state that the user sees (e.g. on input change/form submit).
122
- // With validationBehavior="aria", all errors are displayed in realtime rather than on submit.
123
- let realtimeValidation = controlledError || serverError || clientError || builtinValidation || $5d10129a174740d4$export$dad6ae84456c676a;
124
- let displayValidation = validationBehavior === 'native' ? controlledError || serverError || currentValidity : controlledError || serverError || clientError || builtinValidation || currentValidity;
125
- return {
126
- realtimeValidation: realtimeValidation,
127
- displayValidation: displayValidation,
128
- updateValidation (value) {
129
- // If validationBehavior is 'aria', update in realtime. Otherwise, store in a ref until commit.
130
- if (validationBehavior === 'aria' && !$5d10129a174740d4$var$isEqualValidation(currentValidity, value)) setCurrentValidity(value);
131
- else nextValidation.current = value;
132
- },
133
- resetValidation () {
134
- // Update the currently displayed validation state to valid on form reset,
135
- // even if the native validity says it isn't. It'll show again on the next form submit.
136
- let error = $5d10129a174740d4$export$dad6ae84456c676a;
137
- if (!$5d10129a174740d4$var$isEqualValidation(error, lastError.current)) {
138
- lastError.current = error;
139
- setCurrentValidity(error);
140
- }
141
- // Do not commit validation after the next render. This avoids a condition where
142
- // useSelect calls commitValidation inside an onReset handler.
143
- if (validationBehavior === 'native') setCommitQueued(false);
144
- setServerErrorCleared(true);
145
- },
146
- commitValidation () {
147
- // Commit validation state so the user sees it on blur/change/submit. Also clear any server errors.
148
- // Wait until after the next render to commit so that the latest value has been validated.
149
- if (validationBehavior === 'native') setCommitQueued(true);
150
- setServerErrorCleared(true);
151
- }
152
- };
153
- }
154
- function $5d10129a174740d4$var$asArray(v) {
155
- if (!v) return [];
156
- return Array.isArray(v) ? v : [
157
- v
158
- ];
159
- }
160
- function $5d10129a174740d4$var$runValidate(validate, value) {
161
- if (typeof validate === 'function') {
162
- let e = validate(value);
163
- if (e && typeof e !== 'boolean') return $5d10129a174740d4$var$asArray(e);
164
- }
165
- return [];
166
- }
167
- function $5d10129a174740d4$var$getValidationResult(errors) {
168
- return errors.length ? {
169
- isInvalid: true,
170
- validationErrors: errors,
171
- validationDetails: $5d10129a174740d4$var$CUSTOM_VALIDITY_STATE
172
- } : null;
173
- }
174
- function $5d10129a174740d4$var$isEqualValidation(a, b) {
175
- if (a === b) return true;
176
- return !!a && !!b && a.isInvalid === b.isInvalid && a.validationErrors.length === b.validationErrors.length && a.validationErrors.every((a, i)=>a === b.validationErrors[i]) && Object.entries(a.validationDetails).every(([k, v])=>b.validationDetails[k] === v);
177
- }
178
- function $5d10129a174740d4$export$75ee7c75d68f5b0e(...results) {
179
- let errors = new Set();
180
- let isInvalid = false;
181
- let validationDetails = {
182
- ...$5d10129a174740d4$export$aca958c65c314e6c
183
- };
184
- for (let v of results){
185
- var _validationDetails, _key;
186
- for (let e of v.validationErrors)errors.add(e);
187
- // Only these properties apply for checkboxes.
188
- isInvalid || (isInvalid = v.isInvalid);
189
- for(let key in validationDetails)(_validationDetails = validationDetails)[_key = key] || (_validationDetails[_key] = v.validationDetails[key]);
190
- }
191
- validationDetails.valid = !isInvalid;
192
- return {
193
- isInvalid: isInvalid,
194
- validationErrors: [
195
- ...errors
196
- ],
197
- validationDetails: validationDetails
198
- };
199
- }
200
-
201
-
202
- //# sourceMappingURL=useFormValidationState.main.js.map
@@ -1 +0,0 @@
1
- {"mappings":";;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;AAKM,MAAM,4CAAsC;IACjD,UAAU;IACV,aAAa;IACb,iBAAiB;IACjB,eAAe;IACf,gBAAgB;IAChB,cAAc;IACd,SAAS;IACT,UAAU;IACV,cAAc;IACd,cAAc;IACd,OAAO;AACT;AAEA,MAAM,8CAAuC;IAC3C,GAAG,yCAAoB;IACvB,aAAa;IACb,OAAO;AACT;AAEO,MAAM,4CAA8C;IACzD,WAAW;IACX,mBAAmB;IACnB,kBAAkB,EAAE;AACtB;AAEO,MAAM,4CAAmD,CAAA,GAAA,0BAAY,EAAoB,CAAC;AAE1F,MAAM,2CAAqC,0BAA0B,KAAK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CAC9B,OAAO,GAAG,CAAC;QAGb,8CAA8C;QAC9C,cAAA,YAAc,EAAE,SAAS;QACzB,IAAK,IAAI,OAAO,kBACd,CAAA,qBAAA,kBAAiB,CAAC,OAAA,IAAI,KAAtB,kBAAiB,CAAC,KAAI,GAAK,EAAE,iBAAiB,CAAC,IAAI;IAEvD;IAEA,kBAAkB,KAAK,GAAG,CAAC;IAC3B,OAAO;mBACL;QACA,kBAAkB;eAAI;SAAO;2BAC7B;IACF;AACF","sources":["packages/@react-stately/form/src/useFormValidationState.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 {Context, createContext, useContext, useEffect, useMemo, useRef, useState} from 'react';\nimport {Validation, ValidationErrors, ValidationFunction, ValidationResult} from '@react-types/shared';\n\nexport const VALID_VALIDITY_STATE: ValidityState = {\n badInput: false,\n customError: false,\n patternMismatch: false,\n rangeOverflow: false,\n rangeUnderflow: false,\n stepMismatch: false,\n tooLong: false,\n tooShort: false,\n typeMismatch: false,\n valueMissing: false,\n valid: true\n};\n\nconst CUSTOM_VALIDITY_STATE: ValidityState = {\n ...VALID_VALIDITY_STATE,\n customError: true,\n valid: false\n};\n\nexport const DEFAULT_VALIDATION_RESULT: ValidationResult = {\n isInvalid: false,\n validationDetails: VALID_VALIDITY_STATE,\n validationErrors: []\n};\n\nexport const FormValidationContext: Context<ValidationErrors> = createContext<ValidationErrors>({});\n\nexport const privateValidationStateProp: string = '__formValidationState' + Date.now();\n\ninterface FormValidationProps<T> extends Validation<T> {\n builtinValidation?: ValidationResult,\n name?: string | string[],\n value: T | null\n}\n\nexport interface FormValidationState {\n /** Realtime validation results, updated as the user edits the value. */\n realtimeValidation: ValidationResult,\n /** Currently displayed validation results, updated when the user commits their changes. */\n displayValidation: ValidationResult,\n /** Updates the current validation result. Not displayed to the user until `commitValidation` is called. */\n updateValidation(result: ValidationResult): void,\n /** Resets the displayed validation state to valid when the user resets the form. */\n resetValidation(): void,\n /** Commits the realtime validation so it is displayed to the user. */\n commitValidation(): void\n}\n\nexport function useFormValidationState<T>(props: FormValidationProps<T>): FormValidationState {\n // Private prop for parent components to pass state to children.\n if (props[privateValidationStateProp]) {\n let {realtimeValidation, displayValidation, updateValidation, resetValidation, commitValidation} = props[privateValidationStateProp] as FormValidationState;\n return {realtimeValidation, displayValidation, updateValidation, resetValidation, commitValidation};\n }\n\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useFormValidationStateImpl(props);\n}\n\nfunction useFormValidationStateImpl<T>(props: FormValidationProps<T>): FormValidationState {\n let {isInvalid, validationState, name, value, builtinValidation, validate, validationBehavior = 'aria'} = props;\n\n // backward compatibility.\n if (validationState) {\n isInvalid ||= validationState === 'invalid';\n }\n\n // If the isInvalid prop is controlled, update validation result in realtime.\n let controlledError: ValidationResult | null = isInvalid !== undefined ? {\n isInvalid,\n validationErrors: [],\n validationDetails: CUSTOM_VALIDITY_STATE\n } : null;\n\n // Perform custom client side validation.\n let clientError: ValidationResult | null = useMemo(() => {\n if (!validate || value == null) {\n return null;\n }\n let validateErrors = runValidate(validate, value);\n return getValidationResult(validateErrors);\n }, [validate, value]);\n\n if (builtinValidation?.validationDetails.valid) {\n builtinValidation = undefined;\n }\n\n // Get relevant server errors from the form.\n let serverErrors = useContext(FormValidationContext);\n let serverErrorMessages = useMemo(() => {\n if (name) {\n return Array.isArray(name) ? name.flatMap(name => asArray(serverErrors[name])) : asArray(serverErrors[name]);\n }\n return [];\n }, [serverErrors, name]);\n\n // Show server errors when the form gets a new value, and clear when the user changes the value.\n let [lastServerErrors, setLastServerErrors] = useState(serverErrors);\n let [isServerErrorCleared, setServerErrorCleared] = useState(false);\n if (serverErrors !== lastServerErrors) {\n setLastServerErrors(serverErrors);\n setServerErrorCleared(false);\n }\n\n let serverError: ValidationResult | null = useMemo(() =>\n getValidationResult(isServerErrorCleared ? [] : serverErrorMessages),\n [isServerErrorCleared, serverErrorMessages]\n );\n\n // Track the next validation state in a ref until commitValidation is called.\n let nextValidation = useRef(DEFAULT_VALIDATION_RESULT);\n let [currentValidity, setCurrentValidity] = useState(DEFAULT_VALIDATION_RESULT);\n\n let lastError = useRef(DEFAULT_VALIDATION_RESULT);\n let commitValidation = () => {\n if (!commitQueued) {\n return;\n }\n\n setCommitQueued(false);\n let error = clientError || builtinValidation || nextValidation.current;\n if (!isEqualValidation(error, lastError.current)) {\n lastError.current = error;\n setCurrentValidity(error);\n }\n };\n\n let [commitQueued, setCommitQueued] = useState(false);\n useEffect(commitValidation);\n\n // realtimeValidation is used to update the native input element's state based on custom validation logic.\n // displayValidation is the currently displayed validation state that the user sees (e.g. on input change/form submit).\n // With validationBehavior=\"aria\", all errors are displayed in realtime rather than on submit.\n let realtimeValidation = controlledError || serverError || clientError || builtinValidation || DEFAULT_VALIDATION_RESULT;\n let displayValidation = validationBehavior === 'native'\n ? controlledError || serverError || currentValidity\n : controlledError || serverError || clientError || builtinValidation || currentValidity;\n\n return {\n realtimeValidation,\n displayValidation,\n updateValidation(value) {\n // If validationBehavior is 'aria', update in realtime. Otherwise, store in a ref until commit.\n if (validationBehavior === 'aria' && !isEqualValidation(currentValidity, value)) {\n setCurrentValidity(value);\n } else {\n nextValidation.current = value;\n }\n },\n resetValidation() {\n // Update the currently displayed validation state to valid on form reset,\n // even if the native validity says it isn't. It'll show again on the next form submit.\n let error = DEFAULT_VALIDATION_RESULT;\n if (!isEqualValidation(error, lastError.current)) {\n lastError.current = error;\n setCurrentValidity(error);\n }\n\n // Do not commit validation after the next render. This avoids a condition where\n // useSelect calls commitValidation inside an onReset handler.\n if (validationBehavior === 'native') {\n setCommitQueued(false);\n }\n\n setServerErrorCleared(true);\n },\n commitValidation() {\n // Commit validation state so the user sees it on blur/change/submit. Also clear any server errors.\n // Wait until after the next render to commit so that the latest value has been validated.\n if (validationBehavior === 'native') {\n setCommitQueued(true);\n }\n setServerErrorCleared(true);\n }\n };\n}\n\nfunction asArray<T>(v: T | T[]): T[] {\n if (!v) {\n return [];\n }\n\n return Array.isArray(v) ? v : [v];\n}\n\nfunction runValidate<T>(validate: ValidationFunction<T>, value: T): string[] {\n if (typeof validate === 'function') {\n let e = validate(value);\n if (e && typeof e !== 'boolean') {\n return asArray(e);\n }\n }\n\n return [];\n}\n\nfunction getValidationResult(errors: string[]): ValidationResult | null {\n return errors.length ? {\n isInvalid: true,\n validationErrors: errors,\n validationDetails: CUSTOM_VALIDITY_STATE\n } : null;\n}\n\nfunction isEqualValidation(a: ValidationResult | null, b: ValidationResult | null): boolean {\n if (a === b) {\n return true;\n }\n\n return !!a && !!b\n && a.isInvalid === b.isInvalid\n && a.validationErrors.length === b.validationErrors.length\n && a.validationErrors.every((a, i) => a === b.validationErrors[i])\n && Object.entries(a.validationDetails).every(([k, v]) => b.validationDetails[k] === v);\n}\n\nexport function mergeValidation(...results: ValidationResult[]): ValidationResult {\n let errors = new Set<string>();\n let isInvalid = false;\n let validationDetails = {\n ...VALID_VALIDITY_STATE\n };\n\n for (let v of results) {\n for (let e of v.validationErrors) {\n errors.add(e);\n }\n\n // Only these properties apply for checkboxes.\n isInvalid ||= v.isInvalid;\n for (let key in validationDetails) {\n validationDetails[key] ||= v.validationDetails[key];\n }\n }\n\n validationDetails.valid = !isInvalid;\n return {\n isInvalid,\n validationErrors: [...errors],\n validationDetails\n };\n}\n"],"names":[],"version":3,"file":"useFormValidationState.main.js.map"}
@@ -1,192 +0,0 @@
1
- import {createContext as $69F46$createContext, useMemo as $69F46$useMemo, useContext as $69F46$useContext, useState as $69F46$useState, useRef as $69F46$useRef, useEffect as $69F46$useEffect} from "react";
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
- const $e5be200c675c3b3a$export$aca958c65c314e6c = {
15
- badInput: false,
16
- customError: false,
17
- patternMismatch: false,
18
- rangeOverflow: false,
19
- rangeUnderflow: false,
20
- stepMismatch: false,
21
- tooLong: false,
22
- tooShort: false,
23
- typeMismatch: false,
24
- valueMissing: false,
25
- valid: true
26
- };
27
- const $e5be200c675c3b3a$var$CUSTOM_VALIDITY_STATE = {
28
- ...$e5be200c675c3b3a$export$aca958c65c314e6c,
29
- customError: true,
30
- valid: false
31
- };
32
- const $e5be200c675c3b3a$export$dad6ae84456c676a = {
33
- isInvalid: false,
34
- validationDetails: $e5be200c675c3b3a$export$aca958c65c314e6c,
35
- validationErrors: []
36
- };
37
- const $e5be200c675c3b3a$export$571b5131b7e65c11 = (0, $69F46$createContext)({});
38
- const $e5be200c675c3b3a$export$a763b9476acd3eb = '__formValidationState' + Date.now();
39
- function $e5be200c675c3b3a$export$fc1a364ae1f3ff10(props) {
40
- // Private prop for parent components to pass state to children.
41
- if (props[$e5be200c675c3b3a$export$a763b9476acd3eb]) {
42
- let { realtimeValidation: realtimeValidation, displayValidation: displayValidation, updateValidation: updateValidation, resetValidation: resetValidation, commitValidation: commitValidation } = props[$e5be200c675c3b3a$export$a763b9476acd3eb];
43
- return {
44
- realtimeValidation: realtimeValidation,
45
- displayValidation: displayValidation,
46
- updateValidation: updateValidation,
47
- resetValidation: resetValidation,
48
- commitValidation: commitValidation
49
- };
50
- }
51
- // eslint-disable-next-line react-hooks/rules-of-hooks
52
- return $e5be200c675c3b3a$var$useFormValidationStateImpl(props);
53
- }
54
- function $e5be200c675c3b3a$var$useFormValidationStateImpl(props) {
55
- let { isInvalid: isInvalid, validationState: validationState, name: name, value: value, builtinValidation: builtinValidation, validate: validate, validationBehavior: validationBehavior = 'aria' } = props;
56
- // backward compatibility.
57
- if (validationState) isInvalid || (isInvalid = validationState === 'invalid');
58
- // If the isInvalid prop is controlled, update validation result in realtime.
59
- let controlledError = isInvalid !== undefined ? {
60
- isInvalid: isInvalid,
61
- validationErrors: [],
62
- validationDetails: $e5be200c675c3b3a$var$CUSTOM_VALIDITY_STATE
63
- } : null;
64
- // Perform custom client side validation.
65
- let clientError = (0, $69F46$useMemo)(()=>{
66
- if (!validate || value == null) return null;
67
- let validateErrors = $e5be200c675c3b3a$var$runValidate(validate, value);
68
- return $e5be200c675c3b3a$var$getValidationResult(validateErrors);
69
- }, [
70
- validate,
71
- value
72
- ]);
73
- if (builtinValidation === null || builtinValidation === void 0 ? void 0 : builtinValidation.validationDetails.valid) builtinValidation = undefined;
74
- // Get relevant server errors from the form.
75
- let serverErrors = (0, $69F46$useContext)($e5be200c675c3b3a$export$571b5131b7e65c11);
76
- let serverErrorMessages = (0, $69F46$useMemo)(()=>{
77
- if (name) return Array.isArray(name) ? name.flatMap((name)=>$e5be200c675c3b3a$var$asArray(serverErrors[name])) : $e5be200c675c3b3a$var$asArray(serverErrors[name]);
78
- return [];
79
- }, [
80
- serverErrors,
81
- name
82
- ]);
83
- // Show server errors when the form gets a new value, and clear when the user changes the value.
84
- let [lastServerErrors, setLastServerErrors] = (0, $69F46$useState)(serverErrors);
85
- let [isServerErrorCleared, setServerErrorCleared] = (0, $69F46$useState)(false);
86
- if (serverErrors !== lastServerErrors) {
87
- setLastServerErrors(serverErrors);
88
- setServerErrorCleared(false);
89
- }
90
- let serverError = (0, $69F46$useMemo)(()=>$e5be200c675c3b3a$var$getValidationResult(isServerErrorCleared ? [] : serverErrorMessages), [
91
- isServerErrorCleared,
92
- serverErrorMessages
93
- ]);
94
- // Track the next validation state in a ref until commitValidation is called.
95
- let nextValidation = (0, $69F46$useRef)($e5be200c675c3b3a$export$dad6ae84456c676a);
96
- let [currentValidity, setCurrentValidity] = (0, $69F46$useState)($e5be200c675c3b3a$export$dad6ae84456c676a);
97
- let lastError = (0, $69F46$useRef)($e5be200c675c3b3a$export$dad6ae84456c676a);
98
- let commitValidation = ()=>{
99
- if (!commitQueued) return;
100
- setCommitQueued(false);
101
- let error = clientError || builtinValidation || nextValidation.current;
102
- if (!$e5be200c675c3b3a$var$isEqualValidation(error, lastError.current)) {
103
- lastError.current = error;
104
- setCurrentValidity(error);
105
- }
106
- };
107
- let [commitQueued, setCommitQueued] = (0, $69F46$useState)(false);
108
- (0, $69F46$useEffect)(commitValidation);
109
- // realtimeValidation is used to update the native input element's state based on custom validation logic.
110
- // displayValidation is the currently displayed validation state that the user sees (e.g. on input change/form submit).
111
- // With validationBehavior="aria", all errors are displayed in realtime rather than on submit.
112
- let realtimeValidation = controlledError || serverError || clientError || builtinValidation || $e5be200c675c3b3a$export$dad6ae84456c676a;
113
- let displayValidation = validationBehavior === 'native' ? controlledError || serverError || currentValidity : controlledError || serverError || clientError || builtinValidation || currentValidity;
114
- return {
115
- realtimeValidation: realtimeValidation,
116
- displayValidation: displayValidation,
117
- updateValidation (value) {
118
- // If validationBehavior is 'aria', update in realtime. Otherwise, store in a ref until commit.
119
- if (validationBehavior === 'aria' && !$e5be200c675c3b3a$var$isEqualValidation(currentValidity, value)) setCurrentValidity(value);
120
- else nextValidation.current = value;
121
- },
122
- resetValidation () {
123
- // Update the currently displayed validation state to valid on form reset,
124
- // even if the native validity says it isn't. It'll show again on the next form submit.
125
- let error = $e5be200c675c3b3a$export$dad6ae84456c676a;
126
- if (!$e5be200c675c3b3a$var$isEqualValidation(error, lastError.current)) {
127
- lastError.current = error;
128
- setCurrentValidity(error);
129
- }
130
- // Do not commit validation after the next render. This avoids a condition where
131
- // useSelect calls commitValidation inside an onReset handler.
132
- if (validationBehavior === 'native') setCommitQueued(false);
133
- setServerErrorCleared(true);
134
- },
135
- commitValidation () {
136
- // Commit validation state so the user sees it on blur/change/submit. Also clear any server errors.
137
- // Wait until after the next render to commit so that the latest value has been validated.
138
- if (validationBehavior === 'native') setCommitQueued(true);
139
- setServerErrorCleared(true);
140
- }
141
- };
142
- }
143
- function $e5be200c675c3b3a$var$asArray(v) {
144
- if (!v) return [];
145
- return Array.isArray(v) ? v : [
146
- v
147
- ];
148
- }
149
- function $e5be200c675c3b3a$var$runValidate(validate, value) {
150
- if (typeof validate === 'function') {
151
- let e = validate(value);
152
- if (e && typeof e !== 'boolean') return $e5be200c675c3b3a$var$asArray(e);
153
- }
154
- return [];
155
- }
156
- function $e5be200c675c3b3a$var$getValidationResult(errors) {
157
- return errors.length ? {
158
- isInvalid: true,
159
- validationErrors: errors,
160
- validationDetails: $e5be200c675c3b3a$var$CUSTOM_VALIDITY_STATE
161
- } : null;
162
- }
163
- function $e5be200c675c3b3a$var$isEqualValidation(a, b) {
164
- if (a === b) return true;
165
- return !!a && !!b && a.isInvalid === b.isInvalid && a.validationErrors.length === b.validationErrors.length && a.validationErrors.every((a, i)=>a === b.validationErrors[i]) && Object.entries(a.validationDetails).every(([k, v])=>b.validationDetails[k] === v);
166
- }
167
- function $e5be200c675c3b3a$export$75ee7c75d68f5b0e(...results) {
168
- let errors = new Set();
169
- let isInvalid = false;
170
- let validationDetails = {
171
- ...$e5be200c675c3b3a$export$aca958c65c314e6c
172
- };
173
- for (let v of results){
174
- var _validationDetails, _key;
175
- for (let e of v.validationErrors)errors.add(e);
176
- // Only these properties apply for checkboxes.
177
- isInvalid || (isInvalid = v.isInvalid);
178
- for(let key in validationDetails)(_validationDetails = validationDetails)[_key = key] || (_validationDetails[_key] = v.validationDetails[key]);
179
- }
180
- validationDetails.valid = !isInvalid;
181
- return {
182
- isInvalid: isInvalid,
183
- validationErrors: [
184
- ...errors
185
- ],
186
- validationDetails: validationDetails
187
- };
188
- }
189
-
190
-
191
- export {$e5be200c675c3b3a$export$aca958c65c314e6c as VALID_VALIDITY_STATE, $e5be200c675c3b3a$export$dad6ae84456c676a as DEFAULT_VALIDATION_RESULT, $e5be200c675c3b3a$export$571b5131b7e65c11 as FormValidationContext, $e5be200c675c3b3a$export$a763b9476acd3eb as privateValidationStateProp, $e5be200c675c3b3a$export$fc1a364ae1f3ff10 as useFormValidationState, $e5be200c675c3b3a$export$75ee7c75d68f5b0e as mergeValidation};
192
- //# sourceMappingURL=useFormValidationState.module.js.map
@@ -1,192 +0,0 @@
1
- import {createContext as $69F46$createContext, useMemo as $69F46$useMemo, useContext as $69F46$useContext, useState as $69F46$useState, useRef as $69F46$useRef, useEffect as $69F46$useEffect} from "react";
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
- const $e5be200c675c3b3a$export$aca958c65c314e6c = {
15
- badInput: false,
16
- customError: false,
17
- patternMismatch: false,
18
- rangeOverflow: false,
19
- rangeUnderflow: false,
20
- stepMismatch: false,
21
- tooLong: false,
22
- tooShort: false,
23
- typeMismatch: false,
24
- valueMissing: false,
25
- valid: true
26
- };
27
- const $e5be200c675c3b3a$var$CUSTOM_VALIDITY_STATE = {
28
- ...$e5be200c675c3b3a$export$aca958c65c314e6c,
29
- customError: true,
30
- valid: false
31
- };
32
- const $e5be200c675c3b3a$export$dad6ae84456c676a = {
33
- isInvalid: false,
34
- validationDetails: $e5be200c675c3b3a$export$aca958c65c314e6c,
35
- validationErrors: []
36
- };
37
- const $e5be200c675c3b3a$export$571b5131b7e65c11 = (0, $69F46$createContext)({});
38
- const $e5be200c675c3b3a$export$a763b9476acd3eb = '__formValidationState' + Date.now();
39
- function $e5be200c675c3b3a$export$fc1a364ae1f3ff10(props) {
40
- // Private prop for parent components to pass state to children.
41
- if (props[$e5be200c675c3b3a$export$a763b9476acd3eb]) {
42
- let { realtimeValidation: realtimeValidation, displayValidation: displayValidation, updateValidation: updateValidation, resetValidation: resetValidation, commitValidation: commitValidation } = props[$e5be200c675c3b3a$export$a763b9476acd3eb];
43
- return {
44
- realtimeValidation: realtimeValidation,
45
- displayValidation: displayValidation,
46
- updateValidation: updateValidation,
47
- resetValidation: resetValidation,
48
- commitValidation: commitValidation
49
- };
50
- }
51
- // eslint-disable-next-line react-hooks/rules-of-hooks
52
- return $e5be200c675c3b3a$var$useFormValidationStateImpl(props);
53
- }
54
- function $e5be200c675c3b3a$var$useFormValidationStateImpl(props) {
55
- let { isInvalid: isInvalid, validationState: validationState, name: name, value: value, builtinValidation: builtinValidation, validate: validate, validationBehavior: validationBehavior = 'aria' } = props;
56
- // backward compatibility.
57
- if (validationState) isInvalid || (isInvalid = validationState === 'invalid');
58
- // If the isInvalid prop is controlled, update validation result in realtime.
59
- let controlledError = isInvalid !== undefined ? {
60
- isInvalid: isInvalid,
61
- validationErrors: [],
62
- validationDetails: $e5be200c675c3b3a$var$CUSTOM_VALIDITY_STATE
63
- } : null;
64
- // Perform custom client side validation.
65
- let clientError = (0, $69F46$useMemo)(()=>{
66
- if (!validate || value == null) return null;
67
- let validateErrors = $e5be200c675c3b3a$var$runValidate(validate, value);
68
- return $e5be200c675c3b3a$var$getValidationResult(validateErrors);
69
- }, [
70
- validate,
71
- value
72
- ]);
73
- if (builtinValidation === null || builtinValidation === void 0 ? void 0 : builtinValidation.validationDetails.valid) builtinValidation = undefined;
74
- // Get relevant server errors from the form.
75
- let serverErrors = (0, $69F46$useContext)($e5be200c675c3b3a$export$571b5131b7e65c11);
76
- let serverErrorMessages = (0, $69F46$useMemo)(()=>{
77
- if (name) return Array.isArray(name) ? name.flatMap((name)=>$e5be200c675c3b3a$var$asArray(serverErrors[name])) : $e5be200c675c3b3a$var$asArray(serverErrors[name]);
78
- return [];
79
- }, [
80
- serverErrors,
81
- name
82
- ]);
83
- // Show server errors when the form gets a new value, and clear when the user changes the value.
84
- let [lastServerErrors, setLastServerErrors] = (0, $69F46$useState)(serverErrors);
85
- let [isServerErrorCleared, setServerErrorCleared] = (0, $69F46$useState)(false);
86
- if (serverErrors !== lastServerErrors) {
87
- setLastServerErrors(serverErrors);
88
- setServerErrorCleared(false);
89
- }
90
- let serverError = (0, $69F46$useMemo)(()=>$e5be200c675c3b3a$var$getValidationResult(isServerErrorCleared ? [] : serverErrorMessages), [
91
- isServerErrorCleared,
92
- serverErrorMessages
93
- ]);
94
- // Track the next validation state in a ref until commitValidation is called.
95
- let nextValidation = (0, $69F46$useRef)($e5be200c675c3b3a$export$dad6ae84456c676a);
96
- let [currentValidity, setCurrentValidity] = (0, $69F46$useState)($e5be200c675c3b3a$export$dad6ae84456c676a);
97
- let lastError = (0, $69F46$useRef)($e5be200c675c3b3a$export$dad6ae84456c676a);
98
- let commitValidation = ()=>{
99
- if (!commitQueued) return;
100
- setCommitQueued(false);
101
- let error = clientError || builtinValidation || nextValidation.current;
102
- if (!$e5be200c675c3b3a$var$isEqualValidation(error, lastError.current)) {
103
- lastError.current = error;
104
- setCurrentValidity(error);
105
- }
106
- };
107
- let [commitQueued, setCommitQueued] = (0, $69F46$useState)(false);
108
- (0, $69F46$useEffect)(commitValidation);
109
- // realtimeValidation is used to update the native input element's state based on custom validation logic.
110
- // displayValidation is the currently displayed validation state that the user sees (e.g. on input change/form submit).
111
- // With validationBehavior="aria", all errors are displayed in realtime rather than on submit.
112
- let realtimeValidation = controlledError || serverError || clientError || builtinValidation || $e5be200c675c3b3a$export$dad6ae84456c676a;
113
- let displayValidation = validationBehavior === 'native' ? controlledError || serverError || currentValidity : controlledError || serverError || clientError || builtinValidation || currentValidity;
114
- return {
115
- realtimeValidation: realtimeValidation,
116
- displayValidation: displayValidation,
117
- updateValidation (value) {
118
- // If validationBehavior is 'aria', update in realtime. Otherwise, store in a ref until commit.
119
- if (validationBehavior === 'aria' && !$e5be200c675c3b3a$var$isEqualValidation(currentValidity, value)) setCurrentValidity(value);
120
- else nextValidation.current = value;
121
- },
122
- resetValidation () {
123
- // Update the currently displayed validation state to valid on form reset,
124
- // even if the native validity says it isn't. It'll show again on the next form submit.
125
- let error = $e5be200c675c3b3a$export$dad6ae84456c676a;
126
- if (!$e5be200c675c3b3a$var$isEqualValidation(error, lastError.current)) {
127
- lastError.current = error;
128
- setCurrentValidity(error);
129
- }
130
- // Do not commit validation after the next render. This avoids a condition where
131
- // useSelect calls commitValidation inside an onReset handler.
132
- if (validationBehavior === 'native') setCommitQueued(false);
133
- setServerErrorCleared(true);
134
- },
135
- commitValidation () {
136
- // Commit validation state so the user sees it on blur/change/submit. Also clear any server errors.
137
- // Wait until after the next render to commit so that the latest value has been validated.
138
- if (validationBehavior === 'native') setCommitQueued(true);
139
- setServerErrorCleared(true);
140
- }
141
- };
142
- }
143
- function $e5be200c675c3b3a$var$asArray(v) {
144
- if (!v) return [];
145
- return Array.isArray(v) ? v : [
146
- v
147
- ];
148
- }
149
- function $e5be200c675c3b3a$var$runValidate(validate, value) {
150
- if (typeof validate === 'function') {
151
- let e = validate(value);
152
- if (e && typeof e !== 'boolean') return $e5be200c675c3b3a$var$asArray(e);
153
- }
154
- return [];
155
- }
156
- function $e5be200c675c3b3a$var$getValidationResult(errors) {
157
- return errors.length ? {
158
- isInvalid: true,
159
- validationErrors: errors,
160
- validationDetails: $e5be200c675c3b3a$var$CUSTOM_VALIDITY_STATE
161
- } : null;
162
- }
163
- function $e5be200c675c3b3a$var$isEqualValidation(a, b) {
164
- if (a === b) return true;
165
- return !!a && !!b && a.isInvalid === b.isInvalid && a.validationErrors.length === b.validationErrors.length && a.validationErrors.every((a, i)=>a === b.validationErrors[i]) && Object.entries(a.validationDetails).every(([k, v])=>b.validationDetails[k] === v);
166
- }
167
- function $e5be200c675c3b3a$export$75ee7c75d68f5b0e(...results) {
168
- let errors = new Set();
169
- let isInvalid = false;
170
- let validationDetails = {
171
- ...$e5be200c675c3b3a$export$aca958c65c314e6c
172
- };
173
- for (let v of results){
174
- var _validationDetails, _key;
175
- for (let e of v.validationErrors)errors.add(e);
176
- // Only these properties apply for checkboxes.
177
- isInvalid || (isInvalid = v.isInvalid);
178
- for(let key in validationDetails)(_validationDetails = validationDetails)[_key = key] || (_validationDetails[_key] = v.validationDetails[key]);
179
- }
180
- validationDetails.valid = !isInvalid;
181
- return {
182
- isInvalid: isInvalid,
183
- validationErrors: [
184
- ...errors
185
- ],
186
- validationDetails: validationDetails
187
- };
188
- }
189
-
190
-
191
- export {$e5be200c675c3b3a$export$aca958c65c314e6c as VALID_VALIDITY_STATE, $e5be200c675c3b3a$export$dad6ae84456c676a as DEFAULT_VALIDATION_RESULT, $e5be200c675c3b3a$export$571b5131b7e65c11 as FormValidationContext, $e5be200c675c3b3a$export$a763b9476acd3eb as privateValidationStateProp, $e5be200c675c3b3a$export$fc1a364ae1f3ff10 as useFormValidationState, $e5be200c675c3b3a$export$75ee7c75d68f5b0e as mergeValidation};
192
- //# sourceMappingURL=useFormValidationState.module.js.map
@@ -1 +0,0 @@
1
- {"mappings":";;AAAA;;;;;;;;;;CAUC;AAKM,MAAM,4CAAsC;IACjD,UAAU;IACV,aAAa;IACb,iBAAiB;IACjB,eAAe;IACf,gBAAgB;IAChB,cAAc;IACd,SAAS;IACT,UAAU;IACV,cAAc;IACd,cAAc;IACd,OAAO;AACT;AAEA,MAAM,8CAAuC;IAC3C,GAAG,yCAAoB;IACvB,aAAa;IACb,OAAO;AACT;AAEO,MAAM,4CAA8C;IACzD,WAAW;IACX,mBAAmB;IACnB,kBAAkB,EAAE;AACtB;AAEO,MAAM,4CAAmD,CAAA,GAAA,oBAAY,EAAoB,CAAC;AAE1F,MAAM,2CAAqC,0BAA0B,KAAK,GAAG;AAqB7E,SAAS,0CAA0B,K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C9C,cAAA,YAAc,EAAE,SAAS;QACzB,IAAK,IAAI,OAAO,kBACd,CAAA,qBAAA,kBAAiB,CAAC,OAAA,IAAI,KAAtB,kBAAiB,CAAC,KAAI,GAAK,EAAE,iBAAiB,CAAC,IAAI;IAEvD;IAEA,kBAAkB,KAAK,GAAG,CAAC;IAC3B,OAAO;mBACL;QACA,kBAAkB;eAAI;SAAO;2BAC7B;IACF;AACF","sources":["packages/@react-stately/form/src/useFormValidationState.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 {Context, createContext, useContext, useEffect, useMemo, useRef, useState} from 'react';\nimport {Validation, ValidationErrors, ValidationFunction, ValidationResult} from '@react-types/shared';\n\nexport const VALID_VALIDITY_STATE: ValidityState = {\n badInput: false,\n customError: false,\n patternMismatch: false,\n rangeOverflow: false,\n rangeUnderflow: false,\n stepMismatch: false,\n tooLong: false,\n tooShort: false,\n typeMismatch: false,\n valueMissing: false,\n valid: true\n};\n\nconst CUSTOM_VALIDITY_STATE: ValidityState = {\n ...VALID_VALIDITY_STATE,\n customError: true,\n valid: false\n};\n\nexport const DEFAULT_VALIDATION_RESULT: ValidationResult = {\n isInvalid: false,\n validationDetails: VALID_VALIDITY_STATE,\n validationErrors: []\n};\n\nexport const FormValidationContext: Context<ValidationErrors> = createContext<ValidationErrors>({});\n\nexport const privateValidationStateProp: string = '__formValidationState' + Date.now();\n\ninterface FormValidationProps<T> extends Validation<T> {\n builtinValidation?: ValidationResult,\n name?: string | string[],\n value: T | null\n}\n\nexport interface FormValidationState {\n /** Realtime validation results, updated as the user edits the value. */\n realtimeValidation: ValidationResult,\n /** Currently displayed validation results, updated when the user commits their changes. */\n displayValidation: ValidationResult,\n /** Updates the current validation result. Not displayed to the user until `commitValidation` is called. */\n updateValidation(result: ValidationResult): void,\n /** Resets the displayed validation state to valid when the user resets the form. */\n resetValidation(): void,\n /** Commits the realtime validation so it is displayed to the user. */\n commitValidation(): void\n}\n\nexport function useFormValidationState<T>(props: FormValidationProps<T>): FormValidationState {\n // Private prop for parent components to pass state to children.\n if (props[privateValidationStateProp]) {\n let {realtimeValidation, displayValidation, updateValidation, resetValidation, commitValidation} = props[privateValidationStateProp] as FormValidationState;\n return {realtimeValidation, displayValidation, updateValidation, resetValidation, commitValidation};\n }\n\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useFormValidationStateImpl(props);\n}\n\nfunction useFormValidationStateImpl<T>(props: FormValidationProps<T>): FormValidationState {\n let {isInvalid, validationState, name, value, builtinValidation, validate, validationBehavior = 'aria'} = props;\n\n // backward compatibility.\n if (validationState) {\n isInvalid ||= validationState === 'invalid';\n }\n\n // If the isInvalid prop is controlled, update validation result in realtime.\n let controlledError: ValidationResult | null = isInvalid !== undefined ? {\n isInvalid,\n validationErrors: [],\n validationDetails: CUSTOM_VALIDITY_STATE\n } : null;\n\n // Perform custom client side validation.\n let clientError: ValidationResult | null = useMemo(() => {\n if (!validate || value == null) {\n return null;\n }\n let validateErrors = runValidate(validate, value);\n return getValidationResult(validateErrors);\n }, [validate, value]);\n\n if (builtinValidation?.validationDetails.valid) {\n builtinValidation = undefined;\n }\n\n // Get relevant server errors from the form.\n let serverErrors = useContext(FormValidationContext);\n let serverErrorMessages = useMemo(() => {\n if (name) {\n return Array.isArray(name) ? name.flatMap(name => asArray(serverErrors[name])) : asArray(serverErrors[name]);\n }\n return [];\n }, [serverErrors, name]);\n\n // Show server errors when the form gets a new value, and clear when the user changes the value.\n let [lastServerErrors, setLastServerErrors] = useState(serverErrors);\n let [isServerErrorCleared, setServerErrorCleared] = useState(false);\n if (serverErrors !== lastServerErrors) {\n setLastServerErrors(serverErrors);\n setServerErrorCleared(false);\n }\n\n let serverError: ValidationResult | null = useMemo(() =>\n getValidationResult(isServerErrorCleared ? [] : serverErrorMessages),\n [isServerErrorCleared, serverErrorMessages]\n );\n\n // Track the next validation state in a ref until commitValidation is called.\n let nextValidation = useRef(DEFAULT_VALIDATION_RESULT);\n let [currentValidity, setCurrentValidity] = useState(DEFAULT_VALIDATION_RESULT);\n\n let lastError = useRef(DEFAULT_VALIDATION_RESULT);\n let commitValidation = () => {\n if (!commitQueued) {\n return;\n }\n\n setCommitQueued(false);\n let error = clientError || builtinValidation || nextValidation.current;\n if (!isEqualValidation(error, lastError.current)) {\n lastError.current = error;\n setCurrentValidity(error);\n }\n };\n\n let [commitQueued, setCommitQueued] = useState(false);\n useEffect(commitValidation);\n\n // realtimeValidation is used to update the native input element's state based on custom validation logic.\n // displayValidation is the currently displayed validation state that the user sees (e.g. on input change/form submit).\n // With validationBehavior=\"aria\", all errors are displayed in realtime rather than on submit.\n let realtimeValidation = controlledError || serverError || clientError || builtinValidation || DEFAULT_VALIDATION_RESULT;\n let displayValidation = validationBehavior === 'native'\n ? controlledError || serverError || currentValidity\n : controlledError || serverError || clientError || builtinValidation || currentValidity;\n\n return {\n realtimeValidation,\n displayValidation,\n updateValidation(value) {\n // If validationBehavior is 'aria', update in realtime. Otherwise, store in a ref until commit.\n if (validationBehavior === 'aria' && !isEqualValidation(currentValidity, value)) {\n setCurrentValidity(value);\n } else {\n nextValidation.current = value;\n }\n },\n resetValidation() {\n // Update the currently displayed validation state to valid on form reset,\n // even if the native validity says it isn't. It'll show again on the next form submit.\n let error = DEFAULT_VALIDATION_RESULT;\n if (!isEqualValidation(error, lastError.current)) {\n lastError.current = error;\n setCurrentValidity(error);\n }\n\n // Do not commit validation after the next render. This avoids a condition where\n // useSelect calls commitValidation inside an onReset handler.\n if (validationBehavior === 'native') {\n setCommitQueued(false);\n }\n\n setServerErrorCleared(true);\n },\n commitValidation() {\n // Commit validation state so the user sees it on blur/change/submit. Also clear any server errors.\n // Wait until after the next render to commit so that the latest value has been validated.\n if (validationBehavior === 'native') {\n setCommitQueued(true);\n }\n setServerErrorCleared(true);\n }\n };\n}\n\nfunction asArray<T>(v: T | T[]): T[] {\n if (!v) {\n return [];\n }\n\n return Array.isArray(v) ? v : [v];\n}\n\nfunction runValidate<T>(validate: ValidationFunction<T>, value: T): string[] {\n if (typeof validate === 'function') {\n let e = validate(value);\n if (e && typeof e !== 'boolean') {\n return asArray(e);\n }\n }\n\n return [];\n}\n\nfunction getValidationResult(errors: string[]): ValidationResult | null {\n return errors.length ? {\n isInvalid: true,\n validationErrors: errors,\n validationDetails: CUSTOM_VALIDITY_STATE\n } : null;\n}\n\nfunction isEqualValidation(a: ValidationResult | null, b: ValidationResult | null): boolean {\n if (a === b) {\n return true;\n }\n\n return !!a && !!b\n && a.isInvalid === b.isInvalid\n && a.validationErrors.length === b.validationErrors.length\n && a.validationErrors.every((a, i) => a === b.validationErrors[i])\n && Object.entries(a.validationDetails).every(([k, v]) => b.validationDetails[k] === v);\n}\n\nexport function mergeValidation(...results: ValidationResult[]): ValidationResult {\n let errors = new Set<string>();\n let isInvalid = false;\n let validationDetails = {\n ...VALID_VALIDITY_STATE\n };\n\n for (let v of results) {\n for (let e of v.validationErrors) {\n errors.add(e);\n }\n\n // Only these properties apply for checkboxes.\n isInvalid ||= v.isInvalid;\n for (let key in validationDetails) {\n validationDetails[key] ||= v.validationDetails[key];\n }\n }\n\n validationDetails.valid = !isInvalid;\n return {\n isInvalid,\n validationErrors: [...errors],\n validationDetails\n };\n}\n"],"names":[],"version":3,"file":"useFormValidationState.module.js.map"}
@@ -1,258 +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 {Context, createContext, useContext, useEffect, useMemo, useRef, useState} from 'react';
14
- import {Validation, ValidationErrors, ValidationFunction, ValidationResult} from '@react-types/shared';
15
-
16
- export const VALID_VALIDITY_STATE: ValidityState = {
17
- badInput: false,
18
- customError: false,
19
- patternMismatch: false,
20
- rangeOverflow: false,
21
- rangeUnderflow: false,
22
- stepMismatch: false,
23
- tooLong: false,
24
- tooShort: false,
25
- typeMismatch: false,
26
- valueMissing: false,
27
- valid: true
28
- };
29
-
30
- const CUSTOM_VALIDITY_STATE: ValidityState = {
31
- ...VALID_VALIDITY_STATE,
32
- customError: true,
33
- valid: false
34
- };
35
-
36
- export const DEFAULT_VALIDATION_RESULT: ValidationResult = {
37
- isInvalid: false,
38
- validationDetails: VALID_VALIDITY_STATE,
39
- validationErrors: []
40
- };
41
-
42
- export const FormValidationContext: Context<ValidationErrors> = createContext<ValidationErrors>({});
43
-
44
- export const privateValidationStateProp: string = '__formValidationState' + Date.now();
45
-
46
- interface FormValidationProps<T> extends Validation<T> {
47
- builtinValidation?: ValidationResult,
48
- name?: string | string[],
49
- value: T | null
50
- }
51
-
52
- export interface FormValidationState {
53
- /** Realtime validation results, updated as the user edits the value. */
54
- realtimeValidation: ValidationResult,
55
- /** Currently displayed validation results, updated when the user commits their changes. */
56
- displayValidation: ValidationResult,
57
- /** Updates the current validation result. Not displayed to the user until `commitValidation` is called. */
58
- updateValidation(result: ValidationResult): void,
59
- /** Resets the displayed validation state to valid when the user resets the form. */
60
- resetValidation(): void,
61
- /** Commits the realtime validation so it is displayed to the user. */
62
- commitValidation(): void
63
- }
64
-
65
- export function useFormValidationState<T>(props: FormValidationProps<T>): FormValidationState {
66
- // Private prop for parent components to pass state to children.
67
- if (props[privateValidationStateProp]) {
68
- let {realtimeValidation, displayValidation, updateValidation, resetValidation, commitValidation} = props[privateValidationStateProp] as FormValidationState;
69
- return {realtimeValidation, displayValidation, updateValidation, resetValidation, commitValidation};
70
- }
71
-
72
- // eslint-disable-next-line react-hooks/rules-of-hooks
73
- return useFormValidationStateImpl(props);
74
- }
75
-
76
- function useFormValidationStateImpl<T>(props: FormValidationProps<T>): FormValidationState {
77
- let {isInvalid, validationState, name, value, builtinValidation, validate, validationBehavior = 'aria'} = props;
78
-
79
- // backward compatibility.
80
- if (validationState) {
81
- isInvalid ||= validationState === 'invalid';
82
- }
83
-
84
- // If the isInvalid prop is controlled, update validation result in realtime.
85
- let controlledError: ValidationResult | null = isInvalid !== undefined ? {
86
- isInvalid,
87
- validationErrors: [],
88
- validationDetails: CUSTOM_VALIDITY_STATE
89
- } : null;
90
-
91
- // Perform custom client side validation.
92
- let clientError: ValidationResult | null = useMemo(() => {
93
- if (!validate || value == null) {
94
- return null;
95
- }
96
- let validateErrors = runValidate(validate, value);
97
- return getValidationResult(validateErrors);
98
- }, [validate, value]);
99
-
100
- if (builtinValidation?.validationDetails.valid) {
101
- builtinValidation = undefined;
102
- }
103
-
104
- // Get relevant server errors from the form.
105
- let serverErrors = useContext(FormValidationContext);
106
- let serverErrorMessages = useMemo(() => {
107
- if (name) {
108
- return Array.isArray(name) ? name.flatMap(name => asArray(serverErrors[name])) : asArray(serverErrors[name]);
109
- }
110
- return [];
111
- }, [serverErrors, name]);
112
-
113
- // Show server errors when the form gets a new value, and clear when the user changes the value.
114
- let [lastServerErrors, setLastServerErrors] = useState(serverErrors);
115
- let [isServerErrorCleared, setServerErrorCleared] = useState(false);
116
- if (serverErrors !== lastServerErrors) {
117
- setLastServerErrors(serverErrors);
118
- setServerErrorCleared(false);
119
- }
120
-
121
- let serverError: ValidationResult | null = useMemo(() =>
122
- getValidationResult(isServerErrorCleared ? [] : serverErrorMessages),
123
- [isServerErrorCleared, serverErrorMessages]
124
- );
125
-
126
- // Track the next validation state in a ref until commitValidation is called.
127
- let nextValidation = useRef(DEFAULT_VALIDATION_RESULT);
128
- let [currentValidity, setCurrentValidity] = useState(DEFAULT_VALIDATION_RESULT);
129
-
130
- let lastError = useRef(DEFAULT_VALIDATION_RESULT);
131
- let commitValidation = () => {
132
- if (!commitQueued) {
133
- return;
134
- }
135
-
136
- setCommitQueued(false);
137
- let error = clientError || builtinValidation || nextValidation.current;
138
- if (!isEqualValidation(error, lastError.current)) {
139
- lastError.current = error;
140
- setCurrentValidity(error);
141
- }
142
- };
143
-
144
- let [commitQueued, setCommitQueued] = useState(false);
145
- useEffect(commitValidation);
146
-
147
- // realtimeValidation is used to update the native input element's state based on custom validation logic.
148
- // displayValidation is the currently displayed validation state that the user sees (e.g. on input change/form submit).
149
- // With validationBehavior="aria", all errors are displayed in realtime rather than on submit.
150
- let realtimeValidation = controlledError || serverError || clientError || builtinValidation || DEFAULT_VALIDATION_RESULT;
151
- let displayValidation = validationBehavior === 'native'
152
- ? controlledError || serverError || currentValidity
153
- : controlledError || serverError || clientError || builtinValidation || currentValidity;
154
-
155
- return {
156
- realtimeValidation,
157
- displayValidation,
158
- updateValidation(value) {
159
- // If validationBehavior is 'aria', update in realtime. Otherwise, store in a ref until commit.
160
- if (validationBehavior === 'aria' && !isEqualValidation(currentValidity, value)) {
161
- setCurrentValidity(value);
162
- } else {
163
- nextValidation.current = value;
164
- }
165
- },
166
- resetValidation() {
167
- // Update the currently displayed validation state to valid on form reset,
168
- // even if the native validity says it isn't. It'll show again on the next form submit.
169
- let error = DEFAULT_VALIDATION_RESULT;
170
- if (!isEqualValidation(error, lastError.current)) {
171
- lastError.current = error;
172
- setCurrentValidity(error);
173
- }
174
-
175
- // Do not commit validation after the next render. This avoids a condition where
176
- // useSelect calls commitValidation inside an onReset handler.
177
- if (validationBehavior === 'native') {
178
- setCommitQueued(false);
179
- }
180
-
181
- setServerErrorCleared(true);
182
- },
183
- commitValidation() {
184
- // Commit validation state so the user sees it on blur/change/submit. Also clear any server errors.
185
- // Wait until after the next render to commit so that the latest value has been validated.
186
- if (validationBehavior === 'native') {
187
- setCommitQueued(true);
188
- }
189
- setServerErrorCleared(true);
190
- }
191
- };
192
- }
193
-
194
- function asArray<T>(v: T | T[]): T[] {
195
- if (!v) {
196
- return [];
197
- }
198
-
199
- return Array.isArray(v) ? v : [v];
200
- }
201
-
202
- function runValidate<T>(validate: ValidationFunction<T>, value: T): string[] {
203
- if (typeof validate === 'function') {
204
- let e = validate(value);
205
- if (e && typeof e !== 'boolean') {
206
- return asArray(e);
207
- }
208
- }
209
-
210
- return [];
211
- }
212
-
213
- function getValidationResult(errors: string[]): ValidationResult | null {
214
- return errors.length ? {
215
- isInvalid: true,
216
- validationErrors: errors,
217
- validationDetails: CUSTOM_VALIDITY_STATE
218
- } : null;
219
- }
220
-
221
- function isEqualValidation(a: ValidationResult | null, b: ValidationResult | null): boolean {
222
- if (a === b) {
223
- return true;
224
- }
225
-
226
- return !!a && !!b
227
- && a.isInvalid === b.isInvalid
228
- && a.validationErrors.length === b.validationErrors.length
229
- && a.validationErrors.every((a, i) => a === b.validationErrors[i])
230
- && Object.entries(a.validationDetails).every(([k, v]) => b.validationDetails[k] === v);
231
- }
232
-
233
- export function mergeValidation(...results: ValidationResult[]): ValidationResult {
234
- let errors = new Set<string>();
235
- let isInvalid = false;
236
- let validationDetails = {
237
- ...VALID_VALIDITY_STATE
238
- };
239
-
240
- for (let v of results) {
241
- for (let e of v.validationErrors) {
242
- errors.add(e);
243
- }
244
-
245
- // Only these properties apply for checkboxes.
246
- isInvalid ||= v.isInvalid;
247
- for (let key in validationDetails) {
248
- validationDetails[key] ||= v.validationDetails[key];
249
- }
250
- }
251
-
252
- validationDetails.valid = !isInvalid;
253
- return {
254
- isInvalid,
255
- validationErrors: [...errors],
256
- validationDetails
257
- };
258
- }