mirror of
https://github.com/cupcakearmy/formhero.git
synced 2024-12-22 08:06:24 +00:00
testing
This commit is contained in:
parent
0c19233b4d
commit
7cc9654507
101
test/test.tsx
101
test/test.tsx
@ -1,65 +1,70 @@
|
||||
import React, { useState } from 'react'
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom'
|
||||
|
||||
import { useForm, HTMLInputExtractor } from '../'
|
||||
import { useForm } from '../'
|
||||
|
||||
|
||||
const TextError: React.FC<{ error?: string }> = ({ error }) => !error
|
||||
? null
|
||||
: <div className="has-text-danger">{error}</div>
|
||||
? null
|
||||
: <div className="has-text-danger">{error}</div>
|
||||
|
||||
const Index: React.FC = () => {
|
||||
|
||||
const { auto, form, update, errors } = useForm({
|
||||
username: '',
|
||||
password: '',
|
||||
type: 'formhero',
|
||||
awesome: true,
|
||||
}, {
|
||||
username: /^test/,
|
||||
password: {
|
||||
validator: /^.{3,}$/,
|
||||
message: 'To short',
|
||||
},
|
||||
awesome: (value) => !!value
|
||||
}, { extractor: HTMLInputExtractor })
|
||||
const { auto, form, errors } = useForm({
|
||||
username: '',
|
||||
password: '',
|
||||
type: 'formhero',
|
||||
awesome: true,
|
||||
}, {
|
||||
username: /^test/,
|
||||
password: {
|
||||
validator: /^.{3,}$/,
|
||||
message: 'To short',
|
||||
},
|
||||
awesome: (value) => !!value
|
||||
})
|
||||
|
||||
const _submit = (e: React.MouseEvent) => {
|
||||
e.preventDefault()
|
||||
console.log(form, errors)
|
||||
}
|
||||
const _submit = (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
console.log(form, errors)
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<form>
|
||||
<div>Username</div>
|
||||
<input {...auto('username')} />
|
||||
<TextError error={errors.username} />
|
||||
<br />
|
||||
return (
|
||||
<div>
|
||||
<form onSubmit={_submit}>
|
||||
<div>Username</div>
|
||||
<input {...auto('username')} />
|
||||
{errors.username && 'Something went wrong'}
|
||||
<TextError error={errors.username} />
|
||||
<br />
|
||||
|
||||
<div>Password</div>
|
||||
<input {...auto('password')} />
|
||||
<TextError error={errors.password} />
|
||||
<br />
|
||||
<div>Password</div>
|
||||
<input {...auto('password')} />
|
||||
<TextError error={errors.password} />
|
||||
<br />
|
||||
|
||||
<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 />
|
||||
<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 />
|
||||
|
||||
<div>Is it awesome?</div>
|
||||
<input type="checkbox" name="vehicle" {...auto('awesome', { setter: 'checked', extractor: (e) => e.target.checked })} />
|
||||
<TextError error={errors.awesome} />
|
||||
<br />
|
||||
<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 />
|
||||
|
||||
<button onClick={_submit}>Test</button>
|
||||
</form>
|
||||
</div>
|
||||
)
|
||||
<button type="submit">Go 🚀</button>
|
||||
</form>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
ReactDOM.render(<Index />, document.getElementById('root'))
|
||||
|
Loading…
Reference in New Issue
Block a user