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"
|
|
|
|
|
2017-05-29 22:56:38 +00:00
|
|
|
#import <React/RCTBridge.h>
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTEventDispatcher.h>
|
2017-05-29 22:56:38 +00:00
|
|
|
#import <React/RCTUIManager.h>
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
#import <React/UIView+React.h>
|
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
#import "RCTBackedTextInputDelegate.h"
|
2016-08-26 00:18:05 +00:00
|
|
|
#import "RCTTextSelection.h"
|
2017-05-29 22:56:47 +00:00
|
|
|
#import "RCTUITextField.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
@interface RCTTextField () <RCTBackedTextInputDelegate>
|
2017-01-30 23:36:57 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@implementation RCTTextField
|
|
|
|
{
|
2017-07-18 21:33:31 +00:00
|
|
|
RCTUITextField *_backedTextInput;
|
2015-11-05 05:01:49 +00:00
|
|
|
BOOL _submitted;
|
2017-05-29 22:56:47 +00:00
|
|
|
CGSize _previousContentSize;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 22:56:38 +00:00
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2017-06-27 23:05:05 +00:00
|
|
|
if (self = [super initWithBridge:bridge]) {
|
2017-06-10 15:19:34 +00:00
|
|
|
// `blurOnSubmit` defaults to `true` for <TextInput multiline={false}> by design.
|
|
|
|
_blurOnSubmit = YES;
|
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput = [[RCTUITextField alloc] initWithFrame:self.bounds];
|
|
|
|
_backedTextInput.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
_backedTextInput.textInputDelegate = self;
|
2017-05-29 22:56:47 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
[self addSubview:_backedTextInput];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2017-05-29 22:56:47 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-05-29 22:56:47 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
|
|
|
|
2017-06-27 23:05:04 +00:00
|
|
|
- (id<RCTBackedTextInputViewProtocol>)backedTextInputView
|
|
|
|
{
|
2017-07-18 21:33:31 +00:00
|
|
|
return _backedTextInput;
|
2017-06-27 23:05:04 +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];
|
|
|
|
}
|
|
|
|
|
2017-05-29 22:56:47 +00:00
|
|
|
#pragma mark - Properties
|
|
|
|
|
|
|
|
- (NSString *)text
|
|
|
|
{
|
2017-07-18 21:33:31 +00:00
|
|
|
return _backedTextInput.text;
|
2017-05-29 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
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]) {
|
2017-07-18 21:33:31 +00:00
|
|
|
UITextRange *selection = _backedTextInput.selectedTextRange;
|
|
|
|
NSInteger oldTextLength = _backedTextInput.text.length;
|
2015-12-17 18:22:56 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.text = text;
|
2015-12-17 18:22:56 +00:00
|
|
|
|
|
|
|
if (selection.empty) {
|
|
|
|
// maintain cursor position relative to the end of the old text
|
2017-07-18 21:33:31 +00:00
|
|
|
NSInteger offsetStart = [_backedTextInput offsetFromPosition:_backedTextInput.beginningOfDocument toPosition:selection.start];
|
2015-12-17 18:22:56 +00:00
|
|
|
NSInteger offsetFromEnd = oldTextLength - offsetStart;
|
|
|
|
NSInteger newOffset = text.length - offsetFromEnd;
|
2017-07-18 21:33:31 +00:00
|
|
|
UITextPosition *position = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:newOffset];
|
2017-07-18 21:33:45 +00:00
|
|
|
[_backedTextInput setSelectedTextRange:[_backedTextInput textRangeFromPosition:position toPosition:position]
|
|
|
|
notifyDelegate:YES];
|
2015-12-17 18:22:56 +00:00
|
|
|
}
|
2015-07-21 19:37:24 +00:00
|
|
|
} else if (eventLag > RCTTextUpdateLagWarningThreshold) {
|
2017-07-18 21:33:31 +00:00
|
|
|
RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", _backedTextInput.text, eventLag);
|
2015-04-10 00:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
#pragma mark - RCTBackedTextInputDelegate
|
2017-01-30 23:36:57 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
- (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string
|
2017-01-30 23:36:57 +00:00
|
|
|
{
|
|
|
|
// Only allow single keypresses for `onKeyPress`, pasted text will not be sent.
|
2017-07-18 21:33:31 +00:00
|
|
|
if (!_backedTextInput.textWasPasted) {
|
2017-01-30 23:36:57 +00:00
|
|
|
[self sendKeyValueForString:string];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_maxLength != nil && ![string isEqualToString:@"\n"]) { // Make sure forms can be submitted via return.
|
2017-07-18 21:33:31 +00:00
|
|
|
NSUInteger allowedLength = _maxLength.integerValue - MIN(_maxLength.integerValue, _backedTextInput.text.length) + range.length;
|
2017-01-30 23:36:57 +00:00
|
|
|
if (string.length > allowedLength) {
|
|
|
|
if (string.length > 1) {
|
|
|
|
// Truncate the input string so the result is exactly `maxLength`.
|
|
|
|
NSString *limitedString = [string substringToIndex:allowedLength];
|
2017-07-18 21:33:31 +00:00
|
|
|
NSMutableString *newString = _backedTextInput.text.mutableCopy;
|
2017-01-30 23:36:57 +00:00
|
|
|
[newString replaceCharactersInRange:range withString:limitedString];
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.text = newString;
|
2017-01-30 23:36:57 +00:00
|
|
|
|
|
|
|
// Collapse selection at end of insert to match normal paste behavior.
|
2017-07-18 21:33:31 +00:00
|
|
|
UITextPosition *insertEnd = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument
|
2017-05-29 22:56:47 +00:00
|
|
|
offset:(range.location + allowedLength)];
|
2017-07-18 21:33:45 +00:00
|
|
|
[_backedTextInput setSelectedTextRange:[_backedTextInput textRangeFromPosition:insertEnd toPosition:insertEnd]
|
|
|
|
notifyDelegate:YES];
|
2017-07-18 21:33:31 +00:00
|
|
|
[self textInputDidChange];
|
2017-01-30 23:36:57 +00:00
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
- (void)textInputDidChange
|
|
|
|
{
|
|
|
|
_nativeEventCount++;
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeChange
|
|
|
|
reactTag:self.reactTag
|
|
|
|
text:_backedTextInput.text
|
|
|
|
key:nil
|
|
|
|
eventCount:_nativeEventCount];
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@end
|