Skip to content

Instantly share code, notes, and snippets.

@chirag04
Last active August 17, 2017 14:41
Show Gist options
  • Save chirag04/9a0c9d358c89d4504084 to your computer and use it in GitHub Desktop.
Save chirag04/9a0c9d358c89d4504084 to your computer and use it in GitHub Desktop.
//In RCTTextField.h
@interface RCTTextField : UITextField <UITextFieldDelegate>
//In RCTTextField.m
In Init:
_eventDispatcher = eventDispatcher;
//Adding the delegate.
self.delegate = self;
[self addTarget:self action:@selector(_textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
var ToolTipMenu = require('NativeModules').ToolTipMenu;
handleFocus: () => {
ToolTipMenu.show(this.refs.input.getNodeHandle(), ['x', 'y', 'z']);
}
handleChange: (text) => {
this.setState({text: text})
}
#import "RCTTextField.h"
@interface RCTTextField (ToolTipMenu)
- (void)tappedMenuItem:(NSString *)text;
@end
#import "UIView+ToolTipMenu.h"
@implementation RCTTextField (ToolTipMenu)
- (void)tappedMenuItem:(NSString *)text {
self.text = text;
[self sendActionsForControlEvents:UIControlEventEditingChanged];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
NSString *sel = NSStringFromSelector(action);
NSRange match = [sel rangeOfString:@"magic_"];
if (match.location == 0) {
return YES;
}
return NO;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
if ([super methodSignatureForSelector:sel]) {
return [super methodSignatureForSelector:sel];
}
return [super methodSignatureForSelector:@selector(tappedMenuItem:)];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
NSString *sel = NSStringFromSelector([invocation selector]);
NSRange match = [sel rangeOfString:@"magic_"];
if (match.location == 0) {
[self tappedMenuItem:[sel substringFromIndex:6]];
} else {
[super forwardInvocation:invocation];
}
}
@end
#import <UIKit/UIKit.h>
#import "RCTBridgeModule.h"
@interface ToolTipMenu : NSObject <RCTBridgeModule>
@end
#import "ToolTipMenu.h"
#import "UIView+ToolTipMenu.h"
#import "RCTSparseArray.h"
#import "RCTUIManager.h"
@implementation ToolTipMenu
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(show:(NSNumber *)reactTag
items: (NSArray *)items)
{
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) {
UIView *view = viewRegistry[reactTag];
if (!view) {
RCTLogError(@"Cannot find view with tag #%@", reactTag);
return;
}
NSArray *buttons = items;
NSMutableArray *menuItems = [NSMutableArray array];
for (NSString *buttonText in buttons) {
NSString *sel = [NSString stringWithFormat:@"magic_%@", buttonText];
[menuItems addObject:[[UIMenuItem alloc]
initWithTitle:buttonText
action:NSSelectorFromString(sel)]];
}
UIMenuController *menuCont = [UIMenuController sharedMenuController];
[menuCont setTargetRect:view.frame inView:view.superview];
menuCont.arrowDirection = UIMenuControllerArrowDown;
menuCont.menuItems = menuItems;
[menuCont setMenuVisible:YES animated:YES];
}];
}
@end
@enahum
Copy link

enahum commented Aug 17, 2017

@iotashan adding $(SRCROOT)/../react-native/React to the header search paths still displays 'React/RCTTextField.h' file not found for me, any pointers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment