Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
Fix field focus infinite recursion in keyboardWillShow on > RN 45
Browse files Browse the repository at this point in the history
Fixes #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 Aug 9, 2019
1 parent a55b99e commit b4623dc
Showing 1 changed file with 27 additions and 7 deletions.
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:(STPPaymentMethodCardParams *)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 b4623dc

Please sign in to comment.