Skip to content

Commit 729b068

Browse files
author
Hristo Hristov
authored
Fix android search-bar bug (NativeScript#5046)
* Fix android search-bar bug Searching twice for the same item in the search bar leads to a bug where `submit` event won't be raised. This PR fixes NativeScript#5039 * test: include new test page
1 parent 011be36 commit 729b068

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Observable } from 'tns-core-modules/data/observable'
2+
import { ObservableArray } from 'tns-core-modules/data/observable-array'
3+
import { SearchBar } from 'tns-core-modules/ui/search-bar';
4+
5+
export class Issue5039ViewModel extends Observable {
6+
7+
private _items = ['apple', 'apple cider', 'apple pie', 'orange', 'orange juice', 'strawberry', 'blueberry']
8+
public items = new ObservableArray()
9+
10+
constructor(private _searchBar: SearchBar) {
11+
super()
12+
this.items.push(this._items)
13+
}
14+
15+
onSubmit() {
16+
this.filter(this._searchBar.text);
17+
}
18+
19+
clearSearch() {
20+
this.filter();
21+
}
22+
23+
filter(value: string = '') {
24+
this.items.splice(0, this.items.length) // remove all items
25+
this.items.push(this._items.filter(i => -1 !== i.indexOf(value)))
26+
}
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Issue5039ViewModel } from './issue-5039-view-model'
2+
import { SearchBar } from "tns-core-modules/ui/search-bar";
3+
import { Page } from "tns-core-modules/ui/page";
4+
5+
export function navigatingTo(args) {
6+
const page = <Page>args.object;
7+
const searchBar = <SearchBar>page.getViewById("searchBar")
8+
page.bindingContext = new Issue5039ViewModel(searchBar);
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo">
2+
<StackLayout>
3+
<SearchBar id="searchBar" hint="Search" submit="{{ onSubmit }}" clear="{{ clearSearch }}" />
4+
<ListView items="{{ items }}">
5+
<ListView.itemTemplate>
6+
<Label text="{{ $value }}" />
7+
</ListView.itemTemplate>
8+
</ListView>
9+
</StackLayout>
10+
</Page>

apps/app/ui-tests-app/search-bar/main-page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export function loadExamples() {
1313
const examples = new Map<string, string>();
1414
examples.set("issue-4147", "search-bar/issue-4147");
1515
examples.set("search-bar", "search-bar/search-bar");
16+
examples.set("issue-5039","search-bar/issue-5039");
1617
return examples;
1718
}

tns-core-modules/ui/search-bar/search-bar.android.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function initializeNativeClasses(): void {
4343
}
4444

4545
this[SEARCHTEXT] = newText;
46+
this[QUERY] = undefined;
4647
return true;
4748
}
4849

0 commit comments

Comments
 (0)