From 75a445d9d3011acab6f7dfd668b898b991f7d540 Mon Sep 17 00:00:00 2001 From: Eugene Krivopaltsev Date: Fri, 25 Mar 2016 14:48:57 -0700 Subject: [PATCH] Enable input Accessory View for RCTTextField Some keyboard types does not have buttons correspondent for keyReturnType such as Next, Done. It is observable on IPhone for numeric keyboard types. That prevents traversing forms with "number" input fields and impacts end-user experience. The improvement allows application for adding extra buttons via inputAccessoryView. Because RCTText is not a project exposed to React application calling all methods to add a handler is done using selectors. Here is a code fragment which has an example on how to achieve it: Within application: #1 Create a keyboard Handle KeyboardAccessory keyboardHandler = ^UIView *(UITextField *textField) { ReactTextAdapter *kAdapter = [[ReactTextAdapter alloc] init:textField]; return [kAdapter getInputAccessory]; }; 2. Register an adapter Class clz = NSClassFromString(@"RCTTextField"); SEL selector = NSSelectorFromString(@"registerForKeyboardType:handler:"); if ([clz respondsToSelector:selector]) { [ clz performSelector:selector withObject:[NSNumber numberWithInt:UIKeyboardTypeNumberPad] withObject:keyboardHandler]; } #3 Example of Keyboard Adapter @interface ReactTextAdapter :UIToolbar -(instancetype) init:(UITextField *)textField; -(UIView *)getInputAccessory; @end // // ReactTextAdapter.m // KeyboardTest // // Created by Krivopaltsev, Eugene on 3/11/16. // #import "ReactTextAdapter.h" @interface ReactTextAdapter() @property (nonatomic) UITextField *textField; //@property (nonatomic)UIToolbar *toolBar; @property (nonatomic)UIBarButtonItem *flexible; @end @implementation ReactTextAdapter -(instancetype) init:(UITextField *)textField { self = [super init]; if (self) { self.textField = textField; [self setBarStyle:UIBarStyleDefault]; self.translatesAutoresizingMaskIntoConstraints = NO; self.flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; // since there is a next field, add a NEXT button to the picker if (self. textField. returnKeyType == UIReturnKeyNext) { NSString *nextText = @"Next"; UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:nextText style:UIBarButtonItemStyleDone target:self action:@selector(next)]; self.items = [[NSArray alloc] initWithObjects: self.flexible, nextButton, nil]; }else if (self.textField. returnKeyType == UIReturnKeyContinue) { NSString *continueText = @"Continue"; UIBarButtonItem *contButton = [[UIBarButtonItem alloc] initWithTitle:continueText style:UIBarButtonItemStyleDone target:self action:@selector(next)]; self.items = [[NSArray alloc] initWithObjects: self.flexible, contButton, nil]; } else if (self. textField.returnKeyType == UIReturnKeyDone) { NSString *doneText = @"Done"; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:doneText style:UIBarButtonItemStyleDone target:self action:@selector(done)]; doneButton.enabled = YES; self.items = [[NSArray alloc] initWithObjects: self.flexible, doneButton, nil]; } } return self; } -(void) done { [self passToReactNative]; [self.textField resignFirstResponder]; } -(void) next { [self passToReactNative]; } -(void) passToReactNative { SEL selector = @selector(textFieldSubmitEditing); if ([self.textField respondsToSelector:selector]) { [self.textField performSelector:selector withObject:nil]; } } -(UIView *)getInputAccessory { return self; } --- Libraries/Text/RCTTextField.m | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Libraries/Text/RCTTextField.m b/Libraries/Text/RCTTextField.m index 40da29de460a63..804a7479a8c941 100644 --- a/Libraries/Text/RCTTextField.m +++ b/Libraries/Text/RCTTextField.m @@ -302,21 +302,20 @@ +(NSMutableDictionary*)getAcessoryMapping { dispatch_once(&onceToken, ^{ textFieldAccessoryMapping = [[NSMutableDictionary alloc] init]; - // Do any other initialisation stuff here }); return textFieldAccessoryMapping; } /** - * The method to allow application register for injecting inputAccessoryView for particular keyboard type + * The method to allow application register handle to produce inputAccessoryView for particular keyboard type * handler presents instancdispatch_oncee of KeyboardAccessory type */ +(void) registerForKeyboardType:(NSNumber *)type handler:(KeyboardAccessory)handler{ - NSMutableDictionary *dct = [RCTTextField getAcessoryMapping]; - dct[type] = handler; + [RCTTextField getAcessoryMapping][type] = handler; + }