From b4623dc33861a75ccf799d399ac721c93889e7b4 Mon Sep 17 00:00:00 2001 From: Frederic Barthelemy Date: Tue, 23 Jul 2019 16:21:43 -0700 Subject: [PATCH] Fix field focus infinite recursion in keyboardWillShow on > RN 45 Fixes https://github.com/tipsi/tipsi-stripe/issues/408 React-Native removed support for the react(Will|Did)MakeFirstResponder APIs in this commit: https://github.com/facebook/react-native/commit/bc1ea548d0017f131c36a30ce06bf4d512cb2f8c#diff-3e154f92c7ba4cb47e96785b9dadf3aa --- ios/TPSStripe/TPSCardField.m | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/ios/TPSStripe/TPSCardField.m b/ios/TPSStripe/TPSCardField.m index d3173c772..a1b3a2c96 100644 --- a/ios/TPSStripe/TPSCardField.m +++ b/ios/TPSStripe/TPSCardField.m @@ -12,10 +12,11 @@ @interface TPSCardField () @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 { @@ -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; } @@ -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; } @@ -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; } @@ -74,6 +93,7 @@ - (BOOL)becomeFirstResponder { } - (BOOL)resignFirstResponder { + _jsRequestingFirstResponder = NO; _isFirstResponder = NO; return [_paymentCardTextField resignFirstResponder]; } @@ -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