diff --git a/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js b/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js index 286dfaf8cd43..18cc5cd3ad78 100644 --- a/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js +++ b/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js @@ -347,7 +347,7 @@ const definitions: FeatureFlagDefinitions = { ossReleaseStage: 'none', }, enableImperativeFocus: { - defaultValue: true, // [macOS] + defaultValue: false, metadata: { dateAdded: '2024-09-13', description: diff --git a/packages/react-native/src/private/webapis/dom/nodes/ReactNativeElement.js b/packages/react-native/src/private/webapis/dom/nodes/ReactNativeElement.js index 78ab5696f43c..2decde0dfdb5 100644 --- a/packages/react-native/src/private/webapis/dom/nodes/ReactNativeElement.js +++ b/packages/react-native/src/private/webapis/dom/nodes/ReactNativeElement.js @@ -28,6 +28,7 @@ import TextInputState from '../../../../../Libraries/Components/TextInput/TextIn import {Commands as ViewCommands} from '../../../../../Libraries/Components/View/ViewNativeComponent'; import {create as createAttributePayload} from '../../../../../Libraries/ReactNative/ReactFabricPublicInstance/ReactNativeAttributePayload'; import warnForStyleProps from '../../../../../Libraries/ReactNative/ReactFabricPublicInstance/warnForStyleProps'; +import Platform from '../../../../../Libraries/Utilities/Platform'; // [macOS] import * as ReactNativeFeatureFlags from '../../../featureflags/ReactNativeFeatureFlags'; import { getNativeElementReference, @@ -144,7 +145,10 @@ class ReactNativeElement extends ReadOnlyElement implements NativeMethods { blur(): void { if (TextInputState.isTextInput(this)) { TextInputState.blurTextInput(this); - } else if (ReactNativeFeatureFlags.enableImperativeFocus()) { + } else if ( + Platform.OS === 'macos' || // [macOS] + ReactNativeFeatureFlags.enableImperativeFocus() + ) { ViewCommands.blur(this); } } @@ -152,7 +156,10 @@ class ReactNativeElement extends ReadOnlyElement implements NativeMethods { focus() { if (TextInputState.isTextInput(this)) { TextInputState.focusTextInput(this); - } else if (ReactNativeFeatureFlags.enableImperativeFocus()) { + } else if ( + Platform.OS === 'macos' || // [macOS] + ReactNativeFeatureFlags.enableImperativeFocus() + ) { ViewCommands.focus(this); } } diff --git a/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-test.js b/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-test.js new file mode 100644 index 000000000000..4df931fbf3b5 --- /dev/null +++ b/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-test.js @@ -0,0 +1,94 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +// [macOS] + +import TextInputState from '../../../../../../Libraries/Components/TextInput/TextInputState'; +import {Commands as ViewCommands} from '../../../../../../Libraries/Components/View/ViewNativeComponent'; +import Platform from '../../../../../../Libraries/Utilities/Platform'; +import * as ReactNativeFeatureFlags from '../../../../featureflags/ReactNativeFeatureFlags'; +import ReactNativeElement from '../ReactNativeElement'; + +jest.mock( + '../../../../../../Libraries/Components/View/ViewNativeComponent', + () => ({ + Commands: { + blur: jest.fn(), + focus: jest.fn(), + }, + }), +); +jest.mock('../specs/NativeDOM', () => ({})); + +const originalPlatform = Platform.OS; + +describe.each(['focus', 'blur'])('ReactNativeElement.%s', command => { + let element: ReactNativeElement; + + beforeEach(() => { + element = Object.create(ReactNativeElement.prototype); + jest.clearAllMocks(); + }); + + afterEach(() => { + TextInputState.unregisterInput(element); + // $FlowExpectedError[incompatible-type] Restore the platform-specific OS literal. + Object.defineProperty(Platform, 'OS', {value: originalPlatform}); + jest.restoreAllMocks(); + }); + + it.each([ + ['macos', false, true], + ['ios', false, false], + ['android', false, false], + ['ios', true, true], + ['android', true, true], + ])( + 'dispatches on %s with enableImperativeFocus=%s: %s', + (platform, enabled, shouldDispatch) => { + Object.defineProperty(Platform, 'OS', {value: platform}); + jest + .spyOn(ReactNativeFeatureFlags, 'enableImperativeFocus') + .mockReturnValue(enabled); + + element[command](); + + if (shouldDispatch) { + expect(ViewCommands[command]).toHaveBeenCalledTimes(1); + expect(ViewCommands[command]).toHaveBeenCalledWith(element); + } else { + expect(ViewCommands[command]).not.toHaveBeenCalled(); + } + }, + ); + + it.each(['macos', 'ios', 'android'])( + 'keeps TextInput commands separate on %s', + platform => { + Object.defineProperty(Platform, 'OS', {value: platform}); + jest + .spyOn(ReactNativeFeatureFlags, 'enableImperativeFocus') + .mockReturnValue(true); + TextInputState.registerInput(element); + const textInputCommand = jest + .spyOn( + TextInputState, + command === 'focus' ? 'focusTextInput' : 'blurTextInput', + ) + .mockImplementation(() => {}); + + element[command](); + + expect(textInputCommand).toHaveBeenCalledTimes(1); + expect(textInputCommand).toHaveBeenCalledWith(element); + expect(ViewCommands[command]).not.toHaveBeenCalled(); + }, + ); +});