forked from umhan35/react-native-search-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNSearchBar.m
86 lines (65 loc) · 2.34 KB
/
RNSearchBar.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
#import "RNSearchBar.h"
#import <React/UIView+React.h>
#import <React/RCTEventDispatcher.h>
@interface RNSearchBar() <UISearchBarDelegate>
@end
@implementation RNSearchBar
{
RCTEventDispatcher *_eventDispatcher;
NSInteger _nativeEventCount;
}
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
if ((self = [super initWithFrame:CGRectMake(0, 0, 1000, 44)])) {
_eventDispatcher = eventDispatcher;
self.delegate = self;
}
return self;
}
- (void) searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeBlur
reactTag:self.reactTag
text:searchBar.text
key:nil
eventCount:_nativeEventCount];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[self setShowsCancelButton:self._jsShowsCancelButton animated:YES];
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeFocus
reactTag:self.reactTag
text:searchBar.text
key:nil
eventCount:_nativeEventCount];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
_nativeEventCount++;
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeChange
reactTag:self.reactTag
text:searchText
key:nil
eventCount:_nativeEventCount];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSDictionary *event = @{
@"target": self.reactTag,
@"button": @"search",
@"searchText": searchBar.text
};
[_eventDispatcher sendInputEventWithName:@"press" body:event];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
self.text = @"";
[self resignFirstResponder];
[self setShowsCancelButton:NO animated:YES];
NSDictionary *event = @{
@"target": self.reactTag,
@"button": @"cancel"
};
[_eventDispatcher sendInputEventWithName:@"press" body:event];
}
@end