formhero/test/test.tsx

73 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-09-26 19:30:14 +00:00
import React from 'react'
2019-09-26 19:13:27 +00:00
import ReactDOM from 'react-dom'
2019-09-26 19:30:14 +00:00
import { useForm } from '../'
2019-09-26 19:13:27 +00:00
const TextError: React.FC<{ error?: string }> = ({ error }) => !error
2019-09-26 19:30:14 +00:00
? null
: <div className="has-text-danger">{error}</div>
2019-09-26 19:13:27 +00:00
const Index: React.FC = () => {
2019-09-26 19:30:14 +00:00
const { auto, form, errors } = useForm({
username: '',
password: '',
type: 'formhero',
awesome: true,
}, {
username: /^test/,
password: {
validator: /^.{3,}$/,
message: 'To short',
},
awesome: (value) => !!value
})
2019-09-26 19:13:27 +00:00
2019-09-26 19:30:14 +00:00
const _submit = (e: React.FormEvent) => {
e.preventDefault()
console.log(form, errors)
}
2019-09-26 19:13:27 +00:00
2019-09-26 19:30:14 +00:00
return (
<div>
<form onSubmit={_submit}>
<div>Username</div>
<input {...auto('username')} />
{errors.username && 'Something went wrong'}
<TextError error={errors.username} />
<br />
2019-09-26 19:13:27 +00:00
2019-09-26 19:30:14 +00:00
<div>Password</div>
<input {...auto('password')} />
<TextError error={errors.password} />
<br />
2019-09-26 19:13:27 +00:00
2019-09-26 19:30:14 +00:00
<div>Which one to choose?</div>
<select {...auto('type')}>
<option value="redux-form">Redux-Form</option>
<option value="react-hook-forms">React-Hook-Forms</option>
<option value="formik">Formik</option>
<option value="formhero">FormHero</option>
</select>
<br />
2019-09-26 19:13:27 +00:00
2019-09-26 19:30:14 +00:00
<div>Is it awesome?</div>
<input type="checkbox" name="vehicle" {...auto('awesome', {
setter: 'checked',
getter: 'onChange',
extractor: (e) => e.target.checked
})} />
<TextError error={errors.awesome} />
<br />
2019-09-26 19:13:27 +00:00
2019-09-26 19:30:14 +00:00
<button type="submit">Go 🚀</button>
</form>
</div>
)
2019-09-26 19:13:27 +00:00
}
ReactDOM.render(<Index />, document.getElementById('root'))
// @ts-ignore
// if (module.hot) module.hot.accept()