mirror of
https://github.com/status-im/react-native.git
synced 2025-01-27 17:54:48 +00:00
Better TextInput: Simplified focus/first-responder management on iOS
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
This commit is contained in:
parent
beb559d9cf
commit
bc1ea548d0
@ -48,7 +48,6 @@
|
|||||||
@implementation RCTTextField
|
@implementation RCTTextField
|
||||||
{
|
{
|
||||||
RCTEventDispatcher *_eventDispatcher;
|
RCTEventDispatcher *_eventDispatcher;
|
||||||
BOOL _jsRequestingFirstResponder;
|
|
||||||
NSInteger _nativeEventCount;
|
NSInteger _nativeEventCount;
|
||||||
BOOL _submitted;
|
BOOL _submitted;
|
||||||
UITextRange *_previousSelectionRange;
|
UITextRange *_previousSelectionRange;
|
||||||
@ -94,16 +93,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
|||||||
eventCount:_nativeEventCount];
|
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
|
// This method is overridden for `onKeyPress`. The manager
|
||||||
// will not send a keyPress for text that was pasted.
|
// will not send a keyPress for text that was pasted.
|
||||||
- (void)paste:(id)sender
|
- (void)paste:(id)sender
|
||||||
@ -285,21 +274,6 @@ static void RCTUpdatePlaceholder(RCTTextField *self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)canBecomeFirstResponder
|
|
||||||
{
|
|
||||||
return _jsRequestingFirstResponder;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)reactWillMakeFirstResponder
|
|
||||||
{
|
|
||||||
_jsRequestingFirstResponder = YES;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)reactDidMakeFirstResponder
|
|
||||||
{
|
|
||||||
_jsRequestingFirstResponder = NO;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (BOOL)resignFirstResponder
|
- (BOOL)resignFirstResponder
|
||||||
{
|
{
|
||||||
BOOL result = [super resignFirstResponder];
|
BOOL result = [super resignFirstResponder];
|
||||||
@ -314,6 +288,11 @@ static void RCTUpdatePlaceholder(RCTTextField *self)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)didMoveToWindow
|
||||||
|
{
|
||||||
|
[self reactFocusIfNeeded];
|
||||||
|
}
|
||||||
|
|
||||||
#pragma mark - UITextFieldDelegate (Proxied)
|
#pragma mark - UITextFieldDelegate (Proxied)
|
||||||
|
|
||||||
- (BOOL)shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
|
- (BOOL)shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
|
||||||
|
@ -332,7 +332,7 @@ static NSAttributedString *removeReactTagFromString(NSAttributedString *string)
|
|||||||
text:self.text
|
text:self.text
|
||||||
key:nil
|
key:nil
|
||||||
eventCount:_nativeEventCount];
|
eventCount:_nativeEventCount];
|
||||||
[self resignFirstResponder];
|
[_textView resignFirstResponder];
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -541,40 +541,24 @@ static BOOL findMismatch(NSString *first, NSString *second, NSRange *firstRange,
|
|||||||
eventCount:_nativeEventCount];
|
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
|
#pragma mark - Content size
|
||||||
{
|
|
||||||
return [_textView becomeFirstResponder];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)reactDidMakeFirstResponder
|
|
||||||
{
|
|
||||||
[_textView reactDidMakeFirstResponder];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (BOOL)resignFirstResponder
|
|
||||||
{
|
|
||||||
[super resignFirstResponder];
|
|
||||||
return [_textView resignFirstResponder];
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma mark - Content Size
|
|
||||||
|
|
||||||
- (CGSize)contentSize
|
- (CGSize)contentSize
|
||||||
{
|
{
|
||||||
|
@ -9,9 +9,10 @@
|
|||||||
|
|
||||||
#import "RCTUITextView.h"
|
#import "RCTUITextView.h"
|
||||||
|
|
||||||
|
#import <React/UIView+React.h>
|
||||||
|
|
||||||
@implementation RCTUITextView
|
@implementation RCTUITextView
|
||||||
{
|
{
|
||||||
BOOL _jsRequestingFirstResponder;
|
|
||||||
UILabel *_placeholderView;
|
UILabel *_placeholderView;
|
||||||
UITextView *_detachedTextView;
|
UITextView *_detachedTextView;
|
||||||
}
|
}
|
||||||
@ -69,31 +70,6 @@ static UIColor *defaultPlaceholderTextColor()
|
|||||||
[self invalidatePlaceholderVisibility];
|
[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
|
#pragma mark - Overrides
|
||||||
|
|
||||||
- (void)setFont:(UIFont *)font
|
- (void)setFont:(UIFont *)font
|
||||||
|
@ -1077,10 +1077,7 @@ RCT_EXPORT_METHOD(focus:(nonnull NSNumber *)reactTag)
|
|||||||
{
|
{
|
||||||
[self addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
|
[self addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
|
||||||
UIView *newResponder = viewRegistry[reactTag];
|
UIView *newResponder = viewRegistry[reactTag];
|
||||||
[newResponder reactWillMakeFirstResponder];
|
[newResponder reactFocus];
|
||||||
if ([newResponder becomeFirstResponder]) {
|
|
||||||
[newResponder reactDidMakeFirstResponder];
|
|
||||||
}
|
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1088,7 +1085,7 @@ RCT_EXPORT_METHOD(blur:(nonnull NSNumber *)reactTag)
|
|||||||
{
|
{
|
||||||
[self addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry){
|
[self addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry){
|
||||||
UIView *currentResponder = viewRegistry[reactTag];
|
UIView *currentResponder = viewRegistry[reactTag];
|
||||||
[currentResponder resignFirstResponder];
|
[currentResponder reactBlur];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,11 +71,16 @@
|
|||||||
*/
|
*/
|
||||||
- (void)reactAddControllerToClosestParent:(UIViewController *)controller;
|
- (void)reactAddControllerToClosestParent:(UIViewController *)controller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Focus manipulation.
|
||||||
|
*/
|
||||||
|
- (void)reactFocus;
|
||||||
|
- (void)reactFocusIfNeeded;
|
||||||
|
- (void)reactBlur;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Responder overrides - to be deprecated.
|
* Responder overrides - to be deprecated.
|
||||||
*/
|
*/
|
||||||
- (void)reactWillMakeFirstResponder;
|
|
||||||
- (void)reactDidMakeFirstResponder;
|
|
||||||
- (BOOL)reactRespondsToTouch:(UITouch *)touch;
|
- (BOOL)reactRespondsToTouch:(UITouch *)touch;
|
||||||
|
|
||||||
#if RCT_DEV
|
#if RCT_DEV
|
||||||
|
@ -211,11 +211,40 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Responder overrides - to be deprecated.
|
||||||
*/
|
*/
|
||||||
- (void)reactWillMakeFirstResponder {};
|
|
||||||
- (void)reactDidMakeFirstResponder {};
|
|
||||||
- (BOOL)reactRespondsToTouch:(__unused UITouch *)touch
|
- (BOOL)reactRespondsToTouch:(__unused UITouch *)touch
|
||||||
{
|
{
|
||||||
return YES;
|
return YES;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user