Repository navigation
fix(focus): enable imperative view refs on macOS #3171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
|
|
||
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| }, | ||
| ); | ||
| }); | ||
There was a problem hiding this comment.
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