Added blurOnSubmit support to multine TextInput (aka RCTTextView)

Summary:
public

Setting `blurOnSubmit=true` on a multiline `<TextInput>` now causes it to behave like a single-line input with respect to the return key:

With the default value of `false`, pressing return will enter a newline character into the field. If you set the value to `true`, pressing return will now blur the field and trigger the onSubmitEditing event. The newline character will *not* be added to the text.

(See associated github task for dicussion: https://github.com/facebook/react-native/pull/2149)

Reviewed By: javache

Differential Revision: D2710448

fb-gh-sync-id: c9706ae11f8b399932d3400ceb4c7558e455570d
This commit is contained in:
Nick Lockwood
2015-12-02 07:11:20 -08:00
committed by facebook-github-bot-9
parent 807e0d9310
commit 37042573b8
6 changed files with 50 additions and 23 deletions

View File

@@ -36,6 +36,7 @@
[self addTarget:self action:@selector(textFieldSubmitEditing) forControlEvents:UIControlEventEditingDidEndOnExit];
[self addObserver:self forKeyPath:@"selectedTextRange" options:0 context:nil];
_reactSubviews = [NSMutableArray new];
_blurOnSubmit = YES;
}
return self;
}

View File

@@ -17,6 +17,7 @@
@interface RCTTextView : RCTView <UITextViewDelegate>
@property (nonatomic, assign) BOOL autoCorrect;
@property (nonatomic, assign) BOOL blurOnSubmit;
@property (nonatomic, assign) BOOL clearTextOnFocus;
@property (nonatomic, assign) BOOL selectTextOnFocus;
@property (nonatomic, assign) UIEdgeInsets contentInset;

View File

@@ -72,6 +72,7 @@
_contentInset = UIEdgeInsetsZero;
_eventDispatcher = eventDispatcher;
_placeholderTextColor = [self defaultPlaceholderTextColor];
_blurOnSubmit = NO;
_textView = [[RCTUITextView alloc] initWithFrame:CGRectZero];
_textView.backgroundColor = [UIColor clearColor];
@@ -276,11 +277,35 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
if (textView.textWasPasted) {
textView.textWasPasted = NO;
} else {
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeKeyPress
reactTag:self.reactTag
text:nil
key:text
eventCount:_nativeEventCount];
if (_blurOnSubmit && [text isEqualToString:@"\n"]) {
// TODO: the purpose of blurOnSubmit on RCTextField is to decide if the
// field should lose focus when return is pressed or not. We're cheating a
// bit here by using it on RCTextView to decide if return character should
// submit the form, or be entered into the field.
//
// The reason this is cheating is because there's no way to specify that
// you want the return key to be swallowed *and* have the field retain
// focus (which was what blurOnSubmit was originally for). For the case
// where _blurOnSubmit = YES, this is still the correct and expected
// behavior though, so we'll leave the don't-blur-or-add-newline problem
// to be solved another day.
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeSubmit
reactTag:self.reactTag
text:self.text
key:nil
eventCount:_nativeEventCount];
[self resignFirstResponder];
return NO;
}
}
if (_maxLength == nil) {

View File

@@ -25,6 +25,7 @@ RCT_EXPORT_MODULE()
RCT_REMAP_VIEW_PROPERTY(autoCapitalize, textView.autocapitalizationType, UITextAutocapitalizationType)
RCT_EXPORT_VIEW_PROPERTY(autoCorrect, BOOL)
RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL)
RCT_EXPORT_VIEW_PROPERTY(clearTextOnFocus, BOOL)
RCT_REMAP_VIEW_PROPERTY(color, textView.textColor, UIColor)
RCT_REMAP_VIEW_PROPERTY(editable, textView.editable, BOOL)