formhero/test/test.tsx

83 lines
2.0 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-28 17:00:49 +00:00
const { auto, form, errors, isValid } = useForm({
2019-09-26 19:30:14 +00:00
username: '',
password: '',
type: 'formhero',
awesome: true,
}, {
2019-09-28 17:00:49 +00:00
username: [
/^test/,
{
validator: async () => { return true },
message: 'Digits please',
}
],
2019-09-26 19:30:14 +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-09-26 19:30:14 +00:00
return (
<div>
<form onSubmit={_submit}>
<div>Username</div>
2019-09-26 21:05:05 +00:00
<input className="input" {...auto('username')} />
2019-09-26 19:30:14 +00:00
<TextError error={errors.username} />
2019-09-26 21:05:05 +00:00
<br /><br />
2019-09-26 19:13:27 +00:00
2019-09-26 19:30:14 +00:00
<div>Password</div>
2019-09-26 21:05:05 +00:00
<input className="input" {...auto('password')} />
2019-09-26 19:30:14 +00:00
<TextError error={errors.password} />
2019-09-26 21:05:05 +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">
<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>
</div>
<br /><br />
2019-09-26 19:13:27 +00:00
2019-09-26 21:05:05 +00:00
<label className="checkbox">
<input type="checkbox" {...auto('awesome', {
setter: 'checked',
getter: 'onChange',
extractor: (e) => e.target.checked
})} />
Is it awesome?
</label>
2019-09-26 19:30:14 +00:00
<TextError error={errors.awesome} />
2019-09-26 21:05:05 +00:00
<br /><br />
2019-09-26 19:13:27 +00:00
2019-09-26 21:05:05 +00:00
<button className="button" type="submit">Go 🚀</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'))
2019-09-26 21:05:05 +00:00
// @ts-ignore
2019-09-26 19:13:27 +00:00
// if (module.hot) module.hot.accept()