mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
22 lines
465 B
TypeScript
22 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
|