Skip to content

Commit 077386a

Browse files
levibuzolicfacebook-github-bot
authored andcommittedDec 10, 2018
Map textContentType strings to Objective-C constants (#22579)
Summary: Fixes #22578 Currently the only `textContentType` values that work are: `username`, `password`, `location`, `name` and `nickname`. This is due to the strings provided by React Native not matching up with the underlying string constants used in iOS (with the exception of the aforementioned types). Issue #22578 has more detail examples/explanation. Pull Request resolved: #22579 Differential Revision: D13402177 Pulled By: shergin fbshipit-source-id: 55f4a2029cd3ea1fb4834e9f56d2df5a05b31b4e
1 parent 1bc704d commit 077386a

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed
 

‎Libraries/Text/TextInput/RCTBaseTextInputView.m

+55-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
131131

132132
[attributedTextCopy removeAttribute:RCTTextAttributesTagAttributeName
133133
range:NSMakeRange(0, attributedTextCopy.length)];
134-
134+
135135
textNeedsUpdate = ([self textOf:attributedTextCopy equals:backedTextInputViewTextCopy] == NO);
136-
136+
137137
if (eventLag == 0 && textNeedsUpdate) {
138138
UITextRange *selection = self.backedTextInputView.selectedTextRange;
139139
NSInteger oldTextLength = self.backedTextInputView.attributedText.string.length;
@@ -193,9 +193,61 @@ - (void)setTextContentType:(NSString *)type
193193
{
194194
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
195195
if (@available(iOS 10.0, *)) {
196+
197+
static dispatch_once_t onceToken;
198+
static NSDictionary<NSString *, NSString *> *contentTypeMap;
199+
200+
dispatch_once(&onceToken, ^{
201+
contentTypeMap = @{@"none": @"",
202+
@"URL": UITextContentTypeURL,
203+
@"addressCity": UITextContentTypeAddressCity,
204+
@"addressCityAndState":UITextContentTypeAddressCityAndState,
205+
@"addressState": UITextContentTypeAddressState,
206+
@"countryName": UITextContentTypeCountryName,
207+
@"creditCardNumber": UITextContentTypeCreditCardNumber,
208+
@"emailAddress": UITextContentTypeEmailAddress,
209+
@"familyName": UITextContentTypeFamilyName,
210+
@"fullStreetAddress": UITextContentTypeFullStreetAddress,
211+
@"givenName": UITextContentTypeGivenName,
212+
@"jobTitle": UITextContentTypeJobTitle,
213+
@"location": UITextContentTypeLocation,
214+
@"middleName": UITextContentTypeMiddleName,
215+
@"name": UITextContentTypeName,
216+
@"namePrefix": UITextContentTypeNamePrefix,
217+
@"nameSuffix": UITextContentTypeNameSuffix,
218+
@"nickname": UITextContentTypeNickname,
219+
@"organizationName": UITextContentTypeOrganizationName,
220+
@"postalCode": UITextContentTypePostalCode,
221+
@"streetAddressLine1": UITextContentTypeStreetAddressLine1,
222+
@"streetAddressLine2": UITextContentTypeStreetAddressLine2,
223+
@"sublocality": UITextContentTypeSublocality,
224+
@"telephoneNumber": UITextContentTypeTelephoneNumber,
225+
};
226+
227+
if (@available(iOS 11.0, *)) {
228+
NSDictionary<NSString *, NSString *> * extras = @{@"username": UITextContentTypeUsername,
229+
@"password": UITextContentTypePassword};
230+
231+
NSMutableDictionary<NSString *, NSString *> * baseMap = [contentTypeMap mutableCopy];
232+
[baseMap addEntriesFromDictionary:extras];
233+
234+
contentTypeMap = [baseMap copy];
235+
}
236+
237+
if (@available(iOS 12.0, *)) {
238+
NSDictionary<NSString *, NSString *> * extras = @{@"newPassword": UITextContentTypeNewPassword,
239+
@"oneTimeCode": UITextContentTypeOneTimeCode};
240+
241+
NSMutableDictionary<NSString *, NSString *> * baseMap = [contentTypeMap mutableCopy];
242+
[baseMap addEntriesFromDictionary:extras];
243+
244+
contentTypeMap = [baseMap copy];
245+
}
246+
});
247+
196248
// Setting textContentType to an empty string will disable any
197249
// default behaviour, like the autofill bar for password inputs
198-
self.backedTextInputView.textContentType = [type isEqualToString:@"none"] ? @"" : type;
250+
self.backedTextInputView.textContentType = type ? contentTypeMap[type] : type;
199251
}
200252
#endif
201253
}

‎RNTester/js/TextInputExample.ios.js

+15
Original file line numberDiff line numberDiff line change
@@ -1085,4 +1085,19 @@ exports.examples = [
10851085
);
10861086
},
10871087
},
1088+
{
1089+
title: 'Text Content Type',
1090+
render: function() {
1091+
return (
1092+
<View>
1093+
<WithLabel label="emailAddress">
1094+
<TextInput textContentType="emailAddress" style={styles.default} />
1095+
</WithLabel>
1096+
<WithLabel label="name">
1097+
<TextInput textContentType="name" style={styles.default} />
1098+
</WithLabel>
1099+
</View>
1100+
);
1101+
},
1102+
},
10881103
];

0 commit comments

Comments
 (0)
Please sign in to comment.