()
| 4 | let renderCounter = 0; |
| 5 | |
| 6 | const SetValueWithTrigger: React.FC = () => { |
| 7 | const { |
| 8 | register, |
| 9 | setValue, |
| 10 | handleSubmit, |
| 11 | formState: { errors }, |
| 12 | } = useForm<{ |
| 13 | firstName: string; |
| 14 | lastName: string; |
| 15 | }>(); |
| 16 | |
| 17 | useEffect(() => { |
| 18 | register('firstName', { |
| 19 | required: 'required', |
| 20 | minLength: { |
| 21 | value: 10, |
| 22 | message: 'minLength 10', |
| 23 | }, |
| 24 | }); |
| 25 | register('lastName', { |
| 26 | validate: (data) => { |
| 27 | if (data === 'bill') { |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | if (data && data.length < 10) { |
| 32 | return 'too short'; |
| 33 | } |
| 34 | |
| 35 | return 'error message'; |
| 36 | }, |
| 37 | }); |
| 38 | }, [register]); |
| 39 | |
| 40 | renderCounter++; |
| 41 | |
| 42 | return ( |
| 43 | <form onSubmit={handleSubmit(() => {})}> |
| 44 | <input |
| 45 | name="firstName" |
| 46 | placeholder="firstName" |
| 47 | onChange={(e) => |
| 48 | setValue('firstName', e.target.value, { |
| 49 | shouldValidate: true, |
| 50 | shouldDirty: true, |
| 51 | }) |
| 52 | } |
| 53 | /> |
| 54 | {errors.firstName && <p>{errors.firstName.message}</p>} |
| 55 | |
| 56 | <input |
| 57 | name="lastName" |
| 58 | placeholder="lastName" |
| 59 | onChange={(e) => |
| 60 | setValue('lastName', e.target.value, { |
| 61 | shouldValidate: true, |
| 62 | shouldDirty: true, |
| 63 | }) |
nothing calls this directly
no test coverage detected