Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Libraries/Components/TextInput/TextInputState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 is the field is not editable
*/
function focusTextInput(textField: ?ComponentRef) {
if (typeof textField === 'number') {
Expand All @@ -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
Expand Down
21 changes: 19 additions & 2 deletions Libraries/Components/TextInput/__tests__/TextInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(<TextInput ref={textInputRef} value="value1" />);
ReactTestRenderer.create(
<TextInput ref={textInputRef} value="value1" {...extraProps} />,
);
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 => {
Expand Down