Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fabric] Shim RCTTextInput #1681

Merged
merged 3 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ @interface RCTTextInputComponentView () <RCTBackedTextInputDelegate, RCTTextInpu

@implementation RCTTextInputComponentView {
TextInputShadowNode::ConcreteState::Shared _state;
RCTUIView<RCTBackedTextInputViewProtocol> *_backedTextInputView; // [macOS]
#if !TARGET_OS_OSX // [macOS]
RCTUIView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
#else // [macOS
RCTUITextView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
#endif // macOS]
NSUInteger _mostRecentEventCount;
NSAttributedString *_lastStringStateWasUpdatedWith;

Expand Down Expand Up @@ -67,7 +71,11 @@ - (instancetype)initWithFrame:(CGRect)frame
_props = defaultProps;
auto &props = *defaultProps;

#if !TARGET_OS_OSX // [macOS]
_backedTextInputView = props.traits.multiline ? [RCTUITextView new] : [RCTUITextField new];
#else // [macOS
_backedTextInputView = [RCTUITextView new];
#endif // macOS]
_backedTextInputView.textInputDelegate = self;
_ignoreNextTextInputCall = NO;
_comingFromJS = NO;
Expand Down Expand Up @@ -478,25 +486,23 @@ - (void)setTextAndSelection:(NSInteger)eventCount

- (void)setDefaultInputAccessoryView
{
#if !TARGET_OS_OSX // [macOS]
// InputAccessoryView component sets the inputAccessoryView when inputAccessoryViewID exists
if (_backedTextInputView.inputAccessoryViewID) {
if (_backedTextInputView.isFirstResponder) {
#if !TARGET_OS_OSX // [macOS]
[_backedTextInputView reloadInputViews];
#endif // [macOS]
}
return;
}

#if !TARGET_OS_OSX // [macOS]
UIKeyboardType keyboardType = _backedTextInputView.keyboardType;

// These keyboard types (all are number pads) don't have a "Done" button by default,
// so we create an `inputAccessoryView` with this button for them.
BOOL shouldHaveInputAccesoryView =
(keyboardType == UIKeyboardTypeNumberPad || keyboardType == UIKeyboardTypePhonePad ||
keyboardType == UIKeyboardTypeDecimalPad || keyboardType == UIKeyboardTypeASCIICapableNumberPad) &&
_backedTextInputView.returnKeyType == UIReturnKeyDone;
(keyboardType == UIKeyboardTypeNumberPad || keyboardType == UIKeyboardTypePhonePad ||
keyboardType == UIKeyboardTypeDecimalPad || keyboardType == UIKeyboardTypeASCIICapableNumberPad) &&
_backedTextInputView.returnKeyType == UIReturnKeyDone;

if ((_backedTextInputView.inputAccessoryView != nil) == shouldHaveInputAccesoryView) {
return;
Expand All @@ -506,32 +512,30 @@ - (void)setDefaultInputAccessoryView
UIToolbar *toolbarView = [UIToolbar new];
[toolbarView sizeToFit];
UIBarButtonItem *flexibleSpace =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(handleInputAccessoryDoneButton)];
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(handleInputAccessoryDoneButton)];
toolbarView.items = @[ flexibleSpace, doneButton ];
_backedTextInputView.inputAccessoryView = toolbarView;
} else {
_backedTextInputView.inputAccessoryView = nil;
}
#endif // [macOS]


if (_backedTextInputView.isFirstResponder) {
#if !TARGET_OS_OSX // [macOS]
[_backedTextInputView reloadInputViews];
#endif // [macOS]
}
#endif // [macOS]
}

- (void)handleInputAccessoryDoneButton
{
if ([self textInputShouldReturn]) {
#if !TARGET_OS_OSX // [macOS]
if ([self textInputShouldReturn]) {
[_backedTextInputView endEditing:YES];
#endif // [macOS]
}
#endif // [macOS]
}

#pragma mark - Other
Expand Down Expand Up @@ -633,7 +637,11 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString
- (void)_setMultiline:(BOOL)multiline
{
[_backedTextInputView removeFromSuperview];
RCTUIView<RCTBackedTextInputViewProtocol> *backedTextInputView = multiline ? [RCTUITextView new] : [RCTUITextField new]; // [macOS]
#if !TARGET_OS_OSX // [macOS]
RCTUIView<RCTBackedTextInputViewProtocol> *backedTextInputView = multiline ? [RCTUITextView new] : [RCTUITextField new];
#else // [macOS
RCTUITextView<RCTBackedTextInputViewProtocol> *backedTextInputView = [RCTUITextView new];
#endif // macOS]
backedTextInputView.frame = _backedTextInputView.frame;
RCTCopyBackedTextInput(_backedTextInputView, backedTextInputView);
_backedTextInputView = backedTextInputView;
Expand Down Expand Up @@ -663,17 +671,19 @@ - (BOOL)_textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldTe
}];

BOOL shouldFallbackToBareTextComparison =
[_backedTextInputView.textInputMode.primaryLanguage isEqualToString:@"dictation"] ||
#if !TARGET_OS_OSX // [macOS]
[_backedTextInputView.textInputMode.primaryLanguage isEqualToString:@"ko-KR"] ||
_backedTextInputView.markedTextRange || _backedTextInputView.isSecureTextEntry || fontHasBeenUpdatedBySystem;
[_backedTextInputView.textInputMode.primaryLanguage isEqualToString:@"dictation"] ||
[_backedTextInputView.textInputMode.primaryLanguage isEqualToString:@"ko-KR"] ||
_backedTextInputView.markedTextRange ||
_backedTextInputView.isSecureTextEntry ||
#else // [macOS
// There are multiple Korean input sources (2-Set, 3-Set, etc). Check substring instead instead
[[[self.backedTextInputView inputContext] selectedKeyboardInputSource] containsString:@"com.apple.inputmethod.Korean"] ||
[self.backedTextInputView hasMarkedText] ||
[self.backedTextInputView isKindOfClass:[NSSecureTextField class]] ||
fontHasBeenUpdatedBySystem;
// There are multiple Korean input sources (2-Set, 3-Set, etc). Check substring instead instead
[[[_backedTextInputView inputContext] selectedKeyboardInputSource] containsString:@"com.apple.inputmethod.Korean"] ||
[_backedTextInputView hasMarkedText] ||
[_backedTextInputView isKindOfClass:[NSSecureTextField class]] ||
#endif // macOS]
fontHasBeenUpdatedBySystem;

if (shouldFallbackToBareTextComparison) {
return ([newText.string isEqualToString:oldText.string]);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <optional>

#import <React/RCTUIKit.h> // [macOS]

#import <optional>
#import <React/RCTUITextField.h> // [macOS]
#import <React/RCTUITextView.h> // [macOS]

#import <React/RCTBackedTextInputViewProtocol.h>
#import <react/renderer/components/iostextinput/primitives.h>

NS_ASSUME_NONNULL_BEGIN

void RCTCopyBackedTextInput(
RCTUIView<RCTBackedTextInputViewProtocol> *fromTextInput, // [macOS]
RCTUIView<RCTBackedTextInputViewProtocol> *toTextInput); // [macOS]

#if !TARGET_OS_OSX // [macOS]
#if !TARGET_OS_OSX // [macOS]
RCTUIView<RCTBackedTextInputViewProtocol> *fromTextInput,
RCTUIView<RCTBackedTextInputViewProtocol> *toTextInput
#else // [macOS
RCTUITextView<RCTBackedTextInputViewProtocol> *fromTextInput,
RCTUITextView<RCTBackedTextInputViewProtocol> *toTextInput
#endif // macOS]
);

#if !TARGET_OS_OSX // [macOS]
UITextAutocorrectionType RCTUITextAutocorrectionTypeFromOptionalBool(std::optional<bool> autoCorrect);

UITextAutocapitalizationType RCTUITextAutocapitalizationTypeFromAutocapitalizationType(
Expand All @@ -41,6 +48,6 @@ UITextContentType RCTUITextContentTypeFromString(std::string const &contentType)

API_AVAILABLE(ios(12.0))
UITextInputPasswordRules *RCTUITextInputPasswordRulesFromString(std::string const &passwordRules);
#endif // [macOS]
#endif // [macOS]

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
}

void RCTCopyBackedTextInput(
#if !TARGET_OS_OSX // [macOS]
RCTUIView<RCTBackedTextInputViewProtocol> *fromTextInput,
RCTUIView<RCTBackedTextInputViewProtocol> *toTextInput) // [macOS]
RCTUIView<RCTBackedTextInputViewProtocol> *toTextInput
#else // [macOS
RCTUITextView<RCTBackedTextInputViewProtocol> *fromTextInput,
RCTUITextView<RCTBackedTextInputViewProtocol> *toTextInput
#endif // macOS]
)
{
toTextInput.attributedText = RCTSanitizeAttributedString(fromTextInput.attributedText);
toTextInput.placeholder = fromTextInput.placeholder;
Expand Down