formhero/test/test.tsx

111 lines
2.5 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
2019-10-19 06:36:50 +00:00
const TextError: React.FC<{ error?: string }> = ({ error }) => (!error ? null : <div className="has-text-danger">{error}</div>)
2019-09-26 19:13:27 +00:00
2019-10-19 14:39:53 +00:00
const initial = {
username: '',
password: '',
type: 'formhero',
awesome: true,
}
2019-09-26 19:13:27 +00:00
const Index: React.FC = () => {
2019-10-19 14:39:53 +00:00
const { field, form, errors, isValid, setForm, setErrors } = useForm(initial, {
username: [
/^test/,
{
validator: async () => {
return true
2019-10-19 06:36:50 +00:00
},
2019-10-19 14:39:53 +00:00
message: 'Digits please',
2019-10-19 06:36:50 +00:00
},
2019-10-19 14:39:53 +00:00
],
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()
2019-09-28 17:00:49 +00:00
console.log(form, errors, isValid)
2019-09-26 19:30:14 +00:00
}
2019-09-26 19:13:27 +00:00
2019-10-19 14:39:53 +00:00
const reset = () => {
setForm(initial)
}
const error = () => {
setErrors({
username: 'nope',
})
}
2019-09-26 19:30:14 +00:00
return (
<div>
<form onSubmit={_submit}>
<div>Username</div>
2019-10-19 06:45:14 +00:00
<input className="input" {...field('username')} />
2019-09-26 19:30:14 +00:00
<TextError error={errors.username} />
2019-10-19 06:36:50 +00:00
<br />
<br />
2019-09-26 19:13:27 +00:00
2019-09-26 19:30:14 +00:00
<div>Password</div>
2019-10-19 06:45:14 +00:00
<input className="input" {...field('password')} />
2019-09-26 19:30:14 +00:00
<TextError error={errors.password} />
2019-10-19 06:36:50 +00:00
<br />
<br />
2019-09-26 19:13:27 +00:00
2019-09-26 19:30:14 +00:00
<div>Which one to choose?</div>
2019-09-26 21:05:05 +00:00
<div className="select">
2019-10-19 06:45:14 +00:00
<select {...field('type')}>
2019-09-26 21:05:05 +00:00
<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>
</div>
2019-10-19 06:36:50 +00:00
<br />
<br />
2019-09-26 21:05:05 +00:00
<label className="checkbox">
2019-10-19 06:36:50 +00:00
<input
type="checkbox"
2019-10-19 06:45:14 +00:00
{...field('awesome', {
2019-10-19 06:36:50 +00:00
setter: 'checked',
getter: 'onChange',
extractor: e => e.target.checked,
})}
/>
2019-09-26 21:05:05 +00:00
Is it awesome?
</label>
2019-09-26 19:30:14 +00:00
<TextError error={errors.awesome} />
2019-10-19 06:36:50 +00:00
<br />
<br />
2019-09-26 19:13:27 +00:00
2019-10-19 06:36:50 +00:00
<button className="button" type="submit">
Go 🚀
</button>
2019-10-19 14:39:53 +00:00
<br />
<br />
<button className="button" onClick={reset}>
Reset 🔥
</button>
<br />
<br />
<button className="button" onClick={error}>
Set Error
</button>
2019-09-26 19:30:14 +00:00
</form>
</div>
)
2019-09-26 19:13:27 +00:00
}
ReactDOM.render(<Index />, document.getElementById('root'))