diff --git a/packages/react-native/Libraries/Components/TextInput/TextInput.js b/packages/react-native/Libraries/Components/TextInput/TextInput.js index 5203cf36e87..ee48b398c12 100644 --- a/packages/react-native/Libraries/Components/TextInput/TextInput.js +++ b/packages/react-native/Libraries/Components/TextInput/TextInput.js @@ -302,11 +302,11 @@ function InternalTextInput(props: TextInputProps): React.Node { TextInputState.registerInput(inputRefValue); return () => { - TextInputState.unregisterInput(inputRefValue); - if (TextInputState.currentlyFocusedInput() === inputRefValue) { nullthrows(inputRefValue).blur(); } + + TextInputState.unregisterInput(inputRefValue); }; } }, []); diff --git a/packages/react-native/Libraries/Components/TextInput/__tests__/TextInput-itest.js b/packages/react-native/Libraries/Components/TextInput/__tests__/TextInput-itest.js index d7f1636c2cd..1fd3bfe4280 100644 --- a/packages/react-native/Libraries/Components/TextInput/__tests__/TextInput-itest.js +++ b/packages/react-native/Libraries/Components/TextInput/__tests__/TextInput-itest.js @@ -597,6 +597,33 @@ describe('', () => { 'Command {type: "AndroidTextInput", nativeID: "text-input", name: "blur"}', ]); }); + + it('dispatches the blur command when a focused input is unmounted', () => { + const root = Fantom.createRoot(); + const ref = createRef(); + + Fantom.runTask(() => { + root.render(); + }); + + const instance = nullthrows(ref.current); + + Fantom.runTask(() => { + instance.focus(); + }); + + root.takeMountingManagerLogs(); + + Fantom.runTask(() => { + // unmount TextInput + root.render(<>); + }); + + expect(root.takeMountingManagerLogs()).toContain( + 'Command {type: "AndroidTextInput", nativeID: "text-input", name: "blur"}', + ); + expect(TextInput.State.currentlyFocusedInput()).toBe(null); + }); }); describe('clear()', () => {