Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): add search bar token API #11489

Merged
merged 11 commits into from
Mar 16, 2020
49 changes: 49 additions & 0 deletions apidoc/Titanium/UI/SearchBar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ methods:
type: AnimatedOptions
optional: true
default: "{ animated: false }"

- name: insertTokenAtIndex
summary: Inserts a new search token at the specified index.
parameters:
- name: token
type: SearchBarToken
summary: The token to insert
- name: index
type: Number
summary: The index to insert the token at.
osver: { ios: { min: "13.0" } }
since: 9.1.0

- name: removeTokenAtIndex
summary: Removes an existing token at the specified index.
parameters:
- name: index
type: Number
summary: The index to remove the token at.
osver: { ios: { min: "13.0" } }
since: 9.1.0

events:
- name: blur
Expand Down Expand Up @@ -284,6 +305,13 @@ properties:
On Android, the value cannot be set until after the search bar is created.
type: String

- name: tokens
summary: The token of a search text field
since: 9.1.0
osver: { ios: { min: "13.0" } }
type: Array<String>
default: []

examples:

- title: Simple Search Bar
Expand Down Expand Up @@ -323,3 +351,24 @@ examples:
</TableView>
</Alloy>

---
name: SearchBarToken
summary: The search bar token for the <Titanium.UI.SearchBar.insertTokenAtIndex> method.
since: 9.1.0
osver: { ios: { min: "13.0" } }
platforms: [iphone, ipad]
properties:
- name: identifier
summary: The identifier of the search bar token.
type: Titanium.UI.ListSection
hansemannn marked this conversation as resolved.
Show resolved Hide resolved
optional: false

- name: text
summary: The text of the search bar token (displayed in the search bar).
type: String
optional: false

- name: image
summary: The image of the search bar token.
type: String
optional: true
4 changes: 3 additions & 1 deletion iphone/Classes/TiUISearchBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ - (void)setBackgroundImage_:(id)arg
self.backgroundImage = arg;
}

#pragma mark Delegate
#pragma mark UISearchBarDelegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
if (delegate != nil && [delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) {
Expand Down Expand Up @@ -343,6 +344,7 @@ - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)rang
[self processKeyPressed:text];
return YES;
}

@end

#endif
1 change: 1 addition & 0 deletions iphone/Classes/TiUISearchBarProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
@property (nonatomic, readwrite, assign) BOOL showsCancelButton;

#pragma mark - Titanium Internal Use

- (void)ensureSearchBarHierarchy;
- (void)setSearchBar:(UISearchBar *)searchBar;

Expand Down
51 changes: 47 additions & 4 deletions iphone/Classes/TiUISearchBarProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ - (id)color

- (NSNumber *)keyboardType
{
return NUMINT([[self searchBar] keyboardType]);
return @([[self searchBar] keyboardType]);
}

- (NSNumber *)keyboardAppearance
{
return NUMINT([[self searchBar] keyboardAppearance]);
return @([[self searchBar] keyboardAppearance]);
}

- (NSString *)prompt
Expand All @@ -169,7 +169,7 @@ - (NSNumber *)autocorrect

- (NSNumber *)autocapitalization
{
return NUMINT([[self searchBar] autocapitalizationType]);
return @([[self searchBar] autocapitalizationType]);
}

- (id)tintColor
Expand All @@ -184,9 +184,52 @@ - (id)barColor

- (NSNumber *)style
{
return NUMINT([[self searchBar] searchBarStyle]);
return @([[self searchBar] searchBarStyle]);
}

#if IS_SDK_IOS_13
- (void)insertTokenAtIndex:(id)params
{
ENSURE_ARG_COUNT(params, 2);

if (![TiUtils isIOSVersionOrGreater:@"13.0"]) {
return;
}

NSDictionary<NSString *, NSString *> *token = params[0];
int index = [TiUtils intValue:params[1]];

UISearchToken *searchToken = [UISearchToken tokenWithIcon:[TiUtils toImage:token[@"image"] proxy:self]
text:[TiUtils stringValue:@"text" properties:token]];

if (token[@"identifier"] == nil) {
NSLog(@"[WARN] Missing search token identifier! Using a generated UUID …");
}

searchToken.representedObject = [TiUtils stringValue:@"identifier" properties:token def:[TiUtils createUUID]];
hansemannn marked this conversation as resolved.
Show resolved Hide resolved

[[[self searchBar] searchTextField] insertToken:searchToken atIndex:index];
}

- (void)removeTokenAtIndex:(id)index
{
ENSURE_SINGLE_ARG(index, NSNumber);
[[[self searchBar] searchTextField] removeTokenAtIndex:[TiUtils intValue:index]];
hansemannn marked this conversation as resolved.
Show resolved Hide resolved
}

- (NSArray<NSDictionary<NSString *, NSString *> *> *)tokens
{
NSArray<UISearchToken *> *tokens = [[[self searchBar] searchTextField] tokens];
hansemannn marked this conversation as resolved.
Show resolved Hide resolved
NSMutableArray<id> *result = [NSMutableArray arrayWithCapacity:tokens.count];

[tokens enumerateObjectsUsingBlock:^(UISearchToken *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
[result addObject:obj.representedObject];
}];

return result;
}
#endif

USE_VIEW_FOR_CONTENT_HEIGHT
@end

Expand Down