From c1392c2ff3ca411bf4280b2371b1b27116756879 Mon Sep 17 00:00:00 2001 From: Eric Lewis Date: Thu, 21 Feb 2019 23:47:05 -0800 Subject: [PATCH] Toggle secureTextEntry cursor spacing (#23524) Summary: This is a fix for #5859, based on the feedback in #18587. Instead of using `didSetProps` it uses a setter. I will also note that setting to `nil` no longer works (crashes) so setting it to a blank string then back to the original works fine. [iOS] [Fixed] - Toggling secureTextEntry correctly places cursor. Pull Request resolved: https://github.com/facebook/react-native/pull/23524 Differential Revision: D14143028 Pulled By: cpojer fbshipit-source-id: 5f3203d56b1329eb7359465f8ab50eb4f4fa5507 --- .../Text/TextInput/RCTBaseTextInputView.h | 1 + .../Text/TextInput/RCTBaseTextInputView.m | 18 +++++++++++++ .../TextInput/RCTBaseTextInputViewManager.m | 2 +- RNTester/js/TextInputExample.ios.js | 26 ++++++++++++++++++- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.h b/Libraries/Text/TextInput/RCTBaseTextInputView.h index 67c1e54a9db..5b62f902514 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.h +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.h @@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, assign) BOOL blurOnSubmit; @property (nonatomic, assign) BOOL selectTextOnFocus; @property (nonatomic, assign) BOOL clearTextOnFocus; +@property (nonatomic, assign) BOOL secureTextEntry; @property (nonatomic, copy) RCTTextSelection *selection; @property (nonatomic, strong, nullable) NSNumber *maxLength; @property (nonatomic, copy) NSAttributedString *attributedText; diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index 4b84de26029..a0621bfea53 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -283,6 +283,24 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame) } } +- (BOOL)secureTextEntry { + return self.backedTextInputView.secureTextEntry; +} + +- (void)setSecureTextEntry:(BOOL)secureTextEntry { + UIView *textInputView = self.backedTextInputView; + + if (textInputView.secureTextEntry != secureTextEntry) { + textInputView.secureTextEntry = secureTextEntry; + + // Fix #5859, see https://stackoverflow.com/questions/14220187/uitextfield-has-trailing-whitespace-after-securetextentry-toggle/22537788#22537788 + NSAttributedString *originalText = [textInputView.attributedText copy]; + self.backedTextInputView.attributedText = [NSAttributedString new]; + self.backedTextInputView.attributedText = originalText; + } + +} + #pragma mark - RCTBackedTextInputDelegate - (BOOL)textInputShouldBeginEditing diff --git a/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m b/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m index 6d5f7ec5aa0..d226782be62 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m @@ -43,12 +43,12 @@ RCT_REMAP_VIEW_PROPERTY(keyboardAppearance, backedTextInputView.keyboardAppearan RCT_REMAP_VIEW_PROPERTY(placeholder, backedTextInputView.placeholder, NSString) RCT_REMAP_VIEW_PROPERTY(placeholderTextColor, backedTextInputView.placeholderColor, UIColor) RCT_REMAP_VIEW_PROPERTY(returnKeyType, backedTextInputView.returnKeyType, UIReturnKeyType) -RCT_REMAP_VIEW_PROPERTY(secureTextEntry, backedTextInputView.secureTextEntry, BOOL) RCT_REMAP_VIEW_PROPERTY(selectionColor, backedTextInputView.tintColor, UIColor) RCT_REMAP_VIEW_PROPERTY(spellCheck, backedTextInputView.spellCheckingType, UITextSpellCheckingType) RCT_REMAP_VIEW_PROPERTY(caretHidden, backedTextInputView.caretHidden, BOOL) RCT_REMAP_VIEW_PROPERTY(clearButtonMode, backedTextInputView.clearButtonMode, UITextFieldViewMode) RCT_REMAP_VIEW_PROPERTY(scrollEnabled, backedTextInputView.scrollEnabled, BOOL) +RCT_EXPORT_VIEW_PROPERTY(secureTextEntry, BOOL) RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL) RCT_EXPORT_VIEW_PROPERTY(clearTextOnFocus, BOOL) RCT_EXPORT_VIEW_PROPERTY(keyboardType, UIKeyboardType) diff --git a/RNTester/js/TextInputExample.ios.js b/RNTester/js/TextInputExample.ios.js index 5529db69a06..c2a73781a7b 100644 --- a/RNTester/js/TextInputExample.ios.js +++ b/RNTester/js/TextInputExample.ios.js @@ -212,7 +212,11 @@ class SecureEntryExample extends React.Component<$FlowFixMeProps, any> { * comment and run Flow. */ constructor(props) { super(props); - this.state = {text: ''}; + this.state = { + text: '', + password: '', + isSecureTextEntry: true, + }; } render() { return ( @@ -225,6 +229,26 @@ class SecureEntryExample extends React.Component<$FlowFixMeProps, any> { value={this.state.text} /> Current text is: {this.state.text} + + this.setState({password: text})} + secureTextEntry={this.state.isSecureTextEntry} + value={this.state.password} + /> + { + this.setState({isSecureTextEntry: value}); + }} + style={{marginLeft: 4}} + value={this.state.isSecureTextEntry} + /> + ); }