formhero/examples/errorsAndValidation.tsx
2019-09-28 19:00:49 +02:00

48 lines
903 B
TypeScript

import React from 'react'
import ReactDOM from 'react-dom'
import { useForm } from '../'
const Index: React.FC = () => {
const { auto, form, errors } = useForm({
username: '',
email: '',
password: ''
}, {
username: value => value.length > 3,
email: {
validator: /@/,
message: 'Must contain an @',
},
password: [
{
validator: /[A-Z]/,
message: 'Must contain an uppercase letter'
},
{
validator: /[\d]/,
message: 'Must contain a digit'
},
]
})
return (
<form>
<h1>Errors & Validation</h1>
<input {...auto('username')} placeholder="Username" />
{errors.username && 'Must be longer than 3'}
<input {...auto('email')} placeholder="EMail" />
{errors.email}
<input {...auto('password')} placeholder="Password" type="password" />
{errors.password}
</form>
)
}
ReactDOM.render(<Index />, document.getElementById('errors'))