1
1
import * as React from 'react' ;
2
2
import { connect } from 'react-redux' ;
3
3
import MetaAction from '../store/meta/MetaAction' ;
4
- import { Field , FormProps , reduxForm } from 'redux-form' ;
4
+ import { reduxForm , Field , FormProps , FormErrors } from 'redux-form' ;
5
5
import IStore from '../interfaces/IStore' ;
6
6
import { Dispatch } from 'redux' ;
7
+ import IContactForm from '../interfaces/IContactForm' ;
8
+ import IMetaReducerState from '../interfaces/reducers/IMetaReducerState' ;
7
9
8
10
const mapStateToProps = ( state : IStore ) => ( { } ) ;
9
11
10
12
const mapDispatchToProps = ( dispatch : Dispatch < any > ) => ( {
11
- setMeta : ( meta ) => dispatch ( MetaAction . setMeta ( meta ) ) ,
13
+ setMeta : ( meta : IMetaReducerState ) => dispatch ( MetaAction . setMeta ( meta ) ) ,
12
14
} ) ;
13
15
14
- interface IContactProps extends FormProp {
16
+ interface IContactProps extends FormProps {
15
17
}
16
18
17
- class Contact extends React . Component < IContactProps , void > {
19
+ class Contact extends React . Component {
18
20
19
- _handleSubmitHandler = ( formData ) => this . _onFormSubmit ( formData ) ;
21
+ private _handleSubmitHandler : Function = ( formData : IContactForm ) => this . _onFormSubmit ( formData ) ;
20
22
21
23
componentWillMount ( ) : void {
22
24
this . props . setMeta ( { title : 'Contact Page' } ) ;
@@ -121,13 +123,13 @@ class Contact extends React.Component<IContactProps, void> {
121
123
) ;
122
124
}
123
125
124
- _onFormSubmit ( formData ) {
126
+ private _onFormSubmit ( formData : IContactForm ) : void {
125
127
console . info ( formData ) ;
126
128
127
129
window . alert ( JSON . stringify ( formData , null , 2 ) ) ;
128
130
}
129
131
130
- _renderInputField ( field ) {
132
+ private _renderInputField ( field : any ) : JSX . Element {
131
133
const { meta : { touched, error} } = field ;
132
134
const className = `small text-danger ${ touched && error ? '' : 'd-none' } ` ;
133
135
@@ -147,7 +149,7 @@ class Contact extends React.Component<IContactProps, void> {
147
149
) ;
148
150
}
149
151
150
- _renderCheckbox ( field ) {
152
+ private _renderCheckbox ( field : any ) : JSX . Element {
151
153
return (
152
154
< label
153
155
className = "form-check-label"
@@ -163,7 +165,7 @@ class Contact extends React.Component<IContactProps, void> {
163
165
) ;
164
166
}
165
167
166
- _renderRadio ( field ) {
168
+ private _renderRadio ( field : any ) : JSX . Element {
167
169
return (
168
170
< div className = "form-check" >
169
171
< label htmlFor = { field . input . name } className = "form-check-label" >
@@ -183,7 +185,7 @@ class Contact extends React.Component<IContactProps, void> {
183
185
) ;
184
186
}
185
187
186
- _renderTextArea ( field ) {
188
+ private _renderTextArea ( field : any ) : JSX . Element {
187
189
const { meta : { touched, error} } = field ;
188
190
const className = `small text-danger ${ touched && error ? '' : 'd-none' } ` ;
189
191
@@ -203,7 +205,7 @@ class Contact extends React.Component<IContactProps, void> {
203
205
}
204
206
205
207
/* eslint-disable jsx-a11y/label-has-for */
206
- _renderSelect ( field ) {
208
+ private _renderSelect ( field : any ) : JSX . Element {
207
209
return (
208
210
< div >
209
211
< label htmlFor = { field . name } >
@@ -228,9 +230,9 @@ class Contact extends React.Component<IContactProps, void> {
228
230
229
231
export default reduxForm ( {
230
232
form : 'contactForm' ,
231
- validate : ( formData ) => {
232
- const errors = { } ;
233
- const validEmailRegex = / ^ [ A - Z 0 - 9 . _ % + - ] + @ [ A - Z 0 - 9 . - ] + \. [ A - Z ] { 2 , 4 } $ / i;
233
+ validate : ( formData : Readonly < IContactForm > ) => {
234
+ const errors : FormErrors < IContactForm > = { } ;
235
+ const validEmailRegex : RegExp = / ^ [ A - Z 0 - 9 . _ % + - ] + @ [ A - Z 0 - 9 . - ] + \. [ A - Z ] { 2 , 4 } $ / i;
234
236
235
237
if ( ! validEmailRegex . test ( formData . email ) ) {
236
238
errors . email = 'Invalid email address' ;
0 commit comments