Skip to content

Commit

Permalink
Fix field focus infinite recursion in keyboardWillShow on > RN 45
Browse files Browse the repository at this point in the history
Fixes tipsi#408

React-Native removed support for the react(Will|Did)MakeFirstResponder APIs in this commit: facebook/react-native@bc1ea54#diff-3e154f92c7ba4cb47e96785b9dadf3aa
  • Loading branch information
fbartho committed Jul 23, 2019
1 parent 14b41b2 commit b007704
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions ios/TPSStripe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
3A228EB01DC52E54007BB9D0 /* Frameworks */,
);
sourceTree = "<group>";
usesTabs = 0;
};
3A3A7F1B1DC27B520030F9DC /* Products */ = {
isa = PBXGroup;
Expand Down
34 changes: 27 additions & 7 deletions ios/TPSStripe/TPSCardField.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ @interface TPSCardField () <STPPaymentCardTextFieldDelegate>
@end

@implementation TPSCardField {
// Relates to a Deprecated API -- clean this up after RN 0.45 or lower support is dropped
BOOL _jsRequestingFirstResponder;
BOOL _isFirstResponder;
STPPaymentCardTextField *_paymentCardTextField;

STPPaymentCardTextField *_paymentCardTextField;
}

- (void)dealloc {
Expand All @@ -30,10 +31,10 @@ - (instancetype)initWithFrame:(CGRect)frame {
[self addSubview:_paymentCardTextField];
self.backgroundColor = [UIColor clearColor];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.window];
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.window];
}
return self;
}
Expand All @@ -43,6 +44,23 @@ - (void)reactSetFrame:(CGRect)frame {
_paymentCardTextField.frame = self.bounds;
}

- (void)reactFocus {
_jsRequestingFirstResponder = YES;
[self becomeFirstResponder];
}

- (void)reactFocusIfNeeded {
if (!_isFirstResponder) {
[self reactFocus];
}
}

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

// Deprecated API -- removed in 2017, clean this up after RN 0.45 or lower support is dropped
- (void)reactWillMakeFirstResponder {
_jsRequestingFirstResponder = YES;
}
Expand All @@ -51,6 +69,7 @@ - (BOOL)canBecomeFirstResponder {
return _jsRequestingFirstResponder;
}

// Deprecated API -- removed in 2017, clean this up after RN 0.45 or lower support is dropped
- (void)reactDidMakeFirstResponder {
_jsRequestingFirstResponder = NO;
}
Expand All @@ -74,6 +93,7 @@ - (BOOL)becomeFirstResponder {
}

- (BOOL)resignFirstResponder {
_jsRequestingFirstResponder = NO;
_isFirstResponder = NO;
return [_paymentCardTextField resignFirstResponder];
}
Expand Down Expand Up @@ -179,11 +199,11 @@ - (void)setCardParams:(STPCardParams *)cardParams {
}

- (UIKeyboardAppearance)keyboardAppearance {
return _paymentCardTextField.keyboardAppearance;
return _paymentCardTextField.keyboardAppearance;
}

- (void)setKeyboardAppearance:(UIKeyboardAppearance)keyboardAppearance {
_paymentCardTextField.keyboardAppearance = keyboardAppearance;
_paymentCardTextField.keyboardAppearance = keyboardAppearance;
}

#pragma mark - STPPaymentCardTextFieldDelegate
Expand Down

0 comments on commit b007704

Please sign in to comment.