Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map TextInput textContentType strings to Objective-C constants #22611

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 59 additions & 3 deletions Libraries/Text/TextInput/RCTBaseTextInputView.m
Expand Up @@ -141,9 +141,9 @@ - (void)setAttributedText:(NSAttributedString *)attributedText

[attributedTextCopy removeAttribute:RCTTextAttributesTagAttributeName
range:NSMakeRange(0, attributedTextCopy.length)];

textNeedsUpdate = ([self textOf:attributedTextCopy equals:backedTextInputViewTextCopy] == NO);

if (eventLag == 0 && textNeedsUpdate) {
UITextRange *selection = self.backedTextInputView.selectedTextRange;
NSInteger oldTextLength = self.backedTextInputView.attributedText.string.length;
Expand Down Expand Up @@ -203,9 +203,65 @@ - (void)setTextContentType:(NSString *)type
{
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
if (@available(iOS 10.0, *)) {

static dispatch_once_t onceToken;
static NSDictionary<NSString *, NSString *> *contentTypeMap;

dispatch_once(&onceToken, ^{
contentTypeMap = @{@"none": @"",
@"URL": UITextContentTypeURL,
@"addressCity": UITextContentTypeAddressCity,
@"addressCityAndState":UITextContentTypeAddressCityAndState,
@"addressState": UITextContentTypeAddressState,
@"countryName": UITextContentTypeCountryName,
@"creditCardNumber": UITextContentTypeCreditCardNumber,
@"emailAddress": UITextContentTypeEmailAddress,
@"familyName": UITextContentTypeFamilyName,
@"fullStreetAddress": UITextContentTypeFullStreetAddress,
@"givenName": UITextContentTypeGivenName,
@"jobTitle": UITextContentTypeJobTitle,
@"location": UITextContentTypeLocation,
@"middleName": UITextContentTypeMiddleName,
@"name": UITextContentTypeName,
@"namePrefix": UITextContentTypeNamePrefix,
@"nameSuffix": UITextContentTypeNameSuffix,
@"nickname": UITextContentTypeNickname,
@"organizationName": UITextContentTypeOrganizationName,
@"postalCode": UITextContentTypePostalCode,
@"streetAddressLine1": UITextContentTypeStreetAddressLine1,
@"streetAddressLine2": UITextContentTypeStreetAddressLine2,
@"sublocality": UITextContentTypeSublocality,
@"telephoneNumber": UITextContentTypeTelephoneNumber,
};

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
if (@available(iOS 11.0, *)) {
NSDictionary<NSString *, NSString *> * iOS11extras = @{@"username": UITextContentTypeUsername,
@"password": UITextContentTypePassword};

NSMutableDictionary<NSString *, NSString *> * iOS11baseMap = [contentTypeMap mutableCopy];
[iOS11baseMap addEntriesFromDictionary:iOS11extras];

contentTypeMap = [iOS11baseMap copy];
}
#endif

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 120000 /* __IPHONE_12_0 */
if (@available(iOS 12.0, *)) {
NSDictionary<NSString *, NSString *> * iOS12extras = @{@"newPassword": UITextContentTypeNewPassword,
@"oneTimeCode": UITextContentTypeOneTimeCode};

NSMutableDictionary<NSString *, NSString *> * iOS12baseMap = [contentTypeMap mutableCopy];
[iOS12baseMap addEntriesFromDictionary:iOS12extras];

contentTypeMap = [iOS12baseMap copy];
}
#endif
});

// Setting textContentType to an empty string will disable any
// default behaviour, like the autofill bar for password inputs
self.backedTextInputView.textContentType = [type isEqualToString:@"none"] ? @"" : type;
self.backedTextInputView.textContentType = contentTypeMap[type] ?: type;
}
#endif
}
Expand Down
15 changes: 15 additions & 0 deletions RNTester/js/TextInputExample.ios.js
Expand Up @@ -1085,4 +1085,19 @@ exports.examples = [
);
},
},
{
title: 'Text Content Type',
render: function() {
return (
<View>
<WithLabel label="emailAddress">
<TextInput textContentType="emailAddress" style={styles.default} />
</WithLabel>
<WithLabel label="name">
<TextInput textContentType="name" style={styles.default} />
</WithLabel>
</View>
);
},
},
];