forked from umhan35/react-native-search-bar
-
Notifications
You must be signed in to change notification settings - Fork 4
/
RNSearchBarManager.m
98 lines (77 loc) · 2.73 KB
/
RNSearchBarManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#import "RNSearchBarManager.h"
#import "RNSearchBar.h"
#import "RCTBridge.h"
#import "RCTUIManager.h"
#import "RCTSparseArray.h"
@implementation RNSearchBarManager
RCT_EXPORT_MODULE()
@synthesize bridge = _bridge;
- (dispatch_queue_t)methodQueue
{
return _bridge.uiManager.methodQueue;
}
- (UIView *)view
{
RNSearchBar *searchBar = [[RNSearchBar alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
return searchBar;
}
RCT_EXPORT_VIEW_PROPERTY(placeholder, NSString)
RCT_EXPORT_VIEW_PROPERTY(text, NSString)
RCT_EXPORT_VIEW_PROPERTY(showsCancelButton, BOOL)
RCT_EXPORT_VIEW_PROPERTY(barTintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)
RCT_CUSTOM_VIEW_PROPERTY(hideBackground, BOOL, RNSearchBar)
{
if ([RCTConvert BOOL:json]) {
view.backgroundImage = [[UIImage alloc] init];
view.backgroundColor = [UIColor clearColor];
}
}
RCT_CUSTOM_VIEW_PROPERTY(textFieldBackgroundColor, UIColor, RNSearchBar)
{
if ([RCTConvert UIColor:json]) {
// logic borrowed from http://stackoverflow.com/a/22266150/395989
CGSize size = CGSizeMake(34, 34);
// create context with transparent background
UIGraphicsBeginImageContextWithOptions(size, NO, 1);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0, 34, 34)
cornerRadius:5.0] addClip];
[[RCTConvert UIColor:json] setFill];
UIRectFill(CGRectMake(0, 0, size.width, size.height));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[view setSearchFieldBackgroundImage:image forState:UIControlStateNormal];
}
}
- (NSDictionary *)constantsToExport
{
return @{
@"ComponentHeight": @([self view].intrinsicContentSize.height),
};
}
RCT_EXPORT_METHOD(blur:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:
^(__unused RCTUIManager *uiManager, RCTSparseArray *viewRegistry){
RNSearchBar *searchBar = viewRegistry[reactTag];
if ([searchBar isKindOfClass:[RNSearchBar class]]) {
[searchBar endEditing:true];
} else {
RCTLogError(@"Cannot blur: %@ (tag #%@) is not RNSearchBar", searchBar, reactTag);
}
}];
}
RCT_EXPORT_METHOD(focus:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:
^(__unused RCTUIManager *uiManager, RCTSparseArray *viewRegistry){
RNSearchBar *searchBar = viewRegistry[reactTag];
if ([searchBar isKindOfClass:[RNSearchBar class]]) {
[searchBar becomeFirstResponder];
} else {
RCTLogError(@"Cannot focus: %@ (tag #%@) is not RNSearchBar", searchBar, reactTag);
}
}];
}
@end