Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,21 +606,6 @@ var TextInput = React.createClass({
var text = event.nativeEvent.text;
this.props.onChange && this.props.onChange(event);
this.props.onChangeText && this.props.onChangeText(text);

if (!this.refs.input) {
// calling `this.props.onChange` or `this.props.onChangeText`
// may clean up the input itself. Exits here.
return;
}

// This is necessary in case native updates the text and JS decides
// that the update should be ignored and we should stick with the value
// that we have in JS.
if (text !== this.props.value && typeof this.props.value === 'string') {
this.refs.input.setNativeProps({
text: this.props.value,
});
}
},

_onBlur: function(event: Event) {
Expand Down
1 change: 1 addition & 0 deletions Libraries/Text/RCTTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

- (void)textFieldDidChange;
- (void)sendKeyValueForString:(NSString *)string;
- (void)sendTextChangedForString:(NSString *)string;
- (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField;

@end
41 changes: 28 additions & 13 deletions Libraries/Text/RCTTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ - (void)sendKeyValueForString:(NSString *)string
eventCount:_nativeEventCount];
}

- (void)sendTextChangedForString:(NSString *)string
{
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeChange
reactTag:self.reactTag
text:string
key:nil
eventCount:_nativeEventCount];
}

// This method is overridden for `onKeyPress`. The manager
// will not send a keyPress for text that was pasted.
- (void)paste:(id)sender
Expand All @@ -69,19 +78,25 @@ - (void)paste:(id)sender
- (void)setText:(NSString *)text
{
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
if (eventLag == 0 && ![text isEqualToString:self.text]) {
UITextRange *selection = self.selectedTextRange;
NSInteger oldTextLength = self.text.length;

super.text = text;

if (selection.empty) {
// maintain cursor position relative to the end of the old text
NSInteger offsetStart = [self offsetFromPosition:self.beginningOfDocument toPosition:selection.start];
NSInteger offsetFromEnd = oldTextLength - offsetStart;
NSInteger newOffset = text.length - offsetFromEnd;
UITextPosition *position = [self positionFromPosition:self.beginningOfDocument offset:newOffset];
self.selectedTextRange = [self textRangeFromPosition:position toPosition:position];
if (eventLag == 0) {
if (text.length == 0) {
// Note: If we don't set 'super.text' it doesn't clear the autocorrect state; so ensure we do this when clearing the text field or autocorrect results will just keep appending
super.text = text;
}
else if (![text isEqualToString:self.text]) {
UITextRange *selection = self.selectedTextRange;
NSInteger oldTextLength = self.text.length;

super.text = text;

if (selection.empty) {
// maintain cursor position relative to the end of the old text
NSInteger offsetStart = [self offsetFromPosition:self.beginningOfDocument toPosition:selection.start];
NSInteger offsetFromEnd = oldTextLength - offsetStart;
NSInteger newOffset = text.length - offsetFromEnd;
UITextPosition *position = [self positionFromPosition:self.beginningOfDocument offset:newOffset];
self.selectedTextRange = [self textRangeFromPosition:position toPosition:position];
}
}
} else if (eventLag > RCTTextUpdateLagWarningThreshold) {
RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", self.text, eventLag);
Expand Down
14 changes: 12 additions & 2 deletions Libraries/Text/RCTTextFieldManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ - (BOOL)textField:(RCTTextField *)textField shouldChangeCharactersInRange:(NSRan
[textField sendKeyValueForString:string];
}

if (textField.maxLength == nil || [string isEqualToString:@"\n"]) { // Make sure forms can be submitted via return
if ([string isEqualToString:@"\n"]) { // Make sure forms can be submitted via return
return YES;
}

if (textField.maxLength == nil) {
NSMutableString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

[textField sendTextChangedForString:newString];
return NO;
}
NSUInteger allowedLength = textField.maxLength.integerValue - textField.text.length + range.length;
if (string.length > allowedLength) {
if (string.length > 1) {
Expand All @@ -56,7 +63,10 @@ - (BOOL)textField:(RCTTextField *)textField shouldChangeCharactersInRange:(NSRan
}
return NO;
} else {
return YES;
NSMutableString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

[textField sendTextChangedForString:newString];
return NO;
}
}

Expand Down