forked from umhan35/react-native-search-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNSearchBarManager.m
155 lines (129 loc) · 4.79 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#import "RNSearchBarManager.h"
#import "RNSearchBar.h"
#import <React/RCTBridge.h>
#import <React/RCTUIManager.h>
@implementation RCTConvert (UIBarStyle)
RCT_ENUM_CONVERTER(UIBarStyle, (@{
@"default": @(UIBarStyleDefault),
@"black": @(UIBarStyleBlack)
}),
UIBarStyleDefault, integerValue)
@end
@implementation RCTConvert (UISearchBarStyle)
RCT_ENUM_CONVERTER(UISearchBarStyle, (@{
@"default": @(UISearchBarStyleDefault),
@"prominent": @(UISearchBarStyleProminent),
@"minimal": @(UISearchBarStyleMinimal)
}),
UISearchBarStyleDefault, integerValue)
@end
@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_CUSTOM_VIEW_PROPERTY(showsCancelButton, BOOL, RNSearchBar)
{
BOOL value = [RCTConvert BOOL:json];
view._jsShowsCancelButton = value;
view.showsCancelButton = value;
}
RCT_EXPORT_VIEW_PROPERTY(barTintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(enablesReturnKeyAutomatically, BOOL)
RCT_EXPORT_VIEW_PROPERTY(barStyle, UIBarStyle)
RCT_EXPORT_VIEW_PROPERTY(searchBarStyle, UISearchBarStyle)
RCT_CUSTOM_VIEW_PROPERTY(hideBackground, BOOL, RNSearchBar)
{
if ([RCTConvert BOOL:json]) {
view.backgroundImage = [[UIImage alloc] init];
view.backgroundColor = [UIColor clearColor];
}
}
RCT_CUSTOM_VIEW_PROPERTY(editable, BOOL, RNSearchBar)
{
if ([RCTConvert BOOL:json]) {
[view setUserInteractionEnabled: YES];
view.alpha = 1;
} else {
[view setUserInteractionEnabled: NO];
view.alpha = .75;
}
}
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];
[view setSearchTextPositionAdjustment:UIOffsetMake(8.0, 0.0)];
}
}
//based on http://stackoverflow.com/questions/19048766/
RCT_CUSTOM_VIEW_PROPERTY(textColor, UIColor, RNSearchBar)
{
if([RCTConvert UIColor:json]) {
[[UITextField appearanceWhenContainedIn:[RNSearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[RCTConvert UIColor:json]}];
}
}
- (NSDictionary *)constantsToExport
{
return @{
@"ComponentHeight": @([self view].intrinsicContentSize.height),
};
}
RCT_EXPORT_METHOD(blur:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:
^(__unused RCTUIManager *uiManager, NSDictionary *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, NSDictionary *viewRegistry){
RNSearchBar *searchBar = viewRegistry[reactTag];
if ([searchBar isKindOfClass:[RNSearchBar class]]) {
[searchBar becomeFirstResponder];
} else {
RCTLogError(@"Cannot focus: %@ (tag #%@) is not RNSearchBar", searchBar, reactTag);
}
}];
}
RCT_EXPORT_METHOD(unFocus:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:
^(__unused RCTUIManager *uiManager, NSDictionary *viewRegistry){
RNSearchBar *searchBar = viewRegistry[reactTag];
if ([searchBar isKindOfClass:[RNSearchBar class]]) {
[searchBar resignFirstResponder];
} else {
RCTLogError(@"Cannot unFocus: %@ (tag #%@) is not RNSearchBar", searchBar, reactTag);
}
}];
}
@end