6
6
7
7
import React from 'react' ;
8
8
import {
9
- Image ,
10
9
ImageProps ,
11
10
StyleProp ,
12
11
StyleSheet ,
@@ -31,7 +30,6 @@ import {
31
30
} from '../support/services' ;
32
31
import {
33
32
FlexStyleProps ,
34
- InputEndEditEvent ,
35
33
InputFocusEvent ,
36
34
} from '../support/typings' ;
37
35
@@ -59,6 +57,15 @@ export type InputProps = StyledComponentProps & TextInputProps & ComponentProps;
59
57
*
60
58
* @extends React.Component
61
59
*
60
+ * @method {() => void} focus - Requests focus for the given input or view. The exact behavior triggered
61
+ * will depend on the platform and type of view.
62
+ *
63
+ * @method {() => void} blur - Removes focus from an input or view. This is the opposite of `focus()`.
64
+ *
65
+ * @method {() => boolean} isFocused - Returns if the input is currently focused.
66
+ *
67
+ * @method {() => void} clear - Removes all text from the input.
68
+ *
62
69
* @property {boolean } disabled - Determines whether component is disabled.
63
70
* Default is `false`.
64
71
*
@@ -140,7 +147,23 @@ export class InputComponent extends React.Component<InputProps> {
140
147
141
148
static styledComponentName : string = 'Input' ;
142
149
143
- static Icon : React . ComponentClass < ImageProps > = Image ;
150
+ private textInputRef : React . RefObject < TextInput > = React . createRef ( ) ;
151
+
152
+ public focus = ( ) => {
153
+ this . textInputRef . current . focus ( ) ;
154
+ } ;
155
+
156
+ public blur = ( ) => {
157
+ this . textInputRef . current . blur ( ) ;
158
+ } ;
159
+
160
+ public isFocused = ( ) : boolean => {
161
+ return this . textInputRef . current . isFocused ( ) ;
162
+ } ;
163
+
164
+ public clear = ( ) => {
165
+ this . textInputRef . current . clear ( ) ;
166
+ } ;
144
167
145
168
private onFocus = ( event : InputFocusEvent ) => {
146
169
this . props . dispatch ( [ Interaction . FOCUSED ] ) ;
@@ -150,11 +173,11 @@ export class InputComponent extends React.Component<InputProps> {
150
173
}
151
174
} ;
152
175
153
- private onEndEditing = ( event : InputEndEditEvent ) => {
176
+ private onBlur = ( event : InputFocusEvent ) => {
154
177
this . props . dispatch ( [ ] ) ;
155
178
156
- if ( this . props . onEndEditing ) {
157
- this . props . onEndEditing ( event ) ;
179
+ if ( this . props . onBlur ) {
180
+ this . props . onBlur ( event ) ;
158
181
}
159
182
} ;
160
183
@@ -322,12 +345,13 @@ export class InputComponent extends React.Component<InputProps> {
322
345
{ labelElement }
323
346
< View style = { componentStyle . inputContainer } >
324
347
< TextInput
348
+ ref = { this . textInputRef }
325
349
{ ...restProps }
326
350
style = { componentStyle . text }
327
351
placeholderTextColor = { componentStyle . placeholder . color }
328
352
editable = { ! disabled }
329
353
onFocus = { this . onFocus }
330
- onEndEditing = { this . onEndEditing }
354
+ onBlur = { this . onBlur }
331
355
/>
332
356
{ iconElement }
333
357
</ View >
0 commit comments