Skip to content
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

Fixing setting focus when RCTView is not yet in window #1398

Merged
merged 7 commits into from
Sep 7, 2022
Merged
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
3 changes: 3 additions & 0 deletions React/Views/RCTView.m
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,9 @@ - (void)viewDidMoveToWindow
name:NSViewBoundsDidChangeNotification
object:[[self enclosingScrollView] contentView]];
}

[self reactViewDidMoveToWindow]; // TODO(macOS GH#1412)

[super viewDidMoveToWindow];
}

Expand Down
2 changes: 2 additions & 0 deletions React/Views/UIView+React.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
- (void)reactAddControllerToClosestParent:(UIViewController *)controller;
#endif // TODO(macOS GH#774)

- (void)reactViewDidMoveToWindow; // TODO(macOS GH#1412)

/**
* Focus manipulation.
*/
Expand Down
35 changes: 22 additions & 13 deletions React/Views/UIView+React.m
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,17 @@ - (void)reactAddControllerToClosestParent:(UIViewController *)controller
}
#endif // TODO(macOS GH#774)

/**
* Focus manipulation.
*/
- (BOOL)reactIsFocusNeeded
// [TODO(macOS GH#1412)
- (void)reactViewDidMoveToWindow
{
return [(NSNumber *)objc_getAssociatedObject(self, @selector(reactIsFocusNeeded)) boolValue];
[self reactFocusIfNeeded];
}
// TODO(macOS GH#1412)]

- (void)setReactIsFocusNeeded:(BOOL)isFocusNeeded
{
objc_setAssociatedObject(self, @selector(reactIsFocusNeeded), @(isFocusNeeded), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
/**
* Focus manipulation.
*/
static __weak RCTPlatformView *_pendingFocusView; // TODO(macOS GH#1412)

- (void)reactFocus
{
Expand All @@ -287,19 +286,23 @@ - (void)reactFocus
#else
if (![self becomeFirstResponder]) {
#endif // TODO(macOS GH#774)]
self.reactIsFocusNeeded = YES;
}
// [TODO(macOS GH#1412)
_pendingFocusView = self;
} else {
_pendingFocusView = nil;
}
// TODO(macOS GH#1412)]
}

- (void)reactFocusIfNeeded
{
if (self.reactIsFocusNeeded) {
if ([self isEqual:_pendingFocusView]) { // TODO(macOS GH#1412)
#if TARGET_OS_OSX // [TODO(macOS GH#774)
if ([[self window] makeFirstResponder:self]) {
#else
if ([self becomeFirstResponder]) {
#endif // TODO(macOS GH#774)]
self.reactIsFocusNeeded = NO;
_pendingFocusView = nil; // TODO(macOS GH#1412)
}
}
}
Expand All @@ -313,6 +316,12 @@ - (void)reactBlur
#else
[self resignFirstResponder];
#endif // TODO(macOS GH#774)]

// [TODO(macOS GH#1412)
if ([self isEqual:_pendingFocusView]) {
_pendingFocusView = nil;
}
// TODO(macOS GH#1412)]
}

#pragma mark - Layout
Expand Down
54 changes: 54 additions & 0 deletions packages/rn-tester/js/examples/FocusOnMount/FocusOnMount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
// [TODO(macOS GH #1412)
'use strict';

const React = require('react');
const ReactNative = require('react-native');
const {Button, findNodeHandle, UIManager} = ReactNative;

class FocusOnMountExample extends React.Component<{}> {
ref = React.createRef();

componentDidMount() {
if (this.ref.current) {
const commands = UIManager.getViewManagerConfig('RCTView').Commands;
if ('focus' in commands) {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this.ref.current),
UIManager.getViewManagerConfig('RCTView').Commands.focus,
undefined,
);
}
}
}

render() {
return (
<Button
ref={this.ref}
title="This button will be focsued on mount"
onPress={() => {}}
/>
);
}
}

exports.title = 'Focus On Mount';
exports.description = 'Example for focusing a component on mount';
exports.examples = [
{
title: 'FocusOnMountExample',
render: function (): React.Element<any> {
return <FocusOnMountExample />;
},
},
];
// TODO(macOS GH #1412)]
5 changes: 5 additions & 0 deletions packages/rn-tester/js/utils/RNTesterList.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ const Components: Array<RNTesterModuleInfo> = [
key: 'FocusEvents',
module: require('../examples/FocusEventsExample/FocusEventsExample'),
}, // ]TODO(OSS Candidate ISS#2710739)
// [TODO(macOS GH #1412)
{
key: 'FocusOnMount',
module: require('../examples/FocusOnMount/FocusOnMount'),
}, // ]TODO(macOS GH #1412)
{
key: 'KeyboardEvents',
module: require('../examples/KeyboardEventsExample/KeyboardEventsExample'),
Expand Down