MCPcopy Create free account
hub / github.com/react-hook-form/react-hook-form / ConditionalField

Function ConditionalField

app/src/conditionalField.tsx:6–103  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4let renderCounter = 0;
5
6const ConditionalField: React.FC = () => {
7 const {
8 register,
9 handleSubmit,
10 watch,
11 formState: {
12 dirtyFields,
13 isSubmitted,
14 submitCount,
15 touchedFields,
16 isDirty,
17 isSubmitting,
18 isSubmitSuccessful,
19 isValid,
20 errors,
21 },
22 } = useForm<{
23 selectNumber: string;
24 firstName: string;
25 lastName: string;
26 min: string;
27 max: string;
28 notRequired: string;
29 }>({
30 mode: 'onChange',
31 });
32 const [result, setResult] = React.useState({});
33
34 const selectNumber = watch('selectNumber');
35
36 renderCounter++;
37
38 return (
39 <form
40 onSubmit={handleSubmit((data) => {
41 setResult(data);
42 })}
43 >
44 <select {...register('selectNumber', { required: true })}>
45 <option value="">Select</option>
46 <option value="1">1</option>
47 <option value="2">2</option>
48 <option value="3">3</option>
49 </select>
50
51 {selectNumber === '1' && (
52 <>
53 <input
54 {...register('firstName', { required: true })}
55 placeholder="firstName"
56 />
57 <input
58 {...register('lastName', { required: true })}
59 placeholder="lastName"
60 />
61 </>
62 )}
63

Callers

nothing calls this directly

Calls 4

useFormFunction · 0.85
watchFunction · 0.85
handleSubmitFunction · 0.85
registerFunction · 0.85

Tested by

no test coverage detected