File tree 2 files changed +29
-4
lines changed
Libraries/Components/TextInput
2 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -73,7 +73,7 @@ function blurField(textFieldID: ?number) {
73
73
/**
74
74
* @param {number } TextInputID id of the text field to focus
75
75
* Focuses the specified text field
76
- * noop if the text field was already focused
76
+ * noop if the text field was already focused or if the field is not editable
77
77
*/
78
78
function focusTextInput ( textField : ?ComponentRef ) {
79
79
if ( typeof textField === 'number' ) {
@@ -86,7 +86,15 @@ function focusTextInput(textField: ?ComponentRef) {
86
86
return ;
87
87
}
88
88
89
- if ( currentlyFocusedInputRef !== textField && textField != null ) {
89
+ if ( textField != null ) {
90
+ const fieldCanBeFocused =
91
+ currentlyFocusedInputRef !== textField &&
92
+ // $FlowFixMe - `currentProps` is missing in `NativeMethods`
93
+ textField . currentProps ?. editable !== false ;
94
+
95
+ if ( ! fieldCanBeFocused ) {
96
+ return ;
97
+ }
90
98
focusInput ( textField ) ;
91
99
if ( Platform . OS === 'ios' ) {
92
100
// This isn't necessarily a single line text input
Original file line number Diff line number Diff line change @@ -79,9 +79,26 @@ describe('TextInput tests', () => {
79
79
} ) ;
80
80
} ) ;
81
81
82
- it ( 'should have support being focused and unfocused' , ( ) => {
82
+ function createTextInput ( extraProps ) {
83
83
const textInputRef = React . createRef ( null ) ;
84
- ReactTestRenderer . create ( < TextInput ref = { textInputRef } value = "value1" /> ) ;
84
+ ReactTestRenderer . create (
85
+ < TextInput ref = { textInputRef } value = "value1" { ...extraProps } /> ,
86
+ ) ;
87
+ return textInputRef ;
88
+ }
89
+
90
+ it ( 'focus() should not do anything if the TextInput is not editable' , ( ) => {
91
+ const textInputRef = createTextInput ( { editable : false } ) ;
92
+ // currentProps is the property actually containing props at runtime
93
+ textInputRef . current . currentProps = textInputRef . current . props ;
94
+ expect ( textInputRef . current . isFocused ( ) ) . toBe ( false ) ;
95
+
96
+ TextInput . State . focusTextInput ( textInputRef . current ) ;
97
+ expect ( textInputRef . current . isFocused ( ) ) . toBe ( false ) ;
98
+ } ) ;
99
+
100
+ it ( 'should have support for being focused and blurred' , ( ) => {
101
+ const textInputRef = createTextInput ( ) ;
85
102
86
103
expect ( textInputRef . current . isFocused ( ) ) . toBe ( false ) ;
87
104
ReactNative . findNodeHandle = jest . fn ( ) . mockImplementation ( ref => {
You can’t perform that action at this time.
0 commit comments