Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/hooks/use-onblur-error.ts
2500 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { useCallback, useState } from "react";
8
9
// Provided an error message and boolean indicating if related property is valid
10
// returns an error message if field has been blurred and it's invalid.
11
// An `onBlur` handler is meant to be applied to the relevant input field
12
export const useOnBlurError = (message: string, isValid: boolean) => {
13
const [hasBlurred, setHasBlurred] = useState(false);
14
15
const onBlur = useCallback(() => {
16
setHasBlurred(true);
17
}, []);
18
19
return { message: !isValid && hasBlurred ? message : "", isValid, onBlur };
20
};
21
22