overleaf/services/web/frontend/js/features/subscription/hooks/use-validate-field.tsx
M Fahru 9dba1ccd8f Merge pull request #21854 from overleaf/as-checkout-name-fields
Add new Name fields for Checkout Redesign

GitOrigin-RevId: 448084e026a86510efe3c76c6663c0a770c24f8e
2024-11-15 09:06:07 +00:00

21 lines
487 B
TypeScript

import { useState } from 'react'
type Target = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement
function useValidateField<T extends { target: Target }>() {
const [isValid, setIsValid] = useState(true)
const validate = (e: T) => {
let isValid = e.target.checkValidity()
if (e.target.required) {
isValid = isValid && Boolean(e.target.value.trim().length)
}
setIsValid(isValid)
}
return { validate, isValid }
}
export default useValidateField