Skip to content

Commit c1392c2

Browse files
ericlewisgrabbou
authored andcommittedFeb 27, 2019
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: #23524 Differential Revision: D14143028 Pulled By: cpojer fbshipit-source-id: 5f3203d56b1329eb7359465f8ab50eb4f4fa5507
1 parent 8e5eb63 commit c1392c2

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed
 

‎Libraries/Text/TextInput/RCTBaseTextInputView.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
4343
@property (nonatomic, assign) BOOL blurOnSubmit;
4444
@property (nonatomic, assign) BOOL selectTextOnFocus;
4545
@property (nonatomic, assign) BOOL clearTextOnFocus;
46+
@property (nonatomic, assign) BOOL secureTextEntry;
4647
@property (nonatomic, copy) RCTTextSelection *selection;
4748
@property (nonatomic, strong, nullable) NSNumber *maxLength;
4849
@property (nonatomic, copy) NSAttributedString *attributedText;

‎Libraries/Text/TextInput/RCTBaseTextInputView.m

+18
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,24 @@ - (void)setKeyboardType:(UIKeyboardType)keyboardType
283283
}
284284
}
285285

286+
- (BOOL)secureTextEntry {
287+
return self.backedTextInputView.secureTextEntry;
288+
}
289+
290+
- (void)setSecureTextEntry:(BOOL)secureTextEntry {
291+
UIView<RCTBackedTextInputViewProtocol> *textInputView = self.backedTextInputView;
292+
293+
if (textInputView.secureTextEntry != secureTextEntry) {
294+
textInputView.secureTextEntry = secureTextEntry;
295+
296+
// Fix #5859, see https://stackoverflow.com/questions/14220187/uitextfield-has-trailing-whitespace-after-securetextentry-toggle/22537788#22537788
297+
NSAttributedString *originalText = [textInputView.attributedText copy];
298+
self.backedTextInputView.attributedText = [NSAttributedString new];
299+
self.backedTextInputView.attributedText = originalText;
300+
}
301+
302+
}
303+
286304
#pragma mark - RCTBackedTextInputDelegate
287305

288306
- (BOOL)textInputShouldBeginEditing

‎Libraries/Text/TextInput/RCTBaseTextInputViewManager.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ @implementation RCTBaseTextInputViewManager
4343
RCT_REMAP_VIEW_PROPERTY(placeholder, backedTextInputView.placeholder, NSString)
4444
RCT_REMAP_VIEW_PROPERTY(placeholderTextColor, backedTextInputView.placeholderColor, UIColor)
4545
RCT_REMAP_VIEW_PROPERTY(returnKeyType, backedTextInputView.returnKeyType, UIReturnKeyType)
46-
RCT_REMAP_VIEW_PROPERTY(secureTextEntry, backedTextInputView.secureTextEntry, BOOL)
4746
RCT_REMAP_VIEW_PROPERTY(selectionColor, backedTextInputView.tintColor, UIColor)
4847
RCT_REMAP_VIEW_PROPERTY(spellCheck, backedTextInputView.spellCheckingType, UITextSpellCheckingType)
4948
RCT_REMAP_VIEW_PROPERTY(caretHidden, backedTextInputView.caretHidden, BOOL)
5049
RCT_REMAP_VIEW_PROPERTY(clearButtonMode, backedTextInputView.clearButtonMode, UITextFieldViewMode)
5150
RCT_REMAP_VIEW_PROPERTY(scrollEnabled, backedTextInputView.scrollEnabled, BOOL)
51+
RCT_EXPORT_VIEW_PROPERTY(secureTextEntry, BOOL)
5252
RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL)
5353
RCT_EXPORT_VIEW_PROPERTY(clearTextOnFocus, BOOL)
5454
RCT_EXPORT_VIEW_PROPERTY(keyboardType, UIKeyboardType)

‎RNTester/js/TextInputExample.ios.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ class SecureEntryExample extends React.Component<$FlowFixMeProps, any> {
212212
* comment and run Flow. */
213213
constructor(props) {
214214
super(props);
215-
this.state = {text: ''};
215+
this.state = {
216+
text: '',
217+
password: '',
218+
isSecureTextEntry: true,
219+
};
216220
}
217221
render() {
218222
return (
@@ -225,6 +229,26 @@ class SecureEntryExample extends React.Component<$FlowFixMeProps, any> {
225229
value={this.state.text}
226230
/>
227231
<Text>Current text is: {this.state.text}</Text>
232+
<View
233+
style={{
234+
flex: 1,
235+
flexDirection: 'row',
236+
}}>
237+
<TextInput
238+
style={styles.default}
239+
defaultValue="cde"
240+
onChangeText={text => this.setState({password: text})}
241+
secureTextEntry={this.state.isSecureTextEntry}
242+
value={this.state.password}
243+
/>
244+
<Switch
245+
onValueChange={value => {
246+
this.setState({isSecureTextEntry: value});
247+
}}
248+
style={{marginLeft: 4}}
249+
value={this.state.isSecureTextEntry}
250+
/>
251+
</View>
228252
</View>
229253
);
230254
}

0 commit comments

Comments
 (0)
Please sign in to comment.