Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ const definitions: FeatureFlagDefinitions = {
ossReleaseStage: 'none',
},
enableImperativeFocus: {
defaultValue: true, // [macOS]
defaultValue: false,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we come up with a way to override these in our fork more easily

metadata: {
dateAdded: '2024-09-13',
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -144,15 +145,21 @@ 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);
Comment on lines +148 to 152

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This very much feels like th wrong place to set this. Can we not just override the default in the feature flags, even if we have to touch a bunch of files?

}
}

focus() {
if (TextInputState.isTextInput(this)) {
TextInputState.focusTextInput(this);
} else if (ReactNativeFeatureFlags.enableImperativeFocus()) {
} else if (
Platform.OS === 'macos' || // [macOS]
ReactNativeFeatureFlags.enableImperativeFocus()
) {
ViewCommands.focus(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => ({}));
Comment on lines +11 to +28

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we the macOS fork should be responsible for adding these tests. Especially ones that seem to test nothing macOS specific.

https://deepwiki.com/mattpocock/skills/4.2-test-driven-development


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();
},
);
});
Loading