1
1
import React , { Component } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { reduxForm , Field , fieldPropTypes } from 'redux-form' ;
4
+ import { Form , Label , Input as InputComponent , Table , Button } from 'semantic-ui-react' ;
5
+ import { CheckboxField } from 'react-semantic-redux-form' ;
4
6
import { connect } from 'react-redux' ;
5
7
import { isValidEmail } from 'redux/modules/survey' ;
6
8
import surveyValidation from './surveyValidation' ;
@@ -15,44 +17,46 @@ const Input = ({
15
17
label,
16
18
type,
17
19
showAsyncValidating,
18
- className,
19
20
styles,
21
+ required,
22
+ width,
20
23
meta : {
21
24
touched, error, dirty, active, visited, asyncValidating
22
- }
25
+ } ,
26
+ ...rest
23
27
} ) => (
24
- < div className = { `form-group ${ error && touched ? 'has-error' : '' } ` } >
25
- < label htmlFor = { input . name } className = "col-sm-2" >
26
- { label }
27
- </ label >
28
- < div className = { `col-sm-8 ${ styles . inputGroup } ` } >
29
- { showAsyncValidating && asyncValidating && < i className = { `fa fa-cog fa-spin ${ styles . cog } ` } /> }
30
- < input { ...input } type = { type } className = { className } id = { input . name } />
31
- { error && touched && < div className = "text-danger" > { error } </ div > }
32
- < div className = { styles . flags } >
33
- { dirty && (
34
- < span className = { styles . dirty } title = "Dirty" >
35
- D
36
- </ span >
37
- ) }
38
- { active && (
39
- < span className = { styles . active } title = "Active" >
40
- A
41
- </ span >
42
- ) }
43
- { visited && (
44
- < span className = { styles . visited } title = "Visited" >
45
- V
46
- </ span >
47
- ) }
48
- { touched && (
49
- < span className = { styles . touched } title = "Touched" >
50
- T
51
- </ span >
52
- ) }
53
- </ div >
28
+ < Form . Field error = { ! ! ( touched && error ) } required = { required } className = { styles . inputGroup } >
29
+ { label && < label > { label } </ label > }
30
+ { showAsyncValidating && asyncValidating && < i className = { `fa fa-cog fa-spin ${ styles . cog } ` } /> }
31
+ < InputComponent required = { required } { ...input } { ...rest } />
32
+ < div className = { styles . flags } >
33
+ { dirty && (
34
+ < span className = { styles . dirty } title = "Dirty" >
35
+ D
36
+ </ span >
37
+ ) }
38
+ { active && (
39
+ < span className = { styles . active } title = "Active" >
40
+ A
41
+ </ span >
42
+ ) }
43
+ { visited && (
44
+ < span className = { styles . visited } title = "Visited" >
45
+ V
46
+ </ span >
47
+ ) }
48
+ { touched && (
49
+ < span className = { styles . touched } title = "Touched" >
50
+ T
51
+ </ span >
52
+ ) }
54
53
</ div >
55
- </ div >
54
+ { touched && error ? (
55
+ < Label basic color = "red" pointing >
56
+ { error }
57
+ </ Label >
58
+ ) : null }
59
+ </ Form . Field >
56
60
) ;
57
61
58
62
Input . propTypes = fieldPropTypes ;
@@ -64,7 +68,7 @@ Input.propTypes = fieldPropTypes;
64
68
asyncBlurFields : [ 'email' ]
65
69
} )
66
70
@connect ( state => ( {
67
- active : state . form . survey . active
71
+ active : state . form . survey ? state . form . survey . active : ''
68
72
} ) )
69
73
export default class SurveyForm extends Component {
70
74
static propTypes = {
@@ -90,8 +94,8 @@ export default class SurveyForm extends Component {
90
94
91
95
return (
92
96
< div >
93
- < form className = "form-horizontal" onSubmit = { handleSubmit } >
94
- < Field name = "name" type = "text" component = { Input } label = "Full Name" className = "form-control" styles = { styles } />
97
+ < Form onSubmit = { handleSubmit } >
98
+ < Field name = "name" type = "text" component = { Input } label = "Full Name" styles = { styles } />
95
99
96
100
< Field
97
101
name = "email"
@@ -112,66 +116,44 @@ export default class SurveyForm extends Component {
112
116
styles = { styles }
113
117
/>
114
118
115
- < Field
116
- name = "currentlyEmployed"
117
- type = "checkbox"
118
- component = { Input }
119
- label = "Currently Employed?"
120
- styles = { styles }
121
- />
122
-
123
- < div className = "form-group" >
124
- < label htmlFor = "sex" className = "col-sm-2" >
125
- Sex
126
- </ label >
127
- < div className = "col-sm-8" >
128
- < label htmlFor = "sex-male" className = { styles . radioLabel } >
129
- < Field name = "sex" component = "input" type = "radio" id = "sex-male" value = "male" /> Male
130
- </ label >
131
- < label htmlFor = "sex-female" className = { styles . radioLabel } >
132
- < Field name = "sex" component = "input" type = "radio" id = "sex-female" value = "female" /> Female
133
- </ label >
134
- </ div >
135
- </ div >
119
+ < Field name = "currentlyEmployed" component = { CheckboxField } label = "Currently Employed?" styles = { styles } />
136
120
137
- < div className = "form-group" >
138
- < div className = "col-sm-offset-2 col-sm-10" >
139
- < button className = "btn btn-success" onClick = { handleSubmit } >
140
- < i className = "fa fa-paper-plane" /> Submit
141
- </ button >
142
- < button className = "btn btn-warning" type = "button" onClick = { reset } style = { { marginLeft : 15 } } >
143
- < i className = "fa fa-undo" /> Reset
144
- </ button >
145
- </ div >
121
+ < div >
122
+ < Button positive onClick = { handleSubmit } >
123
+ Submit
124
+ </ Button >
125
+ < Button as = "a" negative onClick = { reset } >
126
+ Reset
127
+ </ Button >
146
128
</ div >
147
- </ form >
129
+ </ Form >
148
130
149
131
< h4 > Props from redux-form</ h4 >
150
132
151
- < table className = "table table-striped" >
152
- < tbody >
153
- < tr >
154
- < th > Active Field</ th >
155
- < td > { active } </ td >
156
- </ tr >
157
- < tr >
158
- < th > Dirty</ th >
159
- < td className = { dirty ? 'success ' : 'danger ' } > { dirty ? 'true' : 'false' } </ td >
160
- </ tr >
161
- < tr >
162
- < th > Pristine</ th >
163
- < td className = { pristine ? 'success ' : 'danger ' } > { pristine ? 'true' : 'false' } </ td >
164
- </ tr >
165
- < tr >
166
- < th > Valid</ th >
167
- < td className = { valid ? 'success ' : 'danger ' } > { valid ? 'true' : 'false' } </ td >
168
- </ tr >
169
- < tr >
170
- < th > Invalid</ th >
171
- < td className = { invalid ? 'success ' : 'danger ' } > { invalid ? 'true' : 'false' } </ td >
172
- </ tr >
173
- </ tbody >
174
- </ table >
133
+ < Table celled >
134
+ < Table . Body >
135
+ < Table . Row >
136
+ < Table . Cell > Active Field</ Table . Cell >
137
+ < Table . Cell > { active } </ Table . Cell >
138
+ </ Table . Row >
139
+ < Table . Row >
140
+ < Table . Cell > Dirty</ Table . Cell >
141
+ < Table . Cell className = { dirty ? 'positive ' : 'negative ' } > { dirty ? 'true' : 'false' } </ Table . Cell >
142
+ </ Table . Row >
143
+ < Table . Row >
144
+ < Table . Cell > Pristine</ Table . Cell >
145
+ < Table . Cell className = { pristine ? 'positive ' : 'negative ' } > { pristine ? 'true' : 'false' } </ Table . Cell >
146
+ </ Table . Row >
147
+ < Table . Row >
148
+ < Table . Cell > Valid</ Table . Cell >
149
+ < Table . Cell className = { valid ? 'positive ' : 'negative ' } > { valid ? 'true' : 'false' } </ Table . Cell >
150
+ </ Table . Row >
151
+ < Table . Row >
152
+ < Table . Cell > Invalid</ Table . Cell >
153
+ < Table . Cell className = { invalid ? 'positive ' : 'negative ' } > { invalid ? 'true' : 'false' } </ Table . Cell >
154
+ </ Table . Row >
155
+ </ Table . Body >
156
+ </ Table >
175
157
</ div >
176
158
) ;
177
159
}
0 commit comments