diff --git a/Libraries/Components/TextInput/TextInputState.js b/Libraries/Components/TextInput/TextInputState.js index fe9a1c89da..87cb3ccf78 100644 --- a/Libraries/Components/TextInput/TextInputState.js +++ b/Libraries/Components/TextInput/TextInputState.js @@ -73,7 +73,7 @@ function blurField(textFieldID: ?number) { /** * @param {number} TextInputID id of the text field to focus * Focuses the specified text field - * noop if the text field was already focused + * noop if the text field was already focused or if the field is not editable */ function focusTextInput(textField: ?ComponentRef) { if (typeof textField === 'number') { @@ -86,7 +86,15 @@ function focusTextInput(textField: ?ComponentRef) { return; } - if (currentlyFocusedInputRef !== textField && textField != null) { + if (textField != null) { + const fieldCanBeFocused = + currentlyFocusedInputRef !== textField && + // $FlowFixMe - `currentProps` is missing in `NativeMethods` + textField.currentProps?.editable !== false; + + if (!fieldCanBeFocused) { + return; + } focusInput(textField); if (Platform.OS === 'ios') { // This isn't necessarily a single line text input diff --git a/Libraries/Components/TextInput/__tests__/TextInput-test.js b/Libraries/Components/TextInput/__tests__/TextInput-test.js index 9704a1a2c5..f63c82bebe 100644 --- a/Libraries/Components/TextInput/__tests__/TextInput-test.js +++ b/Libraries/Components/TextInput/__tests__/TextInput-test.js @@ -79,9 +79,26 @@ describe('TextInput tests', () => { }); }); - it('should have support being focused and unfocused', () => { + function createTextInput(extraProps) { const textInputRef = React.createRef(null); - ReactTestRenderer.create(); + ReactTestRenderer.create( + , + ); + return textInputRef; + } + + it('focus() should not do anything if the TextInput is not editable', () => { + const textInputRef = createTextInput({editable: false}); + // currentProps is the property actually containing props at runtime + textInputRef.current.currentProps = textInputRef.current.props; + expect(textInputRef.current.isFocused()).toBe(false); + + TextInput.State.focusTextInput(textInputRef.current); + expect(textInputRef.current.isFocused()).toBe(false); + }); + + it('should have support for being focused and blurred', () => { + const textInputRef = createTextInput(); expect(textInputRef.current.isFocused()).toBe(false); ReactNative.findNodeHandle = jest.fn().mockImplementation(ref => {