Skip to content

Commit cf14133

Browse files
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: facebook#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]; } facebook#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; }
1 parent 51e0136 commit cf14133

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Libraries/Text/RCTTextField.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
#import "RCTUtils.h"
1515
#import "UIView+React.h"
1616

17+
//
18+
// Repository of accessory Handles
19+
//
20+
21+
static NSMutableDictionary *textFieldAccessoryMapping;
22+
23+
// input View Accessory Handle definition
24+
25+
typedef UIView * (^KeyboardAccessory)(UITextField *);
26+
1727
@implementation RCTTextField
1828
{
1929
RCTEventDispatcher *_eventDispatcher;
@@ -257,6 +267,13 @@ - (BOOL)canBecomeFirstResponder
257267

258268
- (void)reactWillMakeFirstResponder
259269
{
270+
271+
// This fragment was added to let application inject input accessory view if needed
272+
//
273+
KeyboardAccessory handler = [RCTTextField getAcessoryMapping][[NSNumber numberWithInt:self.keyboardType]];
274+
if (handler) {
275+
self.inputAccessoryView = handler(self);
276+
}
260277
_jsRequestingFirstResponder = YES;
261278
}
262279

@@ -279,4 +296,28 @@ - (BOOL)resignFirstResponder
279296
return result;
280297
}
281298

299+
300+
+(NSMutableDictionary*)getAcessoryMapping {
301+
static dispatch_once_t onceToken = 0;
302+
303+
dispatch_once(&onceToken, ^{
304+
textFieldAccessoryMapping = [[NSMutableDictionary alloc] init];
305+
// Do any other initialisation stuff here
306+
});
307+
308+
return textFieldAccessoryMapping;
309+
}
310+
311+
/**
312+
* The method to allow application register for injecting inputAccessoryView for particular keyboard type
313+
* handler presents instancdispatch_oncee of KeyboardAccessory type
314+
*/
315+
316+
+(void) registerForKeyboardType:(NSNumber *)type handler:(KeyboardAccessory)handler{
317+
318+
NSMutableDictionary *dct = [RCTTextField getAcessoryMapping];
319+
dct[type] = handler;
320+
321+
}
322+
282323
@end

0 commit comments

Comments
 (0)