Skip to content

Instantly share code, notes, and snippets.

@chirag04
Last active August 17, 2017 14:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@robertjpayne
Copy link

@interface UITextField (ToolTipMenu)
@end

@implementation UITextField (ToolTipMenu)

// make sure you prefix these to avoid collision
- (void)ttm_changeColor:(id)sender {

}


@end

@iotashan
Copy link

I'm doing something similar with the new RN, but am having a strange issue.

I've changed my import to:
#import <React/RCTTextField.h>

But I'm still getting the error:
'React/RCTTextField.h' file not found

I must be doing something dumb. A quick tip would help :)

@iotashan
Copy link

Solved my issue. Need to add $(SRCROOT)/../react-native/React to the header search paths

@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