overleaf/services/web/frontend/js/features/subscription/hooks/use-validate-field.tsx
ilkin-overleaf 7f8d2e37d5 Merge pull request #11530 from overleaf/ii-payment-page-migration-checkout-panel
[web] Payment page migration checkout panel

GitOrigin-RevId: 04edf9961e0032d6fe3631c255e49dc1d4c3e0ca
2023-02-07 09:04:53 +00:00

21 lines
465 B
TypeScript

import { useState } from 'react'
type Target = HTMLInputElement | HTMLSelectElement
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