Skip to content

Commit

Permalink
Map textContentType strings to Objective-C constants (#22579)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
levibuzolic authored and facebook-github-bot committed Dec 10, 2018
1 parent 1bc704d commit 077386a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
58 changes: 55 additions & 3 deletions Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,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 @@ -193,9 +193,61 @@ - (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 (@available(iOS 11.0, *)) {
NSDictionary<NSString *, NSString *> * extras = @{@"username": UITextContentTypeUsername,
@"password": UITextContentTypePassword};

NSMutableDictionary<NSString *, NSString *> * baseMap = [contentTypeMap mutableCopy];
[baseMap addEntriesFromDictionary:extras];

contentTypeMap = [baseMap copy];
}

if (@available(iOS 12.0, *)) {
NSDictionary<NSString *, NSString *> * extras = @{@"newPassword": UITextContentTypeNewPassword,
@"oneTimeCode": UITextContentTypeOneTimeCode};

NSMutableDictionary<NSString *, NSString *> * baseMap = [contentTypeMap mutableCopy];
[baseMap addEntriesFromDictionary:extras];

contentTypeMap = [baseMap copy];
}
});

// 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 = type ? contentTypeMap[type] : type;
}
#endif
}
Expand Down
15 changes: 15 additions & 0 deletions RNTester/js/TextInputExample.ios.js
Original file line number Diff line number Diff line change
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>
);
},
},
];

0 comments on commit 077386a

Please sign in to comment.