1
- // @ts -nocheck
2
1
import _ from 'lodash' ;
3
2
import PropTypes from 'prop-types' ;
4
- import 'react' ;
5
- import { ViewPropTypes , TextInput as RNTextInput } from 'react-native' ;
6
- import { Colors , Typography } from '../../style' ;
7
- import { BaseComponent } from '../../commons' ;
3
+ import { Component } from 'react' ;
4
+ import { ViewPropTypes , TextInput as RNTextInput , StyleProp , ViewStyle , TextInputProps } from 'react-native' ;
8
5
import Validators from './Validators' ;
6
+ import { Colors , Typography } from '../../style' ;
7
+ import * as Modifiers from '../../commons/modifiers' ;
9
8
10
9
const VALIDATORS = {
11
10
REQUIRED : 'required' ,
@@ -15,10 +14,50 @@ const VALIDATORS = {
15
14
PRICE : 'price'
16
15
} ;
17
16
18
- export default class BaseInput extends BaseComponent {
17
+ interface BaseInputProps extends TextInputProps {
18
+ /**
19
+ * text color
20
+ */
21
+ color : string ;
22
+ /**
23
+ * text input container style
24
+ */
25
+ containerStyle : StyleProp < ViewStyle > ;
26
+ /**
27
+ * validator type or custom validator function
28
+ */
29
+ validate : string | Function | ( string | Function ) [ ] ;
30
+ /**
31
+ * Whether to mark required field with an asterisk
32
+ */
33
+ markRequired : boolean ;
34
+ /**
35
+ * the message to be displayed when the validation fails
36
+ */
37
+ errorMessage : string | string [ ] ;
38
+ /**
39
+ * whether to run the validation on mount
40
+ */
41
+ validateOnStart : boolean ;
42
+ /**
43
+ * whether to run the validation on text changed
44
+ */
45
+ validateOnChange : boolean ;
46
+ /**
47
+ * whether to run the validation on blur
48
+ */
49
+ validateOnBlur : boolean ;
50
+ /**
51
+ * callback for validity change
52
+ */
53
+ onChangeValidity : Function ;
54
+ }
55
+
56
+ export default class BaseInput extends Component < BaseInputProps > {
19
57
static displayName = 'BaseInput' ;
20
58
21
59
static propTypes = {
60
+ // @ts -expect-error
22
61
...RNTextInput . propTypes ,
23
62
// ...BaseComponent.propTypes,
24
63
/**
@@ -67,17 +106,12 @@ export default class BaseInput extends BaseComponent {
67
106
validateOnBlur : true
68
107
} ;
69
108
70
- constructor ( props ) {
71
- super ( props ) ;
72
-
73
- this . state = {
74
- ...this . state ,
75
- value : props . value ,
76
- focused : false ,
77
- valid : false ,
78
- error : undefined
79
- } ;
80
- }
109
+ state = {
110
+ value : this . props . value ,
111
+ focused : false ,
112
+ valid : false ,
113
+ error : undefined
114
+ } ;
81
115
82
116
componentDidMount ( ) {
83
117
const { validateOnStart} = this . props ;
@@ -87,12 +121,12 @@ export default class BaseInput extends BaseComponent {
87
121
}
88
122
89
123
/** Events */
90
- onFocus = ( ...args ) => {
124
+ onFocus = ( ...args : any ) => {
91
125
_ . invoke ( this . props , 'onFocus' , ...args ) ;
92
126
this . setState ( { focused : true } ) ;
93
127
} ;
94
128
95
- onBlur = ( ...args ) => {
129
+ onBlur = ( ...args : any ) => {
96
130
_ . invoke ( this . props , 'onBlur' , ...args ) ;
97
131
this . setState ( { focused : false } ) ;
98
132
@@ -102,11 +136,11 @@ export default class BaseInput extends BaseComponent {
102
136
}
103
137
} ;
104
138
105
- onChange = event => {
139
+ onChange = ( event : any ) => {
106
140
_ . invoke ( this . props , 'onChange' , event ) ;
107
141
} ;
108
142
109
- onChangeText = text => {
143
+ onChangeText = ( text : string ) => {
110
144
_ . invoke ( this . props , 'onChangeText' , text ) ;
111
145
this . setState ( { value : text } ) ;
112
146
@@ -118,7 +152,8 @@ export default class BaseInput extends BaseComponent {
118
152
119
153
/** Actions */
120
154
getTypography ( ) {
121
- return this . extractTypographyValue ( ) || Typography . text70 ;
155
+ Modifiers ;
156
+ return Modifiers . extractTypographyValue ( this . props ) || Typography . text70 ;
122
157
}
123
158
124
159
hasText ( ) {
@@ -127,22 +162,27 @@ export default class BaseInput extends BaseComponent {
127
162
}
128
163
129
164
isFocused ( ) {
165
+ // @ts -expect-error
130
166
return this . input . isFocused ( ) ;
131
167
}
132
168
133
169
focus ( ) {
170
+ // @ts -expect-error
134
171
this . input . focus ( ) ;
135
172
}
136
173
137
174
blur ( ) {
175
+ // @ts -expect-error
138
176
this . input . blur ( ) ;
139
177
}
140
178
141
179
clear ( ) {
180
+ // @ts -expect-error
142
181
this . input . clear ( ) ;
143
182
}
144
183
145
- validate = ( value = _ . get ( this , 'state.value' ) , dryRun ) => {
184
+ // @ts -expect-error
185
+ validate = ( value = _ . get ( this , 'state.value' ) , dryRun : boolean ) => {
146
186
// 'input.state.value'
147
187
const { validate} = this . props ;
148
188
if ( ! validate ) {
@@ -158,7 +198,9 @@ export default class BaseInput extends BaseComponent {
158
198
let validatorFunction ;
159
199
if ( _ . isFunction ( validator ) ) {
160
200
validatorFunction = validator ;
201
+ // @ts -expect-error
161
202
} else if ( _ . isString ( validator ) && ! ! Validators [ validator ] ) {
203
+ // @ts -expect-error
162
204
validatorFunction = Validators [ validator ] ;
163
205
}
164
206
@@ -175,6 +217,7 @@ export default class BaseInput extends BaseComponent {
175
217
if ( ! isValid ) {
176
218
const { errorMessage} = this . props ;
177
219
if ( _ . isArray ( errorMessage ) ) {
220
+ // @ts -expect-error
178
221
error = errorMessage [ failingValidatorIndex ] ;
179
222
} else {
180
223
error = errorMessage ;
@@ -203,8 +246,8 @@ export default class BaseInput extends BaseComponent {
203
246
return validate === VALIDATORS . REQUIRED ;
204
247
}
205
248
206
- getRequiredPlaceholder ( placeholder ) {
207
- const { markRequired} = this . getThemeProps ( ) ;
249
+ getRequiredPlaceholder ( placeholder : string ) {
250
+ const { markRequired} = this . props ;
208
251
const shouldDisplayPlaceholderAsRequired = this . isRequiredField ( ) && markRequired && placeholder ;
209
252
210
253
if ( shouldDisplayPlaceholderAsRequired ) {
@@ -214,6 +257,7 @@ export default class BaseInput extends BaseComponent {
214
257
}
215
258
216
259
getErrorMessage ( ) {
260
+ // @ts -expect-error
217
261
const { error : propsError } = this . props ;
218
262
const { error : stateError } = this . state ;
219
263
0 commit comments