Skip to content

Commit 43ed8f9

Browse files
committed
added isElementValid method to the hook to quickly check if an element is valid.
1 parent 3e3fe75 commit 43ed8f9

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-validation-react",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "a lightweight react library for hook-based declarative form validation",
55
"main": "./lib/cjs/index.js",
66
"module": "./lib/esm/index.js",

src/packages/useValidation.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type UseValidationReturnType<T> = {
1313
validateAll: (form: T) => Promise<Record<keyof T, ValidationResultType>>;
1414
validateFormElement: (form: T, elementKey: keyof T) => Promise<void>;
1515
clearValidations: () => void;
16+
isElementValid: (elementKey: keyof T) => boolean
1617
};
1718

1819
export const useValidation = <T>(
@@ -60,12 +61,20 @@ export const useValidation = <T>(
6061
setValidationResult({} as Record<keyof T, ValidationResultType>);
6162
}, []);
6263

64+
const isElementValid = useCallback((elementKey: keyof T) => {
65+
if (!validationResult[elementKey]) {
66+
return true;
67+
}
68+
return validationResult[elementKey].isValid;
69+
}, [validationResult]);
70+
6371
return {
6472
validationResult,
6573
isValidated,
6674
isFormValid,
6775
validateAll,
6876
validateFormElement,
6977
clearValidations,
78+
isElementValid,
7079
};
71-
};
80+
};

0 commit comments

Comments
 (0)