()
| 5 | |
| 6 | |
| 7 | function App() { |
| 8 | const { |
| 9 | register, |
| 10 | handleSubmit, |
| 11 | setError, |
| 12 | formState: { errors, isSubmitting }, |
| 13 | } = useForm(); |
| 14 | |
| 15 | const delay = (d)=>{ |
| 16 | return new Promise((resolve, reject)=>{ |
| 17 | setTimeout(() => { |
| 18 | resolve() |
| 19 | }, d * 1000); |
| 20 | }) |
| 21 | } |
| 22 | |
| 23 | const onSubmit = async (data) => { |
| 24 | // await delay(2) // simulating network delay |
| 25 | let r = await fetch("http://localhost:3000/", {method: "POST", headers: { |
| 26 | "Content-Type": "application/json", |
| 27 | }, body: JSON.stringify(data)}) |
| 28 | let res = await r.text() |
| 29 | console.log(data, res) |
| 30 | // if(data.username !== "shubham"){ |
| 31 | // setError("myform", {message: "Your form is not in good order because credentials are invalid"}) |
| 32 | // } |
| 33 | // if(data.username === "rohan"){ |
| 34 | // setError("blocked", {message: "Sorry this user is blocked"}) |
| 35 | // } |
| 36 | } |
| 37 | |
| 38 | return ( |
| 39 | <> |
| 40 | {isSubmitting && <div>Loading...</div>} |
| 41 | <div className="container"> |
| 42 | <form action="" onSubmit={handleSubmit(onSubmit)}> |
| 43 | <input placeholder='username' {...register("username", { required: {value: true, message: "This field is required"}, minLength: {value: 3, message: "Min length is 3"}, maxLength: {value: 8, message: "Max length is 8"} })} type="text" /> |
| 44 | {errors.username && <div className='red'>{errors.username.message}</div>} |
| 45 | <br /> |
| 46 | <input placeholder='password' {...register("password", {minLength: {value: 7, message: "Min length of password is 7"},})} type="password"/> |
| 47 | {errors.password && <div className='red'>{errors.password.message}</div>} |
| 48 | <br /> |
| 49 | <input disabled={isSubmitting} type="submit" value="Submit" /> |
| 50 | {errors.myform && <div className='red'>{errors.myform.message}</div>} |
| 51 | {errors.blocked && <div className='red'>{errors.blocked.message}</div>} |
| 52 | </form> |
| 53 | </div> |
| 54 | </> |
| 55 | ) |
| 56 | } |
| 57 | |
| 58 | export default App |
nothing calls this directly
no test coverage detected