-
Notifications
You must be signed in to change notification settings - Fork 26
/
RCTAutoComplete.m
175 lines (147 loc) · 6.63 KB
/
RCTAutoComplete.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#import <Foundation/Foundation.h>
#import "RCTAutoComplete.h"
#import "RCTTableViewCell.h"
#import <React/RCTEventDispatcher.h>
#import "UIView+React.h"
#import "AutoCompleteView.h"
#import "DictionaryAutoCompleteObject.h"
#import <React/RCTFont.h>
@implementation RCTAutoComplete
RCT_EXPORT_MODULE()
// From AutoCompleteView.m
RCT_EXPORT_VIEW_PROPERTY(suggestions, NSArray)
RCT_EXPORT_VIEW_PROPERTY(maximumNumberOfAutoCompleteRows, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(applyBoldEffectToAutoCompleteSuggestions, BOOL)
RCT_EXPORT_VIEW_PROPERTY(reverseAutoCompleteSuggestionsBoldEffect, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showTextFieldDropShadowWhenAutoCompleteTableIsOpen, BOOL);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteTableViewHidden, BOOL);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteTableBorderColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteTableBorderWidth, CGFloat);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteTableBackgroundColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteTableCellBackgroundColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteTableCellTextColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteTableCornerRadius, CGFloat);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteRowHeight, CGFloat);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteFontSize, CGFloat);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteBoldFontName, NSString);
RCT_EXPORT_VIEW_PROPERTY(autoCompleteRegularFontName, NSString);
RCT_CUSTOM_VIEW_PROPERTY(autoCompleteTableTopOffset, NSInteger, AutoCompleteView)
{
CGSize size = view.autoCompleteTableOriginOffset;
size.height = [RCTConvert NSInteger:json];
view.autoCompleteTableOriginOffset = size;
}
RCT_CUSTOM_VIEW_PROPERTY(autoCompleteTableLeftOffset, NSInteger, AutoCompleteView)
{
CGSize size = view.autoCompleteTableOriginOffset;
size.width = [RCTConvert NSInteger:json];
view.autoCompleteTableOriginOffset = size;
}
RCT_CUSTOM_VIEW_PROPERTY(autoCompleteTableSizeOffset, NSInteger, AutoCompleteView)
{
view.autoCompleteTableSizeOffset = CGSizeMake([RCTConvert NSInteger:json], 0);
}
RCT_CUSTOM_VIEW_PROPERTY(cellComponent, NSString*, AutoCompleteView) {
// Register RCTTableViewCellBridge (hosts cell React Component)
[view registerAutoCompleteCellClass:[RCTTableViewCell class]
forCellReuseIdentifier:@"RCTTableViewCell"];
// Set cell React Component
[view setCellComponent:[RCTConvert NSString:json]];
}
RCT_EXPORT_VIEW_PROPERTY(autoCompleteFetchRequestDelay, NSTimeInterval);
// From RCTTextFieldManager.m
RCT_EXPORT_VIEW_PROPERTY(autoCorrect, BOOL)
RCT_EXPORT_VIEW_PROPERTY(text, NSString)
RCT_EXPORT_VIEW_PROPERTY(placeholder, NSString)
RCT_EXPORT_VIEW_PROPERTY(placeholderTextColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(clearButtonMode, UITextFieldViewMode)
RCT_REMAP_VIEW_PROPERTY(clearTextOnFocus, clearsOnBeginEditing, BOOL)
RCT_EXPORT_VIEW_PROPERTY(blurOnSubmit, BOOL)
RCT_EXPORT_VIEW_PROPERTY(keyboardType, UIKeyboardType)
RCT_EXPORT_VIEW_PROPERTY(returnKeyType, UIReturnKeyType)
RCT_EXPORT_VIEW_PROPERTY(enablesReturnKeyAutomatically, BOOL)
RCT_REMAP_VIEW_PROPERTY(color, textColor, UIColor)
RCT_REMAP_VIEW_PROPERTY(autoCapitalize, autocapitalizationType, UITextAutocapitalizationType)
RCT_REMAP_VIEW_PROPERTY(textAlign, textAlignment, NSTextAlignment)
RCT_CUSTOM_VIEW_PROPERTY(fontSize, NSNumber, AutoCompleteView)
{
view.font = [RCTFont updateFont:view.font withSize:json ?: @(defaultView.font.pointSize)];
}
RCT_CUSTOM_VIEW_PROPERTY(fontWeight, NSString, __unused AutoCompleteView)
{
view.font = [RCTFont updateFont:view.font withWeight:json]; // defaults to normal
}
RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSString, __unused AutoCompleteView)
{
view.font = [RCTFont updateFont:view.font withStyle:json]; // defaults to normal
}
RCT_CUSTOM_VIEW_PROPERTY(fontFamily, NSString, AutoCompleteView)
{
view.font = [RCTFont updateFont:view.font withFamily:json ?: defaultView.font.familyName];
}
RCT_EXPORT_VIEW_PROPERTY(mostRecentEventCount, NSInteger)
- (UIView *) view
{
AutoCompleteView *searchTextField = [[AutoCompleteView alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
searchTextField.autoCompleteDataSource = self;
searchTextField.autoCompleteDelegate = self;
searchTextField.showTextFieldDropShadowWhenAutoCompleteTableIsOpen = false;
return searchTextField;
}
- (void)autoCompleteTextField:(AutoCompleteView *)textField
possibleCompletionsForString:(NSString *)string
completionHandler:(void (^)(NSArray *))handler
{
// If empty string, empty the completion list
if([string isEqualToString:@""]) {
handler([NSArray array]);
} else {
textField.handler = handler;
NSDictionary *event = @{
@"possibleCompletionsForString": string,
@"target": textField.reactTag
};
[self.bridge.eventDispatcher sendInputEventWithName:@"topChange" body:event];
}
}
- (BOOL)autoCompleteTextField:(MLPAutoCompleteTextField *)textField
shouldConfigureCell:(UITableViewCell *)cell
withAutoCompleteString:(NSString *)autocompleteString
withAttributedString:(NSAttributedString *)boldedString
forAutoCompleteObject:(id<MLPAutoCompletionObject>)autocompleteObject
forRowAtIndexPath:(NSIndexPath *)indexPath;
{
// If no registered Cell Component
if (((AutoCompleteView*)textField).cellComponent == nil) {
return YES;
}
RCTTableViewCell *customCell = (RCTTableViewCell*) cell;
RCTBridge *_bridge = self.bridge;
// 🚀 https://github.com/aksonov/react-native-tableview/blob/8982116711cd74e819a73237c53307839fe071ce/RNTableView/RNTableView.m#L87
while ([_bridge respondsToSelector:NSSelectorFromString(@"parentBridge")]
&& [_bridge valueForKey:@"parentBridge"]) {
_bridge = [_bridge valueForKey:@"parentBridge"];
}
[customCell initWithBridge:_bridge
reactComponent:[(AutoCompleteView*)textField cellComponent]
json:[(DictionaryAutoCompleteObject*)autocompleteObject json]
];
return YES;
}
- (void)autoCompleteTextField:(MLPAutoCompleteTextField *)textField
didSelectAutoCompleteString:(NSString *)selectedString
withAutoCompleteObject:(id<MLPAutoCompletionObject>)selectedObject
forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *event = selectedObject
? @{
@"target": textField.reactTag,
@"didSelectAutoCompleteString": [(DictionaryAutoCompleteObject*)selectedObject json]
}
: @{
@"target": textField.reactTag,
@"didSelectAutoCompleteString": selectedString
};
[self.bridge.eventDispatcher sendInputEventWithName:@"topChange" body:event];
}
@end