@@ -3,10 +3,16 @@ import Button from 'material-ui/Button';
3
3
import Divider from 'material-ui/Divider' ;
4
4
import TextField from 'material-ui/TextField' ;
5
5
import Typography from 'material-ui/Typography' ;
6
+ import MobileStepper from 'material-ui/MobileStepper' ;
6
7
import Card , { CardActions , CardContent } from 'material-ui/Card' ;
8
+ import KeyboardArrowLeft from 'material-ui-icons/KeyboardArrowLeft' ;
9
+ import KeyboardArrowRight from 'material-ui-icons/KeyboardArrowRight' ;
7
10
import withStyles , { WithStyles , StyleRulesCallback } from 'material-ui/styles/withStyles' ;
8
11
12
+ import { AvatarSelector } from '../' ;
13
+
9
14
export class SignUpData {
15
+ Avatar = '0.jpg' ;
10
16
Login : string ;
11
17
Password : string ;
12
18
RepeatPassword : string ;
@@ -20,6 +26,7 @@ interface SignUpProps {
20
26
type SignUpFormProps = SignUpProps & WithStyles < 'root' | 'Card' | 'Typography' > ;
21
27
22
28
interface SignUpFormState {
29
+ ActiveStep : number ;
23
30
LoginError : string ;
24
31
PasswordError : string ;
25
32
RepeatPasswordError : string ;
@@ -32,6 +39,7 @@ const styles: StyleRulesCallback<'root'> = () => ({
32
39
} ) ;
33
40
34
41
class SignUpForm extends React . PureComponent < SignUpFormProps , SignUpFormState > {
42
+ private readonly stepsCount = 2 ;
35
43
private signUpData = new SignUpData ( ) ;
36
44
private get valid ( ) {
37
45
return ! this . state . LoginError && this . signUpData . Login
@@ -43,13 +51,16 @@ class SignUpForm extends React.PureComponent<SignUpFormProps, SignUpFormState> {
43
51
super ( props ) ;
44
52
45
53
this . state = {
54
+ ActiveStep : 0 ,
46
55
LoginError : '' ,
47
56
PasswordError : '' ,
48
57
RepeatPasswordError : ''
49
58
} ;
50
59
51
- this . handleSubmit = this . handleSubmit . bind ( this ) ;
52
60
this . handleChange = this . handleChange . bind ( this ) ;
61
+ this . handleStep = this . handleStep . bind ( this ) ;
62
+ this . handleSubmit = this . handleSubmit . bind ( this ) ;
63
+ this . onPickAvatar = this . onPickAvatar . bind ( this ) ;
53
64
}
54
65
55
66
handleChange ( event : React . ChangeEvent < HTMLInputElement > ) {
@@ -60,78 +71,139 @@ class SignUpForm extends React.PureComponent<SignUpFormProps, SignUpFormState> {
60
71
this . validate ( name , value ) ;
61
72
}
62
73
74
+ handleStep ( e : React . MouseEvent < HTMLFormElement > ) {
75
+ const initialState = {
76
+ LoginError : '' ,
77
+ PasswordError : '' ,
78
+ RepeatPasswordError : ''
79
+ } ;
80
+ if ( e . currentTarget . name === 'next' ) {
81
+ this . setState ( ( prevState ) => ( { ...initialState , ActiveStep : prevState . ActiveStep + 1 } ) ) ;
82
+ } else {
83
+ this . setState ( ( prevState ) => {
84
+ if ( prevState . ActiveStep === 1 ) {
85
+ this . signUpData . Login = '' ;
86
+ this . signUpData . Password = '' ;
87
+ this . signUpData . RepeatPassword = '' ;
88
+ }
89
+ return { ...initialState , ActiveStep : prevState . ActiveStep - 1 } ;
90
+ } ) ;
91
+ }
92
+ }
93
+
63
94
handleSubmit ( event : React . FormEvent < HTMLFormElement > ) {
64
95
event . preventDefault ( ) ;
65
96
const signUpData = { ...this . signUpData } ;
66
97
delete signUpData . RepeatPassword ;
67
98
this . props . onSubmit ( signUpData ) ;
68
99
}
69
100
70
- render ( ) {
101
+ getFormBody ( ) {
102
+ switch ( this . state . ActiveStep ) {
103
+ case 0 : return this . getStep0 ( ) ;
104
+ case 1 : return this . getStep1 ( ) ;
105
+ default : throw 'NullReferenceException' ;
106
+ }
107
+ }
108
+
109
+ getStep0 ( ) {
71
110
const { LoginError, PasswordError, RepeatPasswordError } = this . state ;
111
+
72
112
return (
73
- < div className = { this . props . classes . root } >
74
- < Card className = { 'SignUpForm ' + this . props . classes . Card } >
113
+ < React . Fragment >
114
+ < TextField
115
+ error = { ! ! LoginError }
116
+ helperText = { LoginError ? LoginError : 'Enter your Login' }
117
+ label = "Login"
118
+ name = "Login"
119
+ fullWidth = { true }
120
+ onChange = { this . handleChange }
121
+ />
122
+ < br />
123
+ < br />
124
+ < TextField
125
+ error = { ! ! PasswordError }
126
+ helperText = { PasswordError ? PasswordError : 'Enter your Password' }
127
+ label = "Password"
128
+ name = "Password"
129
+ type = "password"
130
+ fullWidth = { true }
131
+ onChange = { this . handleChange }
132
+ />
133
+ < br />
134
+ < br />
135
+ < TextField
136
+ error = { ! ! RepeatPasswordError }
137
+ helperText = { RepeatPasswordError ? RepeatPasswordError : 'Repeat your Password' }
138
+ label = "Repeat Password"
139
+ name = "RepeatPassword"
140
+ type = "password"
141
+ fullWidth = { true }
142
+ onChange = { this . handleChange }
143
+ />
144
+ </ React . Fragment >
145
+ ) ;
146
+ }
147
+
148
+ getStep1 ( ) {
149
+ return (
150
+ < React . Fragment >
151
+ < AvatarSelector onPickAvatar = { this . onPickAvatar } />
152
+ < Button variant = { 'raised' } type = "submit" color = "primary" fullWidth = { true } disabled = { ! this . valid } >
153
+ Register
154
+ </ Button >
155
+ </ React . Fragment >
156
+ ) ;
157
+ }
158
+
159
+ onPickAvatar ( avatar : string ) {
160
+ this . signUpData . Avatar = avatar ;
161
+ }
162
+
163
+ render ( ) {
164
+ const { ActiveStep } = this . state ;
165
+ const { classes, errors } = this . props ;
166
+ const backButton = (
167
+ < Button size = "small" onClick = { this . handleStep } disabled = { this . state . ActiveStep === 0 } name = "back" >
168
+ < KeyboardArrowLeft />
169
+ Back
170
+ </ Button >
171
+ ) ;
172
+ const nextButton = (
173
+ < Button size = "small" onClick = { this . handleStep } disabled = { ActiveStep === this . stepsCount - 1 } name = "next" >
174
+ Next
175
+ < KeyboardArrowRight />
176
+ </ Button >
177
+ ) ;
178
+
179
+ return (
180
+ < div className = { classes . root } >
181
+ < Card className = { 'SignUpForm ' + classes . Card } >
75
182
< CardContent >
76
- < Typography component = "h2" variant = "headline" className = { this . props . classes . Typography } >
183
+ < Typography component = "h2" variant = "headline" className = { classes . Typography } >
77
184
Sign Up
78
185
</ Typography >
79
- { this . props . errors && this . props . errors
186
+ < Typography className = { classes . Typography } >
187
+ Step { ActiveStep + 1 } of { this . stepsCount }
188
+ </ Typography >
189
+ < Divider />
190
+ { errors && errors
80
191
. map ( ( error : string ) => < p key = { error } > { error } </ p > ) }
81
192
< form onSubmit = { this . handleSubmit } >
82
- < TextField
83
- error = { ! ! LoginError }
84
- helperText = { LoginError ? LoginError : 'Enter your Login' }
85
- label = "Login"
86
- name = "Login"
87
- fullWidth = { true }
88
- onChange = { this . handleChange }
89
- />
90
- < br />
91
- < br />
92
- < TextField
93
- error = { ! ! PasswordError }
94
- helperText = { PasswordError ? PasswordError : 'Enter your Password' }
95
- label = "Password"
96
- name = "Password"
97
- type = "password"
98
- fullWidth = { true }
99
- onChange = { this . handleChange }
193
+ < MobileStepper
194
+ variant = "text"
195
+ steps = { this . stepsCount }
196
+ position = "static"
197
+ activeStep = { ActiveStep }
198
+ nextButton = { ( ActiveStep + 1 < this . stepsCount ) ? nextButton : < React . Fragment /> }
199
+ backButton = { backButton }
100
200
/>
101
- < br />
102
- < br />
103
- < TextField
104
- error = { ! ! RepeatPasswordError }
105
- helperText = { RepeatPasswordError ? RepeatPasswordError : 'Repeat your Password' }
106
- label = "Repeat Password"
107
- name = "RepeatPassword"
108
- type = "password"
109
- fullWidth = { true }
110
- onChange = { this . handleChange }
111
- />
112
- < br />
113
- < br />
114
- < Button
115
- variant = { 'raised' }
116
- type = "submit"
117
- color = "primary"
118
- fullWidth = { true }
119
- disabled = { ! this . valid }
120
- >
121
- Register
122
- </ Button >
201
+ { this . getFormBody ( ) }
123
202
</ form >
124
- < br />
125
- < Divider />
126
203
</ CardContent >
127
204
< CardActions >
128
205
< a href = "/" >
129
- < Button
130
- variant = { 'raised' }
131
- color = "default"
132
- >
133
- Sign In
134
- </ Button >
206
+ < Button variant = { 'raised' } color = "default" > Sign In</ Button >
135
207
</ a >
136
208
</ CardActions >
137
209
</ Card >
@@ -161,6 +233,8 @@ class SignUpForm extends React.PureComponent<SignUpFormProps, SignUpFormState> {
161
233
PasswordError = 'Password must be a minimum of 8 characters' ;
162
234
} else if ( value . length > 249 ) {
163
235
PasswordError = 'Password is too long' ;
236
+ } else if ( value !== this . signUpData . RepeatPassword ) {
237
+ PasswordError = 'These passwords don\'t match' ;
164
238
}
165
239
newState = { PasswordError} ;
166
240
break ;
@@ -172,6 +246,7 @@ class SignUpForm extends React.PureComponent<SignUpFormProps, SignUpFormState> {
172
246
}
173
247
174
248
if ( this . state . RepeatPasswordError === RepeatPasswordError ) {
249
+ newState = { PasswordError : '' } ;
175
250
this . forceUpdate ( ) ;
176
251
} else {
177
252
newState = { RepeatPasswordError} ;
0 commit comments