Path: blob/main/components/dashboard/src/hooks/use-onblur-error.ts
2500 views
/**1* Copyright (c) 2023 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { useCallback, useState } from "react";78// Provided an error message and boolean indicating if related property is valid9// returns an error message if field has been blurred and it's invalid.10// An `onBlur` handler is meant to be applied to the relevant input field11export const useOnBlurError = (message: string, isValid: boolean) => {12const [hasBlurred, setHasBlurred] = useState(false);1314const onBlur = useCallback(() => {15setHasBlurred(true);16}, []);1718return { message: !isValid && hasBlurred ? message : "", isValid, onBlur };19};202122