Skip to content

Commit

Permalink
feat(ios): add search bar token API (#11489)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansemannn authored Mar 16, 2020
1 parent 9e546fc commit 0680dcd
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 5 deletions.
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: String
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
57 changes: 53 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,58 @@ - (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]];

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

- (void)removeTokenAtIndex:(id)index
{
ENSURE_SINGLE_ARG(index, NSNumber);
if (![TiUtils isIOSVersionOrGreater:@"13.0"]) {
return;
}
[[[self searchBar] searchTextField] removeTokenAtIndex:[TiUtils intValue:index]];
}

- (NSArray<NSDictionary<NSString *, NSString *> *> *)tokens
{
if (![TiUtils isIOSVersionOrGreater:@"13.0"]) {
return;
}
NSArray<UISearchToken *> *tokens = [[[self searchBar] searchTextField] tokens];
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

0 comments on commit 0680dcd

Please sign in to comment.