Skip to content

Commit 3e8d2aa

Browse files
author
Vladimir Enchev
committed
Merge pull request NativeScript#189 from NativeScript/search-bar-hint
SearchBar hint property implemented
2 parents fed62dd + abcf4f3 commit 3e8d2aa

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

apps/TelerikNEXT/main-page.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
</Label.formattedText>
4444
</Label>
4545

46-
<SearchBar text="{{ search }}" style="background-color: #fac950; color: #fac950;" textFieldBackgroundColor="white" row="2" />
46+
<SearchBar text="{{ search }}" hint="Search" style="background-color: #fac950; color: #fac950;" textFieldBackgroundColor="white" row="2" />
4747

4848
<ListView items="{{ sessions }}" row="3" separatorColor="#fac950">
4949
<ListView.itemTemplate>

ui/search-bar/search-bar-common.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export module knownEvents {
1111

1212
export class SearchBar extends view.View implements definition.SearchBar {
1313
public static textFieldBackgroundColorProperty = new dependencyObservable.Property("textFieldBackgroundColor", "SearchBar", new proxy.PropertyMetadata(undefined))
14+
public static hintProperty = new dependencyObservable.Property("hint", "SearchBar", new proxy.PropertyMetadata(""))
1415

1516
public static textProperty = new dependencyObservable.Property(
1617
"text",
@@ -25,6 +26,13 @@ export class SearchBar extends view.View implements definition.SearchBar {
2526
this._setValue(SearchBar.textProperty, value);
2627
}
2728

29+
get hint(): string {
30+
return this._getValue(SearchBar.hintProperty);
31+
}
32+
set hint(value: string) {
33+
this._setValue(SearchBar.hintProperty, value);
34+
}
35+
2836
get textFieldBackgroundColor(): color.Color {
2937
return this._getValue(SearchBar.textFieldBackgroundColorProperty);
3038
}

ui/search-bar/search-bar.android.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import dependencyObservable = require("ui/core/dependency-observable");
33
import proxy = require("ui/core/proxy");
44
import color = require("color");
5+
import types = require("utils/types");
56

67
var SEARCHTEXT = "searchText";
78
var QUERY = "query";
@@ -33,10 +34,38 @@ function onTextFieldBackgroundColorPropertyChanged(data: dependencyObservable.Pr
3334
// register the setNativeValue callbacks
3435
(<proxy.PropertyMetadata>common.SearchBar.textFieldBackgroundColorProperty.metadata).onSetNativeValue = onTextFieldBackgroundColorPropertyChanged;
3536

37+
function onHintPropertyChanged(data: dependencyObservable.PropertyChangeData) {
38+
var bar = <SearchBar>data.object;
39+
if (!bar.android) {
40+
return;
41+
}
42+
43+
var newValue = data.newValue;
44+
45+
if (types.isString(newValue)) {
46+
bar.android.setQueryHint(newValue);
47+
}
48+
}
49+
50+
(<proxy.PropertyMetadata>common.SearchBar.hintProperty.metadata).onSetNativeValue = onHintPropertyChanged;
51+
52+
function getTextView(bar: android.widget.SearchView): android.widget.TextView {
53+
if (bar) {
54+
var id = bar.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
55+
if (id) {
56+
return <android.widget.TextView> bar.findViewById(id);
57+
}
58+
}
59+
60+
return undefined;
61+
}
62+
3663
function _changeSearchViewBackgroundColor(bar: android.widget.SearchView, color: number) {
37-
var id = bar.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
38-
var textView = <android.widget.TextView> bar.findViewById(id);
39-
textView.setBackgroundColor(color);
64+
var textView = getTextView(bar);
65+
66+
if (textView) {
67+
textView.setBackgroundColor(color);
68+
}
4069
}
4170

4271
// merge the exports of the common file with the exports of this file

ui/search-bar/search-bar.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ declare module "ui/search-bar" {
5151
*/
5252
text: string;
5353

54+
/**
55+
* Gets or sets the text of the search bar text field hint/placeholder.
56+
*/
57+
hint: string;
58+
5459
/**
5560
* Gets or sets the TextField background color of the SearchBar component.
5661
*/

ui/search-bar/search-bar.ios.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,50 @@
22
import dependencyObservable = require("ui/core/dependency-observable");
33
import proxy = require("ui/core/proxy");
44
import color = require("color");
5+
import types = require("utils/types");
56

67
function onTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
78
var bar = <SearchBar>data.object;
89
bar.ios.text = data.newValue;
910
}
1011

11-
// register the setNativeValue callbacks
1212
(<proxy.PropertyMetadata>common.SearchBar.textProperty.metadata).onSetNativeValue = onTextPropertyChanged;
1313

1414
function onTextFieldBackgroundColorPropertyChanged(data: dependencyObservable.PropertyChangeData) {
1515
var bar = <SearchBar>data.object;
1616
if (data.newValue instanceof color.Color) {
17-
(<UITextField>bar.ios.valueForKey("_searchField")).backgroundColor = data.newValue.ios;
17+
var tf = getUITextField(bar.ios);
18+
if (tf) {
19+
tf.backgroundColor = data.newValue.ios;
20+
}
1821
}
1922
}
2023

21-
// register the setNativeValue callbacks
2224
(<proxy.PropertyMetadata>common.SearchBar.textFieldBackgroundColorProperty.metadata).onSetNativeValue = onTextFieldBackgroundColorPropertyChanged;
2325

26+
function onHintPropertyChanged(data: dependencyObservable.PropertyChangeData) {
27+
var bar = <SearchBar>data.object;
28+
if (!bar.ios) {
29+
return;
30+
}
31+
32+
var newValue = data.newValue;
33+
34+
if (types.isString(newValue)) {
35+
bar.ios.placeholder = newValue;
36+
}
37+
}
38+
39+
(<proxy.PropertyMetadata>common.SearchBar.hintProperty.metadata).onSetNativeValue = onHintPropertyChanged;
40+
41+
function getUITextField(bar: UISearchBar): UITextField {
42+
if (bar) {
43+
return <UITextField> bar.valueForKey("_searchField");
44+
}
45+
46+
return undefined;
47+
}
48+
2449
// merge the exports of the common file with the exports of this file
2550
declare var exports;
2651
require("utils/module-merge").merge(common, exports);
@@ -69,7 +94,7 @@ export class SearchBar extends common.SearchBar {
6994
constructor() {
7095
super();
7196
this._ios = new UISearchBar();
72-
97+
7398
this._delegate = UISearchBarDelegateImpl.new().initWithOwner(this);
7499
this._ios.delegate = this._delegate;
75100
}

0 commit comments

Comments
 (0)