Skip to content

Commit

Permalink
Better TextInput: Simplified focus/first-responder management on iOS
Browse files Browse the repository at this point in the history
Summary:
Pair `reactWillMakeFirstResponder` and `reactDidMakeFirstResponder` was replaced with just `reactFocus` method
which is supposed to incapsulate all "focus" and "focus-later-if-needed" functionality.

Reviewed By: mmmulani

Differential Revision: D4664626

fbshipit-source-id: 8d3b7935ca26d32ba1d1826a585cce0396fcc885
  • Loading branch information
shergin authored and facebook-github-bot committed Apr 3, 2017
1 parent beb559d commit bc1ea54
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 86 deletions.
31 changes: 5 additions & 26 deletions Libraries/Text/RCTTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ - (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField {
@implementation RCTTextField
{
RCTEventDispatcher *_eventDispatcher;
BOOL _jsRequestingFirstResponder;
NSInteger _nativeEventCount;
BOOL _submitted;
UITextRange *_previousSelectionRange;
Expand Down Expand Up @@ -94,16 +93,6 @@ - (void)sendKeyValueForString:(NSString *)string
eventCount:_nativeEventCount];
}

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
[super didUpdateFocusInContext:context withAnimationCoordinator:coordinator];
if(context.nextFocusedView == self) {
_jsRequestingFirstResponder = YES;
} else {
_jsRequestingFirstResponder = NO;
}
}

// This method is overridden for `onKeyPress`. The manager
// will not send a keyPress for text that was pasted.
- (void)paste:(id)sender
Expand Down Expand Up @@ -285,21 +274,6 @@ - (void)sendSelectionEvent
}
}

- (BOOL)canBecomeFirstResponder
{
return _jsRequestingFirstResponder;
}

- (void)reactWillMakeFirstResponder
{
_jsRequestingFirstResponder = YES;
}

- (void)reactDidMakeFirstResponder
{
_jsRequestingFirstResponder = NO;
}

- (BOOL)resignFirstResponder
{
BOOL result = [super resignFirstResponder];
Expand All @@ -314,6 +288,11 @@ - (BOOL)resignFirstResponder
return result;
}

- (void)didMoveToWindow
{
[self reactFocusIfNeeded];
}

#pragma mark - UITextFieldDelegate (Proxied)

- (BOOL)shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Expand Down
34 changes: 9 additions & 25 deletions Libraries/Text/RCTTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ - (BOOL)textView:(RCTUITextView *)textView shouldChangeTextInRange:(NSRange)rang
text:self.text
key:nil
eventCount:_nativeEventCount];
[self resignFirstResponder];
[_textView resignFirstResponder];
return NO;
}
}
Expand Down Expand Up @@ -541,40 +541,24 @@ - (void)textViewDidEndEditing:(UITextView *)textView
eventCount:_nativeEventCount];
}

#pragma mark - UIResponder
#pragma mark - Focus control deledation

- (BOOL)isFirstResponder
- (void)reactFocus
{
return [_textView isFirstResponder];
[_textView reactFocus];
}

- (BOOL)canBecomeFirstResponder
- (void)reactBlur
{
return [_textView canBecomeFirstResponder];
[_textView reactBlur];
}

- (void)reactWillMakeFirstResponder
- (void)didMoveToWindow
{
[_textView reactWillMakeFirstResponder];
[_textView reactFocusIfNeeded];
}

- (BOOL)becomeFirstResponder
{
return [_textView becomeFirstResponder];
}

- (void)reactDidMakeFirstResponder
{
[_textView reactDidMakeFirstResponder];
}

- (BOOL)resignFirstResponder
{
[super resignFirstResponder];
return [_textView resignFirstResponder];
}

#pragma mark - Content Size
#pragma mark - Content size

- (CGSize)contentSize
{
Expand Down
28 changes: 2 additions & 26 deletions Libraries/Text/RCTUITextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

#import "RCTUITextView.h"

#import <React/UIView+React.h>

@implementation RCTUITextView
{
BOOL _jsRequestingFirstResponder;
UILabel *_placeholderView;
UITextView *_detachedTextView;
}
Expand Down Expand Up @@ -69,31 +70,6 @@ - (void)textDidChange
[self invalidatePlaceholderVisibility];
}

#pragma mark - UIResponder

- (void)reactWillMakeFirstResponder
{
_jsRequestingFirstResponder = YES;
}

- (BOOL)canBecomeFirstResponder
{
return _jsRequestingFirstResponder;
}

- (void)reactDidMakeFirstResponder
{
_jsRequestingFirstResponder = NO;
}

- (void)didMoveToWindow
{
if (_jsRequestingFirstResponder) {
[self becomeFirstResponder];
[self reactDidMakeFirstResponder];
}
}

#pragma mark - Overrides

- (void)setFont:(UIFont *)font
Expand Down
7 changes: 2 additions & 5 deletions React/Modules/RCTUIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1077,18 +1077,15 @@ - (void)synchronouslyUpdateViewOnUIThread:(NSNumber *)reactTag
{
[self addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
UIView *newResponder = viewRegistry[reactTag];
[newResponder reactWillMakeFirstResponder];
if ([newResponder becomeFirstResponder]) {
[newResponder reactDidMakeFirstResponder];
}
[newResponder reactFocus];
}];
}

RCT_EXPORT_METHOD(blur:(nonnull NSNumber *)reactTag)
{
[self addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry){
UIView *currentResponder = viewRegistry[reactTag];
[currentResponder resignFirstResponder];
[currentResponder reactBlur];
}];
}

Expand Down
9 changes: 7 additions & 2 deletions React/Views/UIView+React.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@
*/
- (void)reactAddControllerToClosestParent:(UIViewController *)controller;

/**
* Focus manipulation.
*/
- (void)reactFocus;
- (void)reactFocusIfNeeded;
- (void)reactBlur;

/**
* Responder overrides - to be deprecated.
*/
- (void)reactWillMakeFirstResponder;
- (void)reactDidMakeFirstResponder;
- (BOOL)reactRespondsToTouch:(UITouch *)touch;

#if RCT_DEV
Expand Down
33 changes: 31 additions & 2 deletions React/Views/UIView+React.m
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,40 @@ - (void)reactAddControllerToClosestParent:(UIViewController *)controller
}
}

/**
* Focus manipulation.
*/
- (BOOL)reactIsFocusNeeded
{
return [(NSNumber *)objc_getAssociatedObject(self, @selector(reactIsFocusNeeded)) boolValue];
}

- (void)setReactIsFocusNeeded:(BOOL)isFocusNeeded
{
objc_setAssociatedObject(self, @selector(reactIsFocusNeeded), @(isFocusNeeded), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)reactFocus {
if (![self becomeFirstResponder]) {
self.reactIsFocusNeeded = YES;
}
}

- (void)reactFocusIfNeeded {
if (self.reactIsFocusNeeded) {
if ([self becomeFirstResponder]) {
self.reactIsFocusNeeded = NO;
}
}
}

- (void)reactBlur {
[self resignFirstResponder];
}

/**
* Responder overrides - to be deprecated.
*/
- (void)reactWillMakeFirstResponder {};
- (void)reactDidMakeFirstResponder {};
- (BOOL)reactRespondsToTouch:(__unused UITouch *)touch
{
return YES;
Expand Down

0 comments on commit bc1ea54

Please sign in to comment.