mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-06-10 07:10:30 +08:00
Summary: There's an inconsistency in autoCorrect's default state: - If you don't specify a value for autoCorrect, it defaults to on. - If you specify true/false for autoCorrect and later specify null, autoCorrect turns off. It should have reverted to its initial state of on. The reason for this discrepancy is that autoCorrect is exposed to JS as a boolean but it is actually an enum with three states in native: - UITextAutocorrectionTypeDefault (the default value) - UITextAutocorrectionTypeYes - UITextAutocorrectionTypeNo This is fixed by explicitly mapping JS null to UITextAutocorrectionTypeDefault. **Test plan (required)** Verified that switching `autoCorrect` between `true`, `false`, and `null` all work correctly in single line and multiline `TextInputs`. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/11055 Differential Revision: D4226419 Pulled By: javache fbshipit-source-id: e3e5769a3aa537f00fb56ca4ae622ff4213481c5
129 lines
4.9 KiB
Objective-C
129 lines
4.9 KiB
Objective-C
/**
|
|
* 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.
|
|
*/
|
|
|
|
#import "RCTTextFieldManager.h"
|
|
|
|
#import "RCTBridge.h"
|
|
#import "RCTShadowView.h"
|
|
#import "RCTTextField.h"
|
|
#import "RCTFont.h"
|
|
#import "RCTConvert+Text.h"
|
|
|
|
@interface RCTTextFieldManager() <UITextFieldDelegate>
|
|
|
|
@end
|
|
|
|
@implementation RCTTextFieldManager
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
- (UIView *)view
|
|
{
|
|
RCTTextField *textField = [[RCTTextField alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
|
|
textField.delegate = self;
|
|
return textField;
|
|
}
|
|
|
|
- (BOOL)textField:(RCTTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
|
|
{
|
|
// Only allow single keypresses for onKeyPress, pasted text will not be sent.
|
|
if (textField.textWasPasted) {
|
|
textField.textWasPasted = NO;
|
|
} else {
|
|
[textField sendKeyValueForString:string];
|
|
}
|
|
|
|
if (textField.maxLength == nil || [string isEqualToString:@"\n"]) { // Make sure forms can be submitted via return
|
|
return YES;
|
|
}
|
|
NSUInteger allowedLength = textField.maxLength.integerValue - MIN(textField.maxLength.integerValue, textField.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 = 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;
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
- (BOOL)textFieldShouldEndEditing:(RCTTextField *)textField
|
|
{
|
|
return [textField textFieldShouldEndEditing:textField];
|
|
}
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(caretHidden, BOOL)
|
|
RCT_REMAP_VIEW_PROPERTY(autoCorrect, autocorrectionType, UITextAutocorrectionType)
|
|
RCT_REMAP_VIEW_PROPERTY(editable, enabled, BOOL)
|
|
RCT_EXPORT_VIEW_PROPERTY(placeholder, NSString)
|
|
RCT_EXPORT_VIEW_PROPERTY(placeholderTextColor, UIColor)
|
|
RCT_EXPORT_VIEW_PROPERTY(selection, RCTTextSelection)
|
|
RCT_EXPORT_VIEW_PROPERTY(text, NSString)
|
|
RCT_EXPORT_VIEW_PROPERTY(maxLength, NSNumber)
|
|
RCT_EXPORT_VIEW_PROPERTY(clearButtonMode, UITextFieldViewMode)
|
|
RCT_REMAP_VIEW_PROPERTY(clearTextOnFocus, clearsOnBeginEditing, BOOL)
|
|
RCT_EXPORT_VIEW_PROPERTY(selectTextOnFocus, BOOL)
|
|
RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL)
|
|
RCT_EXPORT_VIEW_PROPERTY(keyboardType, UIKeyboardType)
|
|
RCT_EXPORT_VIEW_PROPERTY(keyboardAppearance, UIKeyboardAppearance)
|
|
RCT_EXPORT_VIEW_PROPERTY(onSelectionChange, RCTDirectEventBlock)
|
|
RCT_EXPORT_VIEW_PROPERTY(returnKeyType, UIReturnKeyType)
|
|
RCT_EXPORT_VIEW_PROPERTY(enablesReturnKeyAutomatically, BOOL)
|
|
RCT_EXPORT_VIEW_PROPERTY(secureTextEntry, BOOL)
|
|
RCT_REMAP_VIEW_PROPERTY(password, secureTextEntry, BOOL) // backwards compatibility
|
|
RCT_REMAP_VIEW_PROPERTY(color, textColor, UIColor)
|
|
RCT_REMAP_VIEW_PROPERTY(autoCapitalize, autocapitalizationType, UITextAutocapitalizationType)
|
|
RCT_REMAP_VIEW_PROPERTY(textAlign, textAlignment, NSTextAlignment)
|
|
RCT_REMAP_VIEW_PROPERTY(selectionColor, tintColor, UIColor)
|
|
RCT_CUSTOM_VIEW_PROPERTY(fontSize, NSNumber, RCTTextField)
|
|
{
|
|
view.font = [RCTFont updateFont:view.font withSize:json ?: @(defaultView.font.pointSize)];
|
|
}
|
|
RCT_CUSTOM_VIEW_PROPERTY(fontWeight, NSString, __unused RCTTextField)
|
|
{
|
|
view.font = [RCTFont updateFont:view.font withWeight:json]; // defaults to normal
|
|
}
|
|
RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSString, __unused RCTTextField)
|
|
{
|
|
view.font = [RCTFont updateFont:view.font withStyle:json]; // defaults to normal
|
|
}
|
|
RCT_CUSTOM_VIEW_PROPERTY(fontFamily, NSString, RCTTextField)
|
|
{
|
|
view.font = [RCTFont updateFont:view.font withFamily:json ?: defaultView.font.familyName];
|
|
}
|
|
RCT_EXPORT_VIEW_PROPERTY(mostRecentEventCount, NSInteger)
|
|
|
|
- (RCTViewManagerUIBlock)uiBlockToAmendWithShadowView:(RCTShadowView *)shadowView
|
|
{
|
|
NSNumber *reactTag = shadowView.reactTag;
|
|
UIEdgeInsets padding = shadowView.paddingAsInsets;
|
|
return ^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTTextField *> *viewRegistry) {
|
|
viewRegistry[reactTag].contentInset = padding;
|
|
};
|
|
}
|
|
|
|
@end
|