-
Notifications
You must be signed in to change notification settings - Fork 0
/
Home.js
97 lines (75 loc) · 2.01 KB
/
Home.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { IonButton, IonCardSubtitle, IonContent, IonHeader, IonIcon, IonInput, IonItem, IonLabel, IonPage, IonTitle, IonToolbar } from '@ionic/react';
import './Home.css';
import { useForm } from 'react-hook-form';
import { alertCircleOutline } from "ionicons/icons";
const Home = () => {
const { register, handleSubmit, formState: { errors } } = useForm({
mode: "onTouched",
reValidateMode: "onChange"
});
const fields = [
{
label: "Firstname",
required: true,
requiredOptions: {
maxLength: 10
},
props: {
name: "firstname",
type: "text",
placeholder: "Enter a username"
}
},
{
label: "Age",
required: true,
requiredOptions: {
min: 18,
max: 99
},
props: {
name: "age",
type: "number",
inputmode: "numeric",
placeholder: "Enter your age"
}
}
];
console.log(errors);
const onSubmit = (data) => {
console.log(data);
}
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>React Hook Form</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent fullscreen>
<IonHeader collapse="condense">
<IonToolbar>
<IonTitle size="large">React Hook Form</IonTitle>
</IonToolbar>
</IonHeader>
<IonCardSubtitle className="ion-text-center ion-margin">An example using React Hook Form</IonCardSubtitle>
<form onSubmit={ handleSubmit(onSubmit) }>
{ fields.map((field, index) => {
const { label, required, requiredOptions, props } = field;
return (
<IonItem key={ `form_field_${ index }` } lines="full">
<>
<IonLabel position="fixed">{ label }</IonLabel>
<IonInput { ...props } { ...register(props.name, { required, ...requiredOptions }) } />
</>
{ required && errors[props.name] && <IonIcon icon={ alertCircleOutline } color="danger" /> }
</IonItem>
);
})}
<IonButton type="submit" className="ion-margin-top" expand="full">Submit</IonButton>
</form>
</IonContent>
</IonPage>
);
};
export default Home;