From 242a1ae35caf36ae0fb5006456e8a204ce399ce5 Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Sat, 11 Jul 2020 11:06:35 +0200 Subject: [PATCH 1/2] fix focus --- Libraries/Components/TextInput/TextInput.js | 10 +++++++ .../TextInput/__tests__/TextInput-test.js | 28 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index 4e781fb66bcf..51e4c87ef915 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -1063,6 +1063,15 @@ function InternalTextInput(props: Props): React.Node { return TextInputState.currentlyFocusedInput() === inputRef.current; } + function focus(): void { + const {current} = inputRef; + if (props.editable === false || current === null) { + return; + } + // $FlowFixMe - `focus` is missing in `$ReadOnly` + Object.getPrototypeOf(current)?.focus?.call(current); + } + function getNativeRef(): ?React.ElementRef> { return inputRef.current; } @@ -1096,6 +1105,7 @@ function InternalTextInput(props: Props): React.Node { */ if (ref) { ref.clear = clear; + ref.focus = focus; ref.isFocused = isFocused; ref.getNativeRef = getNativeRef; ref.setSelection = setSelection; diff --git a/Libraries/Components/TextInput/__tests__/TextInput-test.js b/Libraries/Components/TextInput/__tests__/TextInput-test.js index 9704a1a2c5f2..496a7a650ea2 100644 --- a/Libraries/Components/TextInput/__tests__/TextInput-test.js +++ b/Libraries/Components/TextInput/__tests__/TextInput-test.js @@ -55,7 +55,7 @@ describe('TextInput tests', () => { it('has expected instance functions', () => { expect(inputRef.current.isFocused).toBeInstanceOf(Function); // Would have prevented S168585 expect(inputRef.current.clear).toBeInstanceOf(Function); - expect(inputRef.current.focus).toBeInstanceOf(jest.fn().constructor); + expect(inputRef.current.focus).toBeInstanceOf(Function); expect(inputRef.current.blur).toBeInstanceOf(jest.fn().constructor); expect(inputRef.current.setNativeProps).toBeInstanceOf( jest.fn().constructor, @@ -108,6 +108,32 @@ describe('TextInput tests', () => { expect(TextInput.State.currentlyFocusedInput()).toBe(null); }); + describe('focus()', () => { + function createTextInput(extraProps) { + const textInputRef = React.createRef(null); + ReactTestRenderer.create( + , + ); + const {current} = textInputRef; + const mockComponent = Object.getPrototypeOf(current); + mockComponent.focus = jest.fn(); + return {ref: current, mockComponent}; + } + + it('should call focus() up on the prototype (ReactNativeFiberHostComponent on device / mockComponent.js in tests)', () => { + const {ref, mockComponent} = createTextInput(); + ref.focus(); + + expect(mockComponent.focus).toHaveBeenCalled(); + }); + it('should not do anything if the TextInput is not editable', () => { + const {ref, mockComponent} = createTextInput({editable: false}); + ref.focus(); + + expect(mockComponent.focus).not.toHaveBeenCalled(); + }); + }); + it('should unfocus when other TextInput is focused', () => { const textInputRe1 = React.createRef(null); const textInputRe2 = React.createRef(null); From 9db3cf746c10ff47774f6a8ec3a859860249f8f6 Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Wed, 2 Mar 2022 23:03:11 +0700 Subject: [PATCH 2/2] update detection of focusability --- Libraries/Components/TextInput/TextInput.js | 10 ---- .../Components/TextInput/TextInputState.js | 12 ++++- .../TextInput/__tests__/TextInput-test.js | 49 ++++++++----------- 3 files changed, 30 insertions(+), 41 deletions(-) diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index 51e4c87ef915..4e781fb66bcf 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -1063,15 +1063,6 @@ function InternalTextInput(props: Props): React.Node { return TextInputState.currentlyFocusedInput() === inputRef.current; } - function focus(): void { - const {current} = inputRef; - if (props.editable === false || current === null) { - return; - } - // $FlowFixMe - `focus` is missing in `$ReadOnly` - Object.getPrototypeOf(current)?.focus?.call(current); - } - function getNativeRef(): ?React.ElementRef> { return inputRef.current; } @@ -1105,7 +1096,6 @@ function InternalTextInput(props: Props): React.Node { */ if (ref) { ref.clear = clear; - ref.focus = focus; ref.isFocused = isFocused; ref.getNativeRef = getNativeRef; ref.setSelection = setSelection; diff --git a/Libraries/Components/TextInput/TextInputState.js b/Libraries/Components/TextInput/TextInputState.js index fe9a1c89da7d..f6074935b767 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 is 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 496a7a650ea2..f63c82bebe6d 100644 --- a/Libraries/Components/TextInput/__tests__/TextInput-test.js +++ b/Libraries/Components/TextInput/__tests__/TextInput-test.js @@ -55,7 +55,7 @@ describe('TextInput tests', () => { it('has expected instance functions', () => { expect(inputRef.current.isFocused).toBeInstanceOf(Function); // Would have prevented S168585 expect(inputRef.current.clear).toBeInstanceOf(Function); - expect(inputRef.current.focus).toBeInstanceOf(Function); + expect(inputRef.current.focus).toBeInstanceOf(jest.fn().constructor); expect(inputRef.current.blur).toBeInstanceOf(jest.fn().constructor); expect(inputRef.current.setNativeProps).toBeInstanceOf( jest.fn().constructor, @@ -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 => { @@ -108,32 +125,6 @@ describe('TextInput tests', () => { expect(TextInput.State.currentlyFocusedInput()).toBe(null); }); - describe('focus()', () => { - function createTextInput(extraProps) { - const textInputRef = React.createRef(null); - ReactTestRenderer.create( - , - ); - const {current} = textInputRef; - const mockComponent = Object.getPrototypeOf(current); - mockComponent.focus = jest.fn(); - return {ref: current, mockComponent}; - } - - it('should call focus() up on the prototype (ReactNativeFiberHostComponent on device / mockComponent.js in tests)', () => { - const {ref, mockComponent} = createTextInput(); - ref.focus(); - - expect(mockComponent.focus).toHaveBeenCalled(); - }); - it('should not do anything if the TextInput is not editable', () => { - const {ref, mockComponent} = createTextInput({editable: false}); - ref.focus(); - - expect(mockComponent.focus).not.toHaveBeenCalled(); - }); - }); - it('should unfocus when other TextInput is focused', () => { const textInputRe1 = React.createRef(null); const textInputRe2 = React.createRef(null);