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 "RCTTextFieldManager.h"
|
|
|
|
|
2015-03-17 10:51:58 +00:00
|
|
|
#import "RCTBridge.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTShadowView.h"
|
|
|
|
#import "RCTTextField.h"
|
2016-08-05 19:29:19 +00:00
|
|
|
#import "RCTFont.h"
|
2016-11-23 13:41:07 +00:00
|
|
|
#import "RCTConvert+Text.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-08-06 16:53:20 +00:00
|
|
|
@interface RCTTextFieldManager() <UITextFieldDelegate>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@implementation RCTTextFieldManager
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (UIView *)view
|
|
|
|
{
|
2015-08-06 16:53:20 +00:00
|
|
|
RCTTextField *textField = [[RCTTextField alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
|
|
|
|
textField.delegate = self;
|
|
|
|
return textField;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)textField:(RCTTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
|
|
|
|
{
|
2015-11-02 17:13:41 +00:00
|
|
|
// Only allow single keypresses for onKeyPress, pasted text will not be sent.
|
|
|
|
if (textField.textWasPasted) {
|
|
|
|
textField.textWasPasted = NO;
|
|
|
|
} else {
|
|
|
|
[textField sendKeyValueForString:string];
|
|
|
|
}
|
|
|
|
|
2015-08-06 16:53:20 +00:00
|
|
|
if (textField.maxLength == nil || [string isEqualToString:@"\n"]) { // Make sure forms can be submitted via return
|
|
|
|
return YES;
|
|
|
|
}
|
2016-10-18 15:01:00 +00:00
|
|
|
NSUInteger allowedLength = textField.maxLength.integerValue - MIN(textField.maxLength.integerValue, textField.text.length) + range.length;
|
2015-08-06 16:53:20 +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];
|
|
|
|
NSMutableString *newString = textField.text.mutableCopy;
|
|
|
|
[newString replaceCharactersInRange:range withString:limitedString];
|
|
|
|
textField.text = newString;
|
|
|
|
// Collapse selection at end of insert to match normal paste behavior
|
|
|
|
UITextPosition *insertEnd = [textField positionFromPosition:textField.beginningOfDocument
|
|
|
|
offset:(range.location + allowedLength)];
|
|
|
|
textField.selectedTextRange = [textField textRangeFromPosition:insertEnd toPosition:insertEnd];
|
|
|
|
[textField textFieldDidChange];
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
} else {
|
|
|
|
return YES;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 17:13:41 +00:00
|
|
|
// This method allows us to detect a `Backspace` keyPress
|
|
|
|
// even when there is no more text in the TextField
|
|
|
|
- (BOOL)keyboardInputShouldDelete:(RCTTextField *)textField
|
|
|
|
{
|
|
|
|
[self textField:textField shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2015-11-05 05:01:49 +00:00
|
|
|
- (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField
|
|
|
|
{
|
|
|
|
return [textField textFieldShouldEndEditing:textField];
|
|
|
|
}
|
|
|
|
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(caretHidden, BOOL)
|
2016-11-23 13:41:07 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(autoCorrect, autocorrectionType, UITextAutocorrectionType)
|
2015-06-05 15:46:17 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(editable, enabled, BOOL)
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(placeholder, NSString)
|
2015-04-27 17:24:49 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(placeholderTextColor, UIColor)
|
2016-08-26 00:18:05 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(selection, RCTTextSelection)
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(text, NSString)
|
2015-07-21 19:37:24 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(maxLength, NSNumber)
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(clearButtonMode, UITextFieldViewMode)
|
2015-04-15 00:51:28 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(clearTextOnFocus, clearsOnBeginEditing, BOOL)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(selectTextOnFocus, BOOL)
|
2015-11-05 05:01:49 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL)
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(keyboardType, UIKeyboardType)
|
2015-11-11 13:31:18 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(keyboardAppearance, UIKeyboardAppearance)
|
2015-11-14 17:41:59 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(onSelectionChange, RCTDirectEventBlock)
|
2015-03-31 01:25:30 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(returnKeyType, UIReturnKeyType)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(enablesReturnKeyAutomatically, BOOL)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(secureTextEntry, BOOL)
|
2015-06-05 15:46:17 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(password, secureTextEntry, BOOL) // backwards compatibility
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(color, textColor, UIColor)
|
|
|
|
RCT_REMAP_VIEW_PROPERTY(autoCapitalize, autocapitalizationType, UITextAutocapitalizationType)
|
2015-06-24 22:02:04 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(textAlign, textAlignment, NSTextAlignment)
|
2016-02-03 13:48:31 +00:00
|
|
|
RCT_REMAP_VIEW_PROPERTY(selectionColor, tintColor, UIColor)
|
2016-08-05 19:29:21 +00:00
|
|
|
RCT_CUSTOM_VIEW_PROPERTY(fontSize, NSNumber, RCTTextField)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2016-08-05 19:29:19 +00:00
|
|
|
view.font = [RCTFont updateFont:view.font withSize:json ?: @(defaultView.font.pointSize)];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-06-15 14:53:45 +00:00
|
|
|
RCT_CUSTOM_VIEW_PROPERTY(fontWeight, NSString, __unused RCTTextField)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2016-08-05 19:29:19 +00:00
|
|
|
view.font = [RCTFont updateFont:view.font withWeight:json]; // defaults to normal
|
2015-03-25 23:22:59 +00:00
|
|
|
}
|
2015-06-15 14:53:45 +00:00
|
|
|
RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSString, __unused RCTTextField)
|
2015-03-25 23:22:59 +00:00
|
|
|
{
|
2016-08-05 19:29:19 +00:00
|
|
|
view.font = [RCTFont updateFont:view.font withStyle:json]; // defaults to normal
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-03-26 04:29:28 +00:00
|
|
|
RCT_CUSTOM_VIEW_PROPERTY(fontFamily, NSString, RCTTextField)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2016-08-05 19:29:19 +00:00
|
|
|
view.font = [RCTFont updateFont:view.font withFamily:json ?: defaultView.font.familyName];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-07-21 19:37:24 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(mostRecentEventCount, NSInteger)
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
- (RCTViewManagerUIBlock)uiBlockToAmendWithShadowView:(RCTShadowView *)shadowView
|
|
|
|
{
|
|
|
|
NSNumber *reactTag = shadowView.reactTag;
|
|
|
|
UIEdgeInsets padding = shadowView.paddingAsInsets;
|
2015-11-14 18:25:00 +00:00
|
|
|
return ^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTTextField *> *viewRegistry) {
|
|
|
|
viewRegistry[reactTag].contentInset = padding;
|
2015-03-01 23:33:55 +00:00
|
|
|
};
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
@end
|