Skip to content

Commit

Permalink
[TextInput] Fixes 'AutoCorrect on Submit' bug
Browse files Browse the repository at this point in the history
Catches a spelling auto correction when the 'Return' key is pressed and makes sure that a `RCTTextEventTypeChange` event fires.

- Doesn't increment the `nativeEventCount` as it doesn't need to match up with JS.
- Extracts a few methods in `RCTTextFieldManager` to reduce duplication introduced.

Resolves: facebook#2552
  • Loading branch information
dsibiski committed Jan 11, 2016
1 parent e08a7f3 commit 58def5a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions Libraries/Text/RCTTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
- (void)textFieldDidChange;
- (void)sendKeyValueForString:(NSString *)string;
- (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField;
- (void)sendChangeEvent;

@end
5 changes: 5 additions & 0 deletions Libraries/Text/RCTTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ - (BOOL)autoCorrect
- (void)textFieldDidChange
{
_nativeEventCount++;
[self sendChangeEvent];
}

- (void)sendChangeEvent
{
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeChange
reactTag:self.reactTag
text:self.text
Expand Down
37 changes: 31 additions & 6 deletions Libraries/Text/RCTTextFieldManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,28 @@ - (BOOL)textField:(RCTTextField *)textField shouldChangeCharactersInRange:(NSRan
}

if (textField.maxLength == nil || [string isEqualToString:@"\n"]) { // Make sure forms can be submitted via return
if (textField.autocorrectionType == UITextAutocorrectionTypeYes) { // If autoCorrect is on...
if (range.length > 0 && ![string isEqualToString:@""]) { // This catches an auto correction on submit.
textField.text = [self replaceText:textField.text.mutableCopy inRange:range withString:string];
// Collapse selection at end of insert to match normal paste behavior
textField.selectedTextRange = [self textRangeForTextField:textField
withTextPositionOffset:(range.location + string.length)];
[textField sendChangeEvent];
return NO;
}
}
return YES;
}

NSUInteger allowedLength = textField.maxLength.integerValue - textField.text.length + range.length;
if (string.length > allowedLength) {
if (string.length > 1) {
// Truncate the input string so the result is exactly maxLength
NSString *limitedString = [string substringToIndex:allowedLength];
NSMutableString *newString = textField.text.mutableCopy;
[newString replaceCharactersInRange:range withString:limitedString];
textField.text = newString;
textField.text = [self replaceText:textField.text.mutableCopy inRange:range withString:limitedString];
// Collapse selection at end of insert to match normal paste behavior
UITextPosition *insertEnd = [textField positionFromPosition:textField.beginningOfDocument
offset:(range.location + allowedLength)];
textField.selectedTextRange = [textField textRangeFromPosition:insertEnd toPosition:insertEnd];
textField.selectedTextRange = [self textRangeForTextField:textField
withTextPositionOffset:(range.location + allowedLength)];
[textField textFieldDidChange];
}
return NO;
Expand All @@ -73,6 +81,23 @@ - (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField
return [textField textFieldShouldEndEditing:textField];
}

- (NSString *)replaceText:(NSMutableString *)originalString
inRange:(NSRange)range
withString:(NSString *)string
{
NSMutableString *newString = originalString;
[newString replaceCharactersInRange:range withString:string];
return newString;
}

- (UITextRange *)textRangeForTextField:(UITextField *)textField
withTextPositionOffset:(NSUInteger)offset
{
UITextPosition *insertEnd = [textField positionFromPosition:textField.beginningOfDocument
offset:offset];
return [textField textRangeFromPosition:insertEnd toPosition:insertEnd];
}

RCT_EXPORT_VIEW_PROPERTY(caretHidden, BOOL)
RCT_EXPORT_VIEW_PROPERTY(autoCorrect, BOOL)
RCT_REMAP_VIEW_PROPERTY(editable, enabled, BOOL)
Expand Down

0 comments on commit 58def5a

Please sign in to comment.