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,158 @@ 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 > = ( formData , { setError } ) => {
30+ // make api calss
31+ // if fails then call setError
32+ setError ( 'address.line1' , {
33+ message : 'something went wrong with line1' ,
34+ } ) ;
2235 console . log ( formData ) ;
2336 } ;
2437
2538 return (
2639 < Form onSubmit = { handleSubmit } mode = "onChange" >
27- { ( { formState : { errors, dirtyFields } } ) => (
40+ { ( { reset, formState : { errors, dirtyFields } } ) => {
41+ console . log ( 'render' ) ;
42+ console . log ( errors ) ;
43+ return (
44+ < >
45+ < div className = "mb-3" >
46+ < Label for = "email" > Email</ Label >
47+ < FormFeedback >
48+ < Input id = "email" name = "email" required = "Can't be blank" />
49+ </ FormFeedback >
50+
51+ < FormFeedback name = "email" />
52+ </ div >
53+ < div className = "mb-3" >
54+ < legend > Address</ legend >
55+ < Label for = "line1" > Address Line 1</ Label >
56+ < Input invalid = { ! ! errors . address ?. line1 } id = "line1" name = "address.line1" />
57+ < FormFeedback name = "email" />
58+ < Label for = "line2" > Address Line 2</ Label >
59+ < Input id = "line2" name = "address.addr2" />
60+ < Label for = "state" > State</ Label >
61+ < Input id = "state" name = "address.state" />
62+ < Label for = "zipCode" > Zip Code</ Label >
63+ < Input id = "zipCode" name = "address.zipCode" />
64+ </ div >
65+ < div className = "mb-3" >
66+ < Label for = "age" > Age</ Label >
67+ < Input
68+ min = { { value : 1 , message : 'Min is 1' } }
69+ type = "number"
70+ invalid = { ! ! errors . age }
71+ id = "age"
72+ name = "age"
73+ />
74+ < FormFeedback invalid > { errors . age ?. message } </ FormFeedback >
75+ </ div >
76+ < div className = "mb-3" >
77+ < Label for = "select" > Select</ Label >
78+ < Input type = "select" invalid = { ! ! errors . select } id = "select" name = "select" >
79+ < option > 1</ option >
80+ < option > 2</ option >
81+ < option > 3</ option >
82+ < option > 4</ option >
83+ < option > 5</ option >
84+ </ Input >
85+ < FormFeedback invalid > { errors . select ?. message } </ FormFeedback >
86+ </ div >
87+ < div className = "mb-3" >
88+ < FormLabelGroup inputId = "select-multiple" label = "Select multiple" stacked >
89+ < Input type = "select" id = "select-multiple" name = "selectMuliple" multiple >
90+ < option > 1</ option >
91+ < option > 2</ option >
92+ < option > 3</ option >
93+ < option > 4</ option >
94+ < option > 5</ option >
95+ </ Input >
96+ </ FormLabelGroup >
97+ </ div >
98+ < div className = "mb-3" >
99+ < FormLabelGroup inputId = "checkboxes" label = "Check boxes" stacked >
100+ < Input type = "checkbox" id = "checkbox1" name = "checkboxes" value = "Value 1" />
101+ < Input type = "checkbox" id = "checkbox2" name = "checkboxes" value = "Value 2" />
102+ </ FormLabelGroup >
103+ </ div >
104+ < div className = "mb-3" >
105+ < legend > Radio Buttons</ legend >
106+ < FormGroup check >
107+ < Input id = "radio-option-1" name = "radio" type = "radio" value = "radio-option-value-1" /> { ' ' }
108+ < Label check for = "radio-option-1" >
109+ Option one is this and that—be sure to include why it‘s great
110+ </ Label >
111+ </ FormGroup >
112+ < FormGroup check >
113+ < Input id = "radio-option-2" name = "radio" type = "radio" value = "radio-option-value-2" /> { ' ' }
114+ < Label check for = "radio-option-2" >
115+ Option two can be something else and selecting it will deselect option one
116+ </ Label >
117+ </ FormGroup >
118+ </ div >
119+ < Button color = "primary" type = "submit" >
120+ Submit
121+ </ Button >
122+ < Button type = "button" onClick = { ( ) => reset ( ) } >
123+ Reset
124+ </ Button >
125+ </ >
126+ ) ;
127+ } }
128+ </ Form >
129+ ) ;
130+ } ;
131+
132+ interface FormValues {
133+ email : string ;
134+ }
135+
136+ export const SimpleFormNoValidation = ( ) => {
137+ const handleSubmit : SubmitHandler < FormValues > = ( formData ) => {
138+ console . log ( formData ) ;
139+ } ;
140+
141+ return (
142+ < Form onSubmit = { handleSubmit } >
143+ { ( { formState : { isValid } } ) => (
28144 < >
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 >
145+ < FormRow name = "test" type = "month" />
146+ < Button type = "submit" disabled = { ! isValid } >
147+ Submit
148+ </ Button >
80149 </ >
81150 ) }
82151 </ Form >
83152 ) ;
84153} ;
85154
86- export const TestExample = ( ) => {
87- const handleSubmit : SubmitHandler < FormInputs > = ( formData ) => {
155+ export const FormDemo = ( ) => {
156+ const handleSubmit : SubmitHandler < FormValues > = ( formData , { reset } ) => {
88157 console . log ( formData ) ;
158+ reset ( ) ;
89159 } ;
90160
91161 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 >
162+ < Form onSubmit = { handleSubmit } >
163+ < FormLabelGroup label = "First Name" >
164+ < Input id = "first-name" name = "firstName" required = "Can't be blank" />
165+ </ FormLabelGroup >
166+ < FormLabelGroup label = "Last Name" >
167+ < Input id = "last-name" name = "lastName" />
168+ </ FormLabelGroup >
169+ < Button color = "primary" type = "submit" >
170+ Submit
171+ </ Button >
98172 </ Form >
99173 ) ;
100174} ;
0 commit comments