Add support for TextInput clearTextOnFocus and selectTextOnFocus (#1517)

* Enable `selectTextOnFocus` and `clearTextOnFocus` for TextInput

* Fix select all for multiline textview

* Only reset selected range for macOS

* Don't run else branch for iOS

* Fix tags

Co-authored-by: Shawn Dempsey <shawndempsey@fb.com>
Co-authored-by: chiuam <67026167+chiuam@users.noreply.github.com>
Co-authored-by: Saad Najmi <sanajmi@microsoft.com>
This commit is contained in:
Shawn Dempsey 2022-12-28 10:15:56 -08:00 коммит произвёл GitHub
Родитель b2c5fa9738
Коммит 71e7ccfc29
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 16 добавлений и 6 удалений

Просмотреть файл

@ -212,6 +212,10 @@ static RCTUIColor *defaultPlaceholderColor() // TODO(OSS Candidate ISS#2710739)
- (BOOL)resignFirstResponder - (BOOL)resignFirstResponder
{ {
if (self.selectable) {
self.selectedRange = NSMakeRange(NSNotFound, 0);
}
BOOL success = [super resignFirstResponder]; BOOL success = [super resignFirstResponder];
if (success) { if (success) {
@ -436,13 +440,13 @@ static RCTUIColor *defaultPlaceholderColor() // TODO(OSS Candidate ISS#2710739)
{ {
[super selectAll:sender]; [super selectAll:sender];
#if !TARGET_OS_OSX // TODO(macOS GH#774) For `selectTextOnFocus` prop, which isn't supported on macOS atm. #if !TARGET_OS_OSX // TODO(macOS GH#774)
// `selectAll:` does not work for UITextView when it's being called inside UITextView's delegate methods. // `selectAll:` does not work for UITextView when it's being called inside UITextView's delegate methods.
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
UITextRange *selectionRange = [self textRangeFromPosition:self.beginningOfDocument toPosition:self.endOfDocument]; UITextRange *selectionRange = [self textRangeFromPosition:self.beginningOfDocument toPosition:self.endOfDocument];
[self setSelectedTextRange:selectionRange notifyDelegate:NO]; [self setSelectedTextRange:selectionRange notifyDelegate:NO];
}); });
#endif #endif // TODO(macOS GH#774)
} }
#pragma mark - Layout #pragma mark - Layout

Просмотреть файл

@ -379,8 +379,14 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)decoder)
} }
if (_selectTextOnFocus) { if (_selectTextOnFocus) {
if ([self.backedTextInputView respondsToSelector:@selector(selectAll:)]) {
[self.backedTextInputView selectAll:nil]; [self.backedTextInputView selectAll:nil];
} }
#if TARGET_OS_OSX // [TODO(macOS GH#774)
} else {
[self.backedTextInputView setSelectedTextRange:NSMakeRange(NSNotFound, 0) notifyDelegate:NO];
#endif // ]TODO(macOS GH#774)
}
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeFocus [_eventDispatcher sendTextEventWithType:RCTTextEventTypeFocus
reactTag:self.reactTag reactTag:self.reactTag

Просмотреть файл

@ -56,11 +56,11 @@ RCT_REMAP_NOT_OSX_VIEW_PROPERTY(clearButtonMode, backedTextInputView.clearButton
RCT_REMAP_NOT_OSX_VIEW_PROPERTY(secureTextEntry, backedTextInputView.secureTextEntry, BOOL) // TODO(macOS GH#774) RCT_REMAP_NOT_OSX_VIEW_PROPERTY(secureTextEntry, backedTextInputView.secureTextEntry, BOOL) // TODO(macOS GH#774)
RCT_EXPORT_VIEW_PROPERTY(autoFocus, BOOL) RCT_EXPORT_VIEW_PROPERTY(autoFocus, BOOL)
RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL) RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL)
RCT_EXPORT_NOT_OSX_VIEW_PROPERTY(clearTextOnFocus, BOOL) // TODO(macOS GH#774) RCT_EXPORT_VIEW_PROPERTY(clearTextOnFocus, BOOL)
RCT_REMAP_NOT_OSX_VIEW_PROPERTY(keyboardType, backedTextInputView.keyboardType, UIKeyboardType) // TODO(macOS GH#774) RCT_REMAP_NOT_OSX_VIEW_PROPERTY(keyboardType, backedTextInputView.keyboardType, UIKeyboardType) // TODO(macOS GH#774)
RCT_EXPORT_NOT_OSX_VIEW_PROPERTY(showSoftInputOnFocus, BOOL) // TODO(macOS GH#774) RCT_EXPORT_NOT_OSX_VIEW_PROPERTY(showSoftInputOnFocus, BOOL) // TODO(macOS GH#774)
RCT_EXPORT_VIEW_PROPERTY(maxLength, NSNumber) RCT_EXPORT_VIEW_PROPERTY(maxLength, NSNumber)
RCT_EXPORT_NOT_OSX_VIEW_PROPERTY(selectTextOnFocus, BOOL) // TODO(macOS GH#774) RCT_EXPORT_VIEW_PROPERTY(selectTextOnFocus, BOOL)
RCT_EXPORT_VIEW_PROPERTY(selection, RCTTextSelection) RCT_EXPORT_VIEW_PROPERTY(selection, RCTTextSelection)
RCT_EXPORT_VIEW_PROPERTY(inputAccessoryViewID, NSString) RCT_EXPORT_VIEW_PROPERTY(inputAccessoryViewID, NSString)
RCT_EXPORT_VIEW_PROPERTY(textContentType, NSString) RCT_EXPORT_VIEW_PROPERTY(textContentType, NSString)