2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTTextField.h"
|
|
|
|
|
|
|
|
#import "RCTConvert.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "RCTUtils.h"
|
2015-03-26 09:58:06 +00:00
|
|
|
#import "UIView+React.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
@implementation RCTTextField
|
|
|
|
{
|
|
|
|
RCTEventDispatcher *_eventDispatcher;
|
|
|
|
NSMutableArray *_reactSubviews;
|
|
|
|
BOOL _jsRequestingFirstResponder;
|
2015-07-21 19:37:24 +00:00
|
|
|
NSInteger _nativeEventCount;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
|
|
|
|
{
|
|
|
|
if ((self = [super initWithFrame:CGRectZero])) {
|
2015-06-15 14:53:45 +00:00
|
|
|
RCTAssert(eventDispatcher, @"eventDispatcher is a required parameter");
|
2015-02-20 04:10:52 +00:00
|
|
|
_eventDispatcher = eventDispatcher;
|
2015-08-06 16:53:20 +00:00
|
|
|
[self addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
|
|
|
|
[self addTarget:self action:@selector(textFieldBeginEditing) forControlEvents:UIControlEventEditingDidBegin];
|
|
|
|
[self addTarget:self action:@selector(textFieldEndEditing) forControlEvents:UIControlEventEditingDidEnd];
|
|
|
|
[self addTarget:self action:@selector(textFieldSubmitEditing) forControlEvents:UIControlEventEditingDidEndOnExit];
|
2015-08-17 14:35:34 +00:00
|
|
|
_reactSubviews = [NSMutableArray new];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(-initWithFrame:(CGRect)frame)
|
|
|
|
RCT_NOT_IMPLEMENTED(-initWithCoder:(NSCoder *)aDecoder)
|
|
|
|
|
2015-04-10 00:40:30 +00:00
|
|
|
- (void)setText:(NSString *)text
|
|
|
|
{
|
2015-07-21 19:37:24 +00:00
|
|
|
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
|
|
|
|
if (eventLag == 0 && ![text isEqualToString:self.text]) {
|
|
|
|
UITextRange *selection = self.selectedTextRange;
|
2015-04-10 00:40:30 +00:00
|
|
|
[super setText:text];
|
2015-07-21 19:37:24 +00:00
|
|
|
self.selectedTextRange = selection; // maintain cursor position/selection - this is robust to out of bounds
|
|
|
|
} else if (eventLag > RCTTextUpdateLagWarningThreshold) {
|
2015-08-11 13:37:12 +00:00
|
|
|
RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", self.text, eventLag);
|
2015-04-10 00:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 17:24:49 +00:00
|
|
|
static void RCTUpdatePlaceholder(RCTTextField *self)
|
|
|
|
{
|
|
|
|
if (self.placeholder.length > 0 && self.placeholderTextColor) {
|
|
|
|
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder
|
|
|
|
attributes:@{
|
|
|
|
NSForegroundColorAttributeName : self.placeholderTextColor
|
|
|
|
}];
|
|
|
|
} else if (self.placeholder.length) {
|
|
|
|
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor
|
|
|
|
{
|
2015-04-27 17:24:49 +00:00
|
|
|
_placeholderTextColor = placeholderTextColor;
|
|
|
|
RCTUpdatePlaceholder(self);
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)setPlaceholder:(NSString *)placeholder
|
|
|
|
{
|
2015-04-27 17:24:49 +00:00
|
|
|
super.placeholder = placeholder;
|
|
|
|
RCTUpdatePlaceholder(self);
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (NSArray *)reactSubviews
|
|
|
|
{
|
|
|
|
// TODO: do we support subviews of textfield in React?
|
|
|
|
// In any case, we should have a better approach than manually
|
|
|
|
// maintaining array in each view subclass like this
|
|
|
|
return _reactSubviews;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
|
|
|
{
|
2015-03-01 23:33:55 +00:00
|
|
|
// TODO: this is a bit broken - if the TextField inserts any of
|
2015-03-10 02:52:39 +00:00
|
|
|
// its own views below or between React's, the indices won't match
|
2015-02-20 04:10:52 +00:00
|
|
|
[_reactSubviews removeObject:subview];
|
|
|
|
[subview removeFromSuperview];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
|
|
|
|
{
|
2015-03-01 23:33:55 +00:00
|
|
|
// TODO: this is a bit broken - if the TextField inserts any of
|
2015-03-10 02:52:39 +00:00
|
|
|
// its own views below or between React's, the indices won't match
|
2015-02-20 04:10:52 +00:00
|
|
|
[_reactSubviews insertObject:view atIndex:atIndex];
|
|
|
|
[super insertSubview:view atIndex:atIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGRect)caretRectForPosition:(UITextPosition *)position
|
|
|
|
{
|
|
|
|
if (_caretHidden) {
|
|
|
|
return CGRectZero;
|
|
|
|
}
|
|
|
|
return [super caretRectForPosition:position];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGRect)textRectForBounds:(CGRect)bounds
|
|
|
|
{
|
|
|
|
CGRect rect = [super textRectForBounds:bounds];
|
2015-03-27 16:36:33 +00:00
|
|
|
return UIEdgeInsetsInsetRect(rect, _contentInset);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CGRect)editingRectForBounds:(CGRect)bounds
|
|
|
|
{
|
|
|
|
return [self textRectForBounds:bounds];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAutoCorrect:(BOOL)autoCorrect
|
|
|
|
{
|
2015-03-10 02:52:39 +00:00
|
|
|
self.autocorrectionType = (autoCorrect ? UITextAutocorrectionTypeYes : UITextAutocorrectionTypeNo);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)autoCorrect
|
|
|
|
{
|
|
|
|
return self.autocorrectionType == UITextAutocorrectionTypeYes;
|
|
|
|
}
|
|
|
|
|
2015-08-06 16:53:20 +00:00
|
|
|
- (void)textFieldDidChange
|
2015-07-21 19:37:24 +00:00
|
|
|
{
|
|
|
|
_nativeEventCount++;
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeChange
|
|
|
|
reactTag:self.reactTag
|
|
|
|
text:self.text
|
|
|
|
eventCount:_nativeEventCount];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 16:53:20 +00:00
|
|
|
- (void)textFieldEndEditing
|
2015-07-21 19:37:24 +00:00
|
|
|
{
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeEnd
|
|
|
|
reactTag:self.reactTag
|
|
|
|
text:self.text
|
|
|
|
eventCount:_nativeEventCount];
|
|
|
|
}
|
2015-08-06 16:53:20 +00:00
|
|
|
- (void)textFieldSubmitEditing
|
2015-07-21 19:37:24 +00:00
|
|
|
{
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeSubmit
|
|
|
|
reactTag:self.reactTag
|
|
|
|
text:self.text
|
|
|
|
eventCount:_nativeEventCount];
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-08-06 16:53:20 +00:00
|
|
|
- (void)textFieldBeginEditing
|
2015-04-15 00:51:28 +00:00
|
|
|
{
|
|
|
|
if (_selectTextOnFocus) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[self selectAll:nil];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeFocus
|
|
|
|
reactTag:self.reactTag
|
2015-07-21 19:37:24 +00:00
|
|
|
text:self.text
|
|
|
|
eventCount:_nativeEventCount];
|
2015-04-15 00:51:28 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (BOOL)becomeFirstResponder
|
|
|
|
{
|
2015-04-15 00:51:28 +00:00
|
|
|
_jsRequestingFirstResponder = YES;
|
2015-02-20 04:10:52 +00:00
|
|
|
BOOL result = [super becomeFirstResponder];
|
|
|
|
_jsRequestingFirstResponder = NO;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)resignFirstResponder
|
|
|
|
{
|
|
|
|
BOOL result = [super resignFirstResponder];
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeBlur
|
|
|
|
reactTag:self.reactTag
|
2015-07-21 19:37:24 +00:00
|
|
|
text:self.text
|
|
|
|
eventCount:_nativeEventCount];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)canBecomeFirstResponder
|
|
|
|
{
|
|
|
|
return _jsRequestingFirstResponder;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|