Skip to content
Closed
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 @@ -658,17 +658,60 @@ - (void)setCustomInputAccessoryViewWithNativeID:(NSString *)nativeID
}];
}

- (NSString *)returnKeyTypeToString:(UIReturnKeyType)returnKeyType {
switch (returnKeyType) {
case UIReturnKeyDefault:
return @"Default";
case UIReturnKeyGo:
return @"Go";
case UIReturnKeyNext:
return @"Next";
case UIReturnKeySearch:
return @"Search";
case UIReturnKeySend:
return @"Send";
case UIReturnKeyYahoo:
return @"Yahoo";
case UIReturnKeyGoogle:
return @"Google";
case UIReturnKeyRoute:
return @"Route";
case UIReturnKeyJoin:
return @"Join";
case UIReturnKeyEmergencyCall:
return @"Emergency Call";
default:
return @"Done";
}
}

- (void)setDefaultInputAccessoryView
{
UIView<RCTBackedTextInputViewProtocol> *textInputView = self.backedTextInputView;
UIKeyboardType keyboardType = textInputView.keyboardType;

// These keyboard types (all are number pads) don't have a "Done" button by default,
// These keyboard types (all are number pads) don't have a Return Key button by default,
// so we create an `inputAccessoryView` with this button for them.

NSArray<NSNumber *> *returnKeyTypes = @[
@(UIReturnKeyDone),
@(UIReturnKeyGo),
@(UIReturnKeyDefault),
@(UIReturnKeyNext),
@(UIReturnKeySearch),
@(UIReturnKeySend),
@(UIReturnKeyYahoo),
@(UIReturnKeyGoogle),
@(UIReturnKeyRoute),
@(UIReturnKeyJoin),
@(UIReturnKeyRoute),
@(UIReturnKeyEmergencyCall)
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of suggestions here:

  1. Can we create a NSSet<NSNumber *> instead of an NSArray? This will be more efficient when looking up as we won't have to iterate over the array
  2. Can we move this to a static variable out of the implementation?
    (before the @implementation keyword on top)?

The reason for 2 is that this Collection will be created and destroyed every time the method setDefaultInputAccessoryView is invoked, so it is not very efficient.

BOOL containsKeyType = [returnKeyTypes containsObject:@(textInputView.returnKeyType)];
BOOL shouldHaveInputAccessoryView =
(keyboardType == UIKeyboardTypeNumberPad || keyboardType == UIKeyboardTypePhonePad ||
keyboardType == UIKeyboardTypeDecimalPad || keyboardType == UIKeyboardTypeASCIICapableNumberPad) &&
textInputView.returnKeyType == UIReturnKeyDone;
containsKeyType;

if (_hasInputAccessoryView == shouldHaveInputAccessoryView) {
return;
Expand All @@ -677,14 +720,17 @@ - (void)setDefaultInputAccessoryView
_hasInputAccessoryView = shouldHaveInputAccessoryView;

if (shouldHaveInputAccessoryView) {
NSString *buttonLabel = [self returnKeyTypeToString:textInputView.returnKeyType];

UIToolbar *toolbarView = [UIToolbar new];
[toolbarView sizeToFit];
UIBarButtonItem *flexibleSpace =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(handleInputAccessoryDoneButton)];
UIBarButtonItem *doneButton =
[[UIBarButtonItem alloc] initWithTitle:buttonLabel
style:UIBarButtonItemStylePlain
target:self
action:@selector(handleInputAccessoryDoneButton)];
toolbarView.items = @[ flexibleSpace, doneButton ];
textInputView.inputAccessoryView = toolbarView;
} else {
Expand Down