11import React from 'react' ;
2- import { SubmitHandler } from 'react-hook-form' ;
3- import { FormFeedback } from 'reactstrap' ;
42import Button from '../Button/Button' ;
53import FormGroup from '../Form/FormGroup' ;
64import Label from '../Label/Label' ;
75import Form from './Form' ;
6+ import FormFeedback from './FormFeedback' ;
7+ import FormLabelGroup from './FormLabelGroup' ;
8+ import FormRow from './FormRow' ;
89import Input from './Input' ;
10+ import { SubmitHandler } from './types' ;
911
1012export default {
1113 title : 'react-hook-form' ,
@@ -15,86 +17,215 @@ interface FormInputs {
1517 email : string ;
1618 age : string ;
1719 select : string ;
20+ address : {
21+ line1 : string ;
22+ line2 : string ;
23+ state : string ;
24+ zipCode : string ;
25+ } ;
1826}
1927
20- export const LiveExample = ( ) => {
21- const handleSubmit : SubmitHandler < FormInputs > = ( formData ) => {
28+ export const FormWithValidations = ( ) => {
29+ const handleSubmit : SubmitHandler < FormInputs > = (
30+ formData ,
31+ { setError }
32+ ) => {
33+ // make api calss
34+ // if fails then call setError
35+ setError ( 'address.line1' , {
36+ message : 'something went wrong with line1' ,
37+ } ) ;
2238 console . log ( formData ) ;
2339 } ;
2440
2541 return (
2642 < Form onSubmit = { handleSubmit } mode = "onChange" >
27- { ( { formState : { errors, dirtyFields } } ) => (
43+ { ( { reset, formState : { errors, dirtyFields } } ) => {
44+ console . log ( 'render' ) ;
45+ console . log ( errors ) ;
46+ return (
47+ < >
48+ < div className = "mb-3" >
49+
50+ < Label for = "email" > Email</ Label >
51+ < FormFeedback >
52+ < Input id = "email" name = "email" required = "Can't be blank" />
53+ </ FormFeedback >
54+
55+ < FormFeedback name = "email" />
56+
57+ </ div >
58+ < div className = "mb-3" >
59+ < legend > Address</ legend >
60+ < Label for = "line1" > Address Line 1</ Label >
61+ < Input
62+ invalid = { ! ! errors . address ?. line1 }
63+ id = "line1"
64+ name = "address.line1"
65+ />
66+ < FormFeedback name = "email" />
67+ < Label for = "line2" > Address Line 2</ Label >
68+ < Input id = "line2" name = "address.addr2" />
69+ < Label for = "state" > State</ Label >
70+ < Input id = "state" name = "address.state" />
71+ < Label for = "zipCode" > Zip Code</ Label >
72+ < Input id = "zipCode" name = "address.zipCode" />
73+ </ div >
74+ < div className = "mb-3" >
75+ < Label for = "age" > Age</ Label >
76+ < Input
77+ min = { { value : 1 , message : 'Min is 1' } }
78+ type = "number"
79+ invalid = { ! ! errors . age }
80+ id = "age"
81+ name = "age"
82+ />
83+ < FormFeedback invalid >
84+ { errors . age ?. message }
85+ </ FormFeedback >
86+ </ div >
87+ < div className = "mb-3" >
88+ < Label for = "select" > Select</ Label >
89+ < Input
90+ type = "select"
91+ invalid = { ! ! errors . select }
92+ id = "select"
93+ name = "select"
94+ >
95+ < option > 1</ option >
96+ < option > 2</ option >
97+ < option > 3</ option >
98+ < option > 4</ option >
99+ < option > 5</ option >
100+ </ Input >
101+ < FormFeedback invalid >
102+ { errors . select ?. message }
103+ </ FormFeedback >
104+ </ div >
105+ < div className = "mb-3" >
106+ < FormLabelGroup
107+ inputId = "select-multiple"
108+ label = "Select multiple"
109+ stacked
110+ >
111+ < Input
112+ type = "select"
113+ id = "select-multiple"
114+ name = "selectMuliple"
115+ multiple
116+ >
117+ < option > 1</ option >
118+ < option > 2</ option >
119+ < option > 3</ option >
120+ < option > 4</ option >
121+ < option > 5</ option >
122+ </ Input >
123+ </ FormLabelGroup >
124+ </ div >
125+ < div className = "mb-3" >
126+ < FormLabelGroup
127+ inputId = "checkboxes"
128+ label = "Check boxes"
129+ stacked
130+ >
131+ < Input
132+ type = "checkbox"
133+ id = "checkbox1"
134+ name = "checkboxes"
135+ value = "Value 1"
136+ />
137+ < Input
138+ type = "checkbox"
139+ id = "checkbox2"
140+ name = "checkboxes"
141+ value = "Value 2"
142+ />
143+ </ FormLabelGroup >
144+ </ div >
145+ < div className = "mb-3" >
146+ < legend > Radio Buttons</ legend >
147+ < FormGroup check >
148+ < Input
149+ id = "radio-option-1"
150+ name = "radio"
151+ type = "radio"
152+ value = "radio-option-value-1"
153+ /> { ' ' }
154+ < Label check for = "radio-option-1" >
155+ Option one is this and that—be sure to include
156+ why it‘s great
157+ </ Label >
158+ </ FormGroup >
159+ < FormGroup check >
160+ < Input
161+ id = "radio-option-2"
162+ name = "radio"
163+ type = "radio"
164+ value = "radio-option-value-2"
165+ /> { ' ' }
166+ < Label check for = "radio-option-2" >
167+ Option two can be something else and selecting
168+ it will deselect option one
169+ </ Label >
170+ </ FormGroup >
171+ </ div >
172+ < Button color = "primary" type = "submit" >
173+ Submit
174+ </ Button >
175+ < Button type = "button" onClick = { ( ) => reset ( ) } >
176+ Reset
177+ </ Button >
178+ </ >
179+ ) ;
180+ } }
181+ </ Form >
182+ ) ;
183+ } ;
184+
185+ interface FormValues {
186+ email : string ;
187+ }
188+
189+ export const SimpleFormNoValidation = ( ) => {
190+ const handleSubmit : SubmitHandler < FormValues > = ( formData ) => {
191+ console . log ( formData ) ;
192+ } ;
193+
194+ return (
195+ < Form onSubmit = { handleSubmit } >
196+ { ( { formState : { isValid } } ) => (
28197 < >
29- < div className = "mb-3" >
30- < Label for = "email" > Email</ Label >
31- < Input
32- valid = { dirtyFields . email && ! errors . email }
33- invalid = { ! ! errors . email }
34- id = "email"
35- name = "email"
36- validate = { ( value ) => value === 'email' || 'incorrect' }
37- />
38- < FormFeedback invalid > { errors . email ?. message } </ FormFeedback >
39- < FormFeedback valid > Looks good!</ FormFeedback >
40- </ div >
41- < div className = "mb-3" >
42- < Label for = "age" > Age</ Label >
43- < Input
44- min = { { value : 1 , message : 'Min is 1' } }
45- type = "number"
46- invalid = { ! ! errors . age }
47- id = "age"
48- name = "age"
49- required = "Can't be blank"
50- />
51- < FormFeedback invalid > { errors . age ?. message } </ FormFeedback >
52- </ div >
53- < div className = "mb-3" >
54- < Label for = "select" > Select</ Label >
55- < Input type = "select" invalid = { ! ! errors . select } id = "select" name = "select" >
56- < option > 1</ option >
57- < option > 2</ option >
58- < option > 3</ option >
59- < option > 4</ option >
60- < option > 5</ option >
61- </ Input >
62- < FormFeedback invalid > { errors . select ?. message } </ FormFeedback >
63- </ div >
64- < div className = "mb-3" >
65- < legend > Radio Buttons</ legend >
66- < FormGroup check >
67- < Input id = "radio-option-1" name = "radio" type = "radio" value = "radio-option-value-1" /> { ' ' }
68- < Label check for = "radio-option-1" >
69- Option one is this and that—be sure to include why it‘s great
70- </ Label >
71- </ FormGroup >
72- < FormGroup check >
73- < Input id = "radio-option-2" name = "radio" type = "radio" value = "radio-option-value-2" /> { ' ' }
74- < Label check for = "radio-option-2" >
75- Option two can be something else and selecting it will deselect option one
76- </ Label >
77- </ FormGroup >
78- </ div >
79- < Button type = "submit" > Submit</ Button >
198+ < FormRow name = "test" type = "month" />
199+ < Button type = "submit" disabled = { ! isValid } >
200+ Submit
201+ </ Button >
80202 </ >
81203 ) }
82204 </ Form >
83205 ) ;
84206} ;
85207
86- export const TestExample = ( ) => {
87- const handleSubmit : SubmitHandler < FormInputs > = ( formData ) => {
208+ export const FormDemo = ( ) => {
209+ const handleSubmit : SubmitHandler < FormValues > = ( formData ) => {
88210 console . log ( formData ) ;
89211 } ;
90212
91213 return (
92- < Form onSubmit = { handleSubmit } mode = "onChange" >
93- < div className = "mb-3" >
94- < Label for = "email" > Email</ Label >
95- < Input id = "email" name = "email" />
96- < FormFeedback valid > Looks good!</ FormFeedback >
97- </ div >
98- </ Form >
214+
215+ < Form onSubmit = { handleSubmit } >
216+ < FormLabelGroup label = "First Name" >
217+ < Input
218+ id = "first-name"
219+ name = "firstName"
220+ required = "Can't be blank"
221+ />
222+ </ FormLabelGroup >
223+ < FormLabelGroup label = "Last Name" >
224+ < Input id = "last-name" name = "lastName" />
225+ </ FormLabelGroup >
226+ < FormLabelGroup label = { < > </ > } >
227+ < Button color = "primary" type = "submit" > Submit</ Button >
228+ </ FormLabelGroup >
229+ </ Form >
99230 ) ;
100231} ;
0 commit comments