|
| 1 | +import React from 'react' |
| 2 | +import { useForm, SubmitHandler } from 'react-hook-form' |
| 3 | + |
| 4 | +interface IFormInput { |
| 5 | + name: string |
| 6 | + selectField: string |
| 7 | + expectFormError: boolean |
| 8 | +} |
| 9 | + |
| 10 | +const ComplexFormWithHookForm = () => { |
| 11 | + const { |
| 12 | + register, |
| 13 | + handleSubmit, |
| 14 | + watch, |
| 15 | + formState: { errors }, |
| 16 | + } = useForm<IFormInput>() |
| 17 | + const onSubmit: SubmitHandler<IFormInput> = (data) => { |
| 18 | + const statusCode = data.expectFormError ? parseInt(data.name, 10) : 200 |
| 19 | + const formData = { |
| 20 | + status: statusCode, |
| 21 | + name: data.name, |
| 22 | + selectField: data.selectField, |
| 23 | + } |
| 24 | + console.log('Submitting form:', JSON.stringify(formData)) |
| 25 | + fetch('/parrot', { |
| 26 | + method: 'POST', |
| 27 | + headers: { |
| 28 | + 'Content-Type': 'application/json', |
| 29 | + }, |
| 30 | + body: JSON.stringify(formData), |
| 31 | + }) |
| 32 | + .then((response) => response.json()) |
| 33 | + .then((data) => { |
| 34 | + console.log('Form submitted successfully:', data) |
| 35 | + }) |
| 36 | + .catch((error) => { |
| 37 | + console.error('Error submitting form:', error) |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + const name = watch('name') |
| 42 | + const statusCode = React.useMemo(() => { |
| 43 | + try { |
| 44 | + const val = parseInt(name, 10) |
| 45 | + return val >= 100 && val <= 511 ? val : 400 |
| 46 | + } catch (err) { |
| 47 | + return 400 |
| 48 | + } |
| 49 | + }, [name]) |
| 50 | + |
| 51 | + return ( |
| 52 | + <div> |
| 53 | + <button data-custom-attr="some-custom-attribute">Example Button</button> |
| 54 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 55 | + <div> |
| 56 | + <label htmlFor="ex-input-text">Input some text:</label> |
| 57 | + <input |
| 58 | + id="ex-input-text" |
| 59 | + type="text" |
| 60 | + {...register('name', { required: true })} |
| 61 | + /> |
| 62 | + {errors.name && <span>This field is required</span>} |
| 63 | + </div> |
| 64 | + <div> |
| 65 | + <label htmlFor="ex-select">Select an option:</label> |
| 66 | + <select |
| 67 | + id="ex-select" |
| 68 | + {...register('selectField', { required: true })} |
| 69 | + > |
| 70 | + <option value="" disabled> |
| 71 | + Select |
| 72 | + </option> |
| 73 | + <option value="Option 1">Option 1</option> |
| 74 | + <option value="Option 2">Option 2</option> |
| 75 | + </select> |
| 76 | + {errors.selectField && <span>This field is required</span>} |
| 77 | + </div> |
| 78 | + <div> |
| 79 | + <label htmlFor="ex-checkbox">{`Force submit network status: ${statusCode}`}</label> |
| 80 | + <input |
| 81 | + id="ex-checkbox" |
| 82 | + type="checkbox" |
| 83 | + {...register('expectFormError')} |
| 84 | + /> |
| 85 | + </div> |
| 86 | + <button type="submit">Submit via network req</button> |
| 87 | + </form> |
| 88 | + <button> |
| 89 | + <div> |
| 90 | + Other Example Button with <h1>Nested Text</h1> |
| 91 | + </div> |
| 92 | + </button> |
| 93 | + </div> |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | +export default ComplexFormWithHookForm |
0 commit comments