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"
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTEventDispatcher.h>
|
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
#import <React/UIView+React.h>
|
|
|
|
|
2016-08-26 00:18:05 +00:00
|
|
|
#import "RCTTextSelection.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2017-01-30 23:36:57 +00:00
|
|
|
@interface RCTTextField()
|
|
|
|
|
|
|
|
- (BOOL)shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
|
|
|
|
- (BOOL)keyboardInputShouldDelete;
|
|
|
|
- (BOOL)textFieldShouldEndEditing;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface RCTTextFieldDelegateProxy: NSObject <UITextFieldDelegate>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTTextFieldDelegateProxy
|
|
|
|
|
|
|
|
- (BOOL)textField:(RCTTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
|
|
|
|
{
|
|
|
|
return [textField shouldChangeCharactersInRange:range replacementString:string];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)keyboardInputShouldDelete:(RCTTextField *)textField
|
|
|
|
{
|
|
|
|
return [textField keyboardInputShouldDelete];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField {
|
|
|
|
return [textField textFieldShouldEndEditing];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@implementation RCTTextField
|
|
|
|
{
|
|
|
|
RCTEventDispatcher *_eventDispatcher;
|
|
|
|
BOOL _jsRequestingFirstResponder;
|
2015-07-21 19:37:24 +00:00
|
|
|
NSInteger _nativeEventCount;
|
2015-11-05 05:01:49 +00:00
|
|
|
BOOL _submitted;
|
2015-11-14 17:41:59 +00:00
|
|
|
UITextRange *_previousSelectionRange;
|
2017-01-30 23:36:57 +00:00
|
|
|
BOOL _textWasPasted;
|
|
|
|
NSString *_finalText;
|
|
|
|
RCTTextFieldDelegateProxy *_delegateProxy;
|
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-11-14 17:41:59 +00:00
|
|
|
[self addObserver:self forKeyPath:@"selectedTextRange" options:0 context:nil];
|
2015-12-02 15:11:20 +00:00
|
|
|
_blurOnSubmit = YES;
|
2017-01-30 23:36:57 +00:00
|
|
|
|
|
|
|
// We cannot use `self.delegate = self;` here because `UITextField` implements some of these delegate methods itself,
|
|
|
|
// so if we implement this delegate on self, we will override some of its behaviours.
|
|
|
|
_delegateProxy = [RCTTextFieldDelegateProxy new];
|
|
|
|
self.delegate = _delegateProxy;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-11-14 17:41:59 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self removeObserver:self forKeyPath:@"selectedTextRange"];
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-11-02 17:13:41 +00:00
|
|
|
- (void)sendKeyValueForString:(NSString *)string
|
|
|
|
{
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeKeyPress
|
|
|
|
reactTag:self.reactTag
|
|
|
|
text:nil
|
|
|
|
key:string
|
|
|
|
eventCount:_nativeEventCount];
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:26:07 +00:00
|
|
|
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
|
|
|
|
{
|
|
|
|
[super didUpdateFocusInContext:context withAnimationCoordinator:coordinator];
|
|
|
|
if(context.nextFocusedView == self) {
|
|
|
|
_jsRequestingFirstResponder = YES;
|
|
|
|
} else {
|
|
|
|
_jsRequestingFirstResponder = NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:08:39 +00:00
|
|
|
// This method is overridden for `onKeyPress`. The manager
|
2015-11-02 17:13:41 +00:00
|
|
|
// will not send a keyPress for text that was pasted.
|
|
|
|
- (void)paste:(id)sender
|
|
|
|
{
|
|
|
|
_textWasPasted = YES;
|
|
|
|
[super paste:sender];
|
|
|
|
}
|
|
|
|
|
2016-08-26 00:18:05 +00:00
|
|
|
- (void)setSelection:(RCTTextSelection *)selection
|
|
|
|
{
|
|
|
|
if (!selection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UITextRange *currentSelection = self.selectedTextRange;
|
|
|
|
UITextPosition *start = [self positionFromPosition:self.beginningOfDocument offset:selection.start];
|
|
|
|
UITextPosition *end = [self positionFromPosition:self.beginningOfDocument offset:selection.end];
|
|
|
|
UITextRange *selectedTextRange = [self textRangeFromPosition:start toPosition:end];
|
|
|
|
|
|
|
|
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
|
|
|
|
if (eventLag == 0 && ![currentSelection isEqual:selectedTextRange]) {
|
|
|
|
_previousSelectionRange = selectedTextRange;
|
|
|
|
self.selectedTextRange = selectedTextRange;
|
|
|
|
} else if (eventLag > RCTTextUpdateLagWarningThreshold) {
|
|
|
|
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
|
|
|
- (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-12-17 18:22:56 +00:00
|
|
|
NSInteger oldTextLength = self.text.length;
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
super.text = text;
|
2015-12-17 18:22:56 +00:00
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
2015-07-21 19:37:24 +00:00
|
|
|
} 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
|
|
|
- (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];
|
|
|
|
}
|
|
|
|
|
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
|
2015-11-02 17:13:41 +00:00
|
|
|
key:nil
|
2015-07-21 19:37:24 +00:00
|
|
|
eventCount:_nativeEventCount];
|
2015-11-14 17:41:59 +00:00
|
|
|
|
|
|
|
// selectedTextRange observer isn't triggered when you type even though the
|
|
|
|
// cursor position moves, so we send event again here.
|
|
|
|
[self sendSelectionEvent];
|
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
|
|
|
{
|
2017-01-30 23:36:57 +00:00
|
|
|
if (![_finalText isEqualToString:self.text]) {
|
|
|
|
_finalText = nil;
|
|
|
|
// iOS does't send event `UIControlEventEditingChanged` if the change was happened because of autocorrection
|
|
|
|
// which was triggered by loosing focus. We assume that if `text` was changed in the middle of loosing focus process,
|
|
|
|
// we did not receive that event. So, we call `textFieldDidChange` manually.
|
|
|
|
[self textFieldDidChange];
|
|
|
|
}
|
|
|
|
|
2015-07-21 19:37:24 +00:00
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeEnd
|
|
|
|
reactTag:self.reactTag
|
|
|
|
text:self.text
|
2015-11-02 17:13:41 +00:00
|
|
|
key:nil
|
2015-07-21 19:37:24 +00:00
|
|
|
eventCount:_nativeEventCount];
|
|
|
|
}
|
2017-02-02 17:51:19 +00:00
|
|
|
|
2015-08-06 16:53:20 +00:00
|
|
|
- (void)textFieldSubmitEditing
|
2015-07-21 19:37:24 +00:00
|
|
|
{
|
2015-11-05 05:01:49 +00:00
|
|
|
_submitted = YES;
|
2015-07-21 19:37:24 +00:00
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeSubmit
|
|
|
|
reactTag:self.reactTag
|
|
|
|
text:self.text
|
2015-11-02 17:13:41 +00:00
|
|
|
key:nil
|
2015-07-21 19:37:24 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeFocus
|
|
|
|
reactTag:self.reactTag
|
2015-07-21 19:37:24 +00:00
|
|
|
text:self.text
|
2015-11-02 17:13:41 +00:00
|
|
|
key:nil
|
2015-07-21 19:37:24 +00:00
|
|
|
eventCount:_nativeEventCount];
|
2016-08-26 00:18:05 +00:00
|
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
if (self->_selectTextOnFocus) {
|
|
|
|
[self selectAll:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self sendSelectionEvent];
|
|
|
|
});
|
2015-04-15 00:51:28 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 17:41:59 +00:00
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath
|
|
|
|
ofObject:(RCTTextField *)textField
|
|
|
|
change:(NSDictionary *)change
|
|
|
|
context:(void *)context
|
|
|
|
{
|
|
|
|
if ([keyPath isEqualToString:@"selectedTextRange"]) {
|
|
|
|
[self sendSelectionEvent];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sendSelectionEvent
|
|
|
|
{
|
|
|
|
if (_onSelectionChange &&
|
|
|
|
self.selectedTextRange != _previousSelectionRange &&
|
|
|
|
![self.selectedTextRange isEqual:_previousSelectionRange]) {
|
|
|
|
|
|
|
|
_previousSelectionRange = self.selectedTextRange;
|
|
|
|
|
|
|
|
UITextRange *selection = self.selectedTextRange;
|
|
|
|
NSInteger start = [self offsetFromPosition:[self beginningOfDocument] toPosition:selection.start];
|
|
|
|
NSInteger end = [self offsetFromPosition:[self beginningOfDocument] toPosition:selection.end];
|
|
|
|
_onSelectionChange(@{
|
|
|
|
@"selection": @{
|
|
|
|
@"start": @(start),
|
|
|
|
@"end": @(end),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 15:08:29 +00:00
|
|
|
- (BOOL)canBecomeFirstResponder
|
|
|
|
{
|
|
|
|
return _jsRequestingFirstResponder;
|
|
|
|
}
|
|
|
|
|
2015-11-27 14:52:29 +00:00
|
|
|
- (void)reactWillMakeFirstResponder
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-15 00:51:28 +00:00
|
|
|
_jsRequestingFirstResponder = YES;
|
2015-11-27 14:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reactDidMakeFirstResponder
|
|
|
|
{
|
2015-02-20 04:10:52 +00:00
|
|
|
_jsRequestingFirstResponder = NO;
|
2015-11-27 14:52:29 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (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
|
2015-11-02 17:13:41 +00:00
|
|
|
key:nil
|
2015-07-21 19:37:24 +00:00
|
|
|
eventCount:_nativeEventCount];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-30 23:36:57 +00:00
|
|
|
#pragma mark - UITextFieldDelegate (Proxied)
|
|
|
|
|
|
|
|
- (BOOL)shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
|
|
|
|
{
|
|
|
|
// Only allow single keypresses for `onKeyPress`, pasted text will not be sent.
|
|
|
|
if (_textWasPasted) {
|
|
|
|
_textWasPasted = NO;
|
|
|
|
} else {
|
|
|
|
[self sendKeyValueForString:string];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_maxLength != nil && ![string isEqualToString:@"\n"]) { // Make sure forms can be submitted via return.
|
|
|
|
NSUInteger allowedLength = _maxLength.integerValue - MIN(_maxLength.integerValue, self.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 = self.text.mutableCopy;
|
|
|
|
[newString replaceCharactersInRange:range withString:limitedString];
|
|
|
|
self.text = newString;
|
|
|
|
|
|
|
|
// Collapse selection at end of insert to match normal paste behavior.
|
|
|
|
UITextPosition *insertEnd = [self positionFromPosition:self.beginningOfDocument
|
|
|
|
offset:(range.location + allowedLength)];
|
|
|
|
self.selectedTextRange = [self textRangeFromPosition:insertEnd toPosition:insertEnd];
|
|
|
|
[self textFieldDidChange];
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method allows us to detect a `Backspace` keyPress
|
|
|
|
// even when there is no more text in the TextField.
|
|
|
|
- (BOOL)keyboardInputShouldDelete
|
|
|
|
{
|
|
|
|
[self shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldEndEditing
|
|
|
|
{
|
|
|
|
_finalText = self.text;
|
|
|
|
|
|
|
|
if (_submitted) {
|
|
|
|
_submitted = NO;
|
|
|
|
return _blurOnSubmit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@end
|