|
1 | 1 | ---
|
2 | 2 | title: SearchBar
|
| 3 | +description: UI component for entering search queries. |
| 4 | +contributors: |
| 5 | + - rigor789 |
| 6 | + - Ombuweb |
3 | 7 | ---
|
4 | 8 |
|
5 |
| -<!-- TODO: Add flavors --> |
6 |
| - |
7 |
| -`<SearchBar>` is a UI component that provides a user interface for entering search queries and submitting requests to the search provider. |
8 |
| - |
9 |
| ---- |
| 9 | +`<SearchBar>` is a UI component that provides a user interface for entering search queries. |
10 | 10 |
|
11 | 11 | <DeviceFrame type="ios">
|
12 |
| -<img src="https://raw.githubusercontent.com/nativescript-vue/nativescript-vue-ui-tests/master/screenshots/ios-simulator103iPhone6/SearchBar.png"/> |
| 12 | +<img src="../screenshots/ios/SearchBar.png"/> |
13 | 13 | </DeviceFrame>
|
14 | 14 | <DeviceFrame type="android">
|
15 |
| -<img src="https://raw.githubusercontent.com/nativescript-vue/nativescript-vue-ui-tests/master/screenshots/android23/SearchBar.png" /> |
| 15 | +<img src="../screenshots/android/SearchBar.png"/> |
16 | 16 | </DeviceFrame>
|
17 | 17 |
|
18 |
| -<!-- /// flavor plain --> |
19 |
| - |
20 |
| -### A simple SearchBar handling the clear and submit events |
21 |
| - |
22 |
| -```xml |
23 |
| -<SearchBar id="searchBar" |
24 |
| - hint="Enter search term here ..." |
25 |
| - text="{{ searchText }}" clear="{{ onClear }}" |
26 |
| - submit="{{ onSubmit }}" |
27 |
| - loaded="{{ onSearchBarLoaded }}"/> |
28 |
| -``` |
29 |
| - |
30 |
| -```ts |
31 |
| -import { Observable, Page, SearchBar } from '@nativescript/core' |
32 |
| - |
33 |
| -export function onNavigatingTo(args) { |
34 |
| - const page = args.object as Page |
35 |
| - const vm = new HelloWorldModel() |
36 |
| - page.bindingContext = vm |
37 |
| -} |
38 |
| - |
39 |
| -export class HelloWorldModel extends Observable { |
40 |
| - searchText = '' |
41 |
| - constructor() { |
42 |
| - super() |
43 |
| - } |
44 |
| - |
45 |
| - onSearchBarLoaded(args: EventData) { |
46 |
| - const searchBar = args.object as SearchBar |
47 |
| - searchBar.on('textChange', (args: PropertyChangeData) => { |
48 |
| - console.log('Event name: ', args.eventName) |
49 |
| - }) |
50 |
| - } |
51 |
| - onSubmit(args: EventData) { |
52 |
| - const searchBar = args.object as SearchBar |
53 |
| - console.log(`Searching for ${searchBar.text}`) |
54 |
| - } |
55 |
| - |
56 |
| - onClear(args: EventData) { |
57 |
| - const searchBar = args.object as SearchBar |
58 |
| - console.log(`Clear event raised`) |
59 |
| - } |
60 |
| -} |
61 |
| -``` |
62 |
| - |
63 |
| -<!-- /// |
64 |
| -
|
65 |
| -/// flavor angular |
66 |
| -
|
67 |
| -```xml |
68 |
| -<SearchBar |
69 |
| - hint="Enter search term here ..." |
70 |
| - [text]="searchPhrase" |
71 |
| - (textChange)="onTextChanged($event)" |
72 |
| - (clear)="onClear($event)" |
73 |
| - (submit)="onSubmit($event)" |
74 |
| -> |
75 |
| -</SearchBar> |
76 |
| -``` |
77 |
| -
|
78 |
| -```ts |
79 |
| -import { Component } from '@angular/core' |
80 |
| -import { SearchBar } from '@nativescript/core' |
81 |
| -
|
82 |
| -@Component({ |
83 |
| - moduleId: module.id, |
84 |
| - templateUrl: './usage.component.html' |
85 |
| -}) |
86 |
| -export class UsageComponent { |
87 |
| - searchPhrase: string |
88 |
| -
|
89 |
| - onSubmit(args) { |
90 |
| - const searchBar = args.object as SearchBar |
91 |
| - console.log(`Searching for ${searchBar.text}`) |
92 |
| - } |
93 |
| -
|
94 |
| - onTextChanged(args) { |
95 |
| - const searchBar = args.object as SearchBar |
96 |
| - console.log(`Input changed! New value: ${searchBar.text}`) |
97 |
| - } |
98 |
| -
|
99 |
| - onClear(args) { |
100 |
| - const searchBar = args.object as SearchBar |
101 |
| - console.log(`Clear event raised`) |
102 |
| - } |
103 |
| -} |
104 |
| -``` |
105 |
| -
|
106 |
| -/// |
107 |
| -
|
108 |
| -/// flavor vue |
109 |
| -
|
110 |
| -```xml |
111 |
| -<SearchBar |
112 |
| - hint="Search hint" |
113 |
| - :text="searchPhrase" |
114 |
| - @textChange="onTextChanged" |
115 |
| - @submit="onSubmit" |
116 |
| -/> |
117 |
| -``` |
118 |
| -
|
119 |
| -`<SearchBar>` provides two-way data binding using `v-model`. |
120 |
| -
|
121 |
| -```xml |
122 |
| -<SearchBar v-model="searchQuery" /> |
123 |
| -``` |
124 |
| -
|
125 |
| -/// |
126 |
| -
|
127 |
| -/// flavor svelte |
128 |
| -
|
129 |
| -```tsx |
130 |
| -<searchBar |
131 |
| - hint="Search hint" |
132 |
| - text="{searchQuery}" |
133 |
| - on:textChange="{onTextChanged}" |
134 |
| - on:submit="{onSubmit}" |
135 |
| -/> |
136 |
| -``` |
137 |
| -
|
138 |
| -`<SearchBar>` provides two-way data binding for `text`. |
139 |
| -
|
140 |
| -```xml |
141 |
| -<searchBar bind:text="{searchQuery}" /> |
142 |
| -``` |
143 |
| -
|
144 |
| -/// |
145 |
| -
|
146 |
| -/// flavor react |
147 |
| -
|
148 |
| -```tsx |
149 |
| -<searchBar |
150 |
| - hint="Search hint" |
151 |
| - text="searchPhrase" |
152 |
| - onTextChange={onTextChanged} |
153 |
| - onSubmit={onSubmit} |
154 |
| - onClose={onClose} |
155 |
| -/> |
156 |
| -``` |
157 |
| -
|
158 |
| -/// --> |
| 18 | +<<< @/../examples/src/ui/SearchBar/template.xml#example |
159 | 19 |
|
160 | 20 | ## Props
|
161 | 21 |
|
162 | 22 | ### hint
|
163 | 23 |
|
164 |
| -```xml |
165 |
| -<SearchBar hint="Enter search term here ..." /> |
| 24 | +```ts |
| 25 | +hint: string |
166 | 26 | ```
|
167 | 27 |
|
168 |
| -Gets or sets Placeholder text for the input area. |
| 28 | +<!-- textlint-disable terminology --> |
169 | 29 |
|
170 |
| ---- |
| 30 | +Gets or sets the placeholder text for the input area. |
| 31 | + |
| 32 | +<!-- textlint-enable --> |
171 | 33 |
|
172 | 34 | ### text
|
173 | 35 |
|
174 |
| -```xml |
175 |
| -<SearchBar text="{{ searchText }}" /> |
| 36 | +```ts |
| 37 | +text: string |
176 | 38 | ```
|
177 | 39 |
|
178 | 40 | Gets or sets the value of the search query.
|
179 | 41 |
|
180 |
| ---- |
181 |
| - |
182 | 42 | ### textFieldBackgroundColor
|
183 | 43 |
|
184 |
| -```xml |
185 |
| - <SearchBar textFieldBackgroundColor="#76ABEB"/> |
| 44 | +```ts |
| 45 | +textFieldBackgroundColor: Color |
186 | 46 | ```
|
187 | 47 |
|
188 | 48 | Gets or sets the background color of the input area.
|
189 | 49 |
|
190 |
| ---- |
| 50 | +See [Color](/api/class/Color). |
191 | 51 |
|
192 | 52 | ### textFieldHintColor
|
193 | 53 |
|
194 |
| -```xml |
195 |
| - <SearchBar textFieldHintColor="#fff"/> |
| 54 | +```ts |
| 55 | +textFieldHintColor: Color |
196 | 56 | ```
|
197 | 57 |
|
198 |
| -Gets or sets the color of the Placeholder text. |
| 58 | +<!-- textlint-disable terminology --> |
199 | 59 |
|
200 |
| ---- |
| 60 | +Gets or sets the color of the placeholder text. |
| 61 | + |
| 62 | +<!-- textlint-enable --> |
| 63 | + |
| 64 | +See [Color](/api/class/Color). |
201 | 65 |
|
202 | 66 | ### ...Inherited
|
203 | 67 |
|
204 |
| -For additional inherited properties, refer to the [API Reference](https://docs.nativescript.org/api-reference/classes/searchbar) |
| 68 | +For additional inherited properties, refer to the [API Reference](/api/class/SearchBar) |
205 | 69 |
|
206 | 70 | ## Events
|
207 | 71 |
|
208 | 72 | ### textChange
|
209 | 73 |
|
210 | 74 | ```ts
|
211 |
| -searchBar.on('textChange', (args: PropertyChangeData) => { |
212 |
| - console.log('Event name: ', args.oldValue) |
| 75 | +on('textChange', (args: PropertyChangeData) => { |
| 76 | + const searchBar = args.object as SearchBar |
| 77 | + console.log('Search query:', args.value) |
213 | 78 | })
|
214 | 79 | ```
|
215 | 80 |
|
216 | 81 | Emitted when the search text is changed.
|
217 | 82 |
|
218 |
| ---- |
| 83 | +See [PropertyChangeData](/api/interface/PropertyChangeData). |
219 | 84 |
|
220 | 85 | ### submit
|
221 | 86 |
|
222 |
| -```xml |
223 |
| -<SearchBar submit="{{ onSubmit }}" /> |
224 |
| -``` |
225 |
| - |
226 | 87 | ```ts
|
227 |
| -export class HelloWorldModel extends Observable { |
228 |
| - onSubmit(args: EventData) { |
229 |
| - const searchBar = args.object as SearchBar |
230 |
| - console.log(`Searching for ${searchBar.text}`) |
231 |
| - } |
232 |
| -} |
| 88 | +on('submit', (args: EventData) => { |
| 89 | + const searchBar = args.object as SearchBar |
| 90 | + console.log('Search for:', searchBar.text) |
| 91 | +}) |
233 | 92 | ```
|
234 | 93 |
|
235 | 94 | Emitted when the search text is submitted.
|
236 | 95 |
|
237 |
| ---- |
238 |
| - |
239 | 96 | ### clear
|
240 | 97 |
|
241 |
| -```xml |
242 |
| -<SearchBar clear="{{ onClear }}" /> |
243 |
| -``` |
244 |
| - |
245 | 98 | ```ts
|
246 |
| -export class HelloWorldModel extends Observable { |
247 |
| - onClear(args: EventData) { |
248 |
| - const searchBar = args.object as SearchBar |
249 |
| - console.log(`Clear event raised`) |
250 |
| - } |
251 |
| -} |
| 99 | +on('submit', (args: EventData) => { |
| 100 | + const searchBar = args.object as SearchBar |
| 101 | + console.log('SearchBar cleared') |
| 102 | +}) |
252 | 103 | ```
|
253 | 104 |
|
254 |
| -Emitted when the current search input is cleared through the **X** button in the input area. |
255 |
| - |
256 |
| ---- |
| 105 | +Emitted when the search input is cleared through the **✗** button in the input area. |
257 | 106 |
|
258 | 107 | ## Native Component
|
259 | 108 |
|
|
0 commit comments