|
1 | 1 | ---
|
2 | 2 | title: SegmentedBar
|
| 3 | +description: UI component for discrete slection. |
3 | 4 | ---
|
4 | 5 |
|
5 |
| -<!-- TODO: Add flavors --> |
6 |
| - |
7 |
| -`<SegmentedBar>` is a UI bar component that displays a set of buttons for discrete selection. Can show text or images. |
8 |
| - |
9 |
| -As opposed to [`<TabView>`](#tabview): |
10 |
| - |
11 |
| -- The position of `<SegmentedBar>` is not fixed. |
12 |
| -- You can place and style it as needed on the page or inside additional app elements such as hamburger menus. |
13 |
| -- You need to handle the content shown after selection separately. |
14 |
| - |
15 |
| ---- |
| 6 | +`<SegmentedBar>` is a UI bar component that displays a set of buttons for discrete selection. |
16 | 7 |
|
17 | 8 | <DeviceFrame type="ios">
|
18 |
| -<img src="https://raw.githubusercontent.com/nativescript-vue/nativescript-vue-ui-tests/master/screenshots/ios-simulator103iPhone6/SegmentedBar.png"/> |
| 9 | +<img src="../screenshots/ios/SegmentedBar.png"/> |
19 | 10 | </DeviceFrame>
|
20 | 11 | <DeviceFrame type="android">
|
21 |
| -<img src="https://raw.githubusercontent.com/nativescript-vue/nativescript-vue-ui-tests/master/screenshots/android23/SegmentedBar.png" /> |
| 12 | +<img src="../screenshots/android/SegmentedBar.png"/> |
22 | 13 | </DeviceFrame>
|
23 | 14 |
|
24 |
| -### Creating a SegmentedBar with `SegmentedBarItem` |
25 |
| - |
26 |
| -<!-- /// flavor plain --> |
27 |
| - |
28 |
| -```xml |
29 |
| -<SegmentedBar> |
30 |
| - <SegmentedBarItem title="First" /> |
31 |
| - <SegmentedBarItem title="Second" /> |
32 |
| - <SegmentedBarItem title="Third" /> |
33 |
| -</SegmentedBar> |
34 |
| -``` |
35 |
| - |
36 |
| -<!-- /// |
37 |
| -
|
38 |
| -/// flavor angular |
39 |
| -
|
40 |
| -```xml |
41 |
| -<SegmentedBar> |
42 |
| - <SegmentedBarItem title="First"></SegmentedBarItem> |
43 |
| - <SegmentedBarItem title="Second"></SegmentedBarItem> |
44 |
| - <SegmentedBarItem title="Third"></SegmentedBarItem> |
45 |
| -</SegmentedBar> |
46 |
| -``` |
47 |
| -
|
48 |
| -/// |
49 |
| -
|
50 |
| -/// flavor svelte |
51 |
| -
|
52 |
| -```xml |
53 |
| -<segmentedBar> |
54 |
| - <segmentedBarItem title="First" /> |
55 |
| - <segmentedBarItem title="Second" /> |
56 |
| - <segmentedBarItem title="Third" /> |
57 |
| -</segmentedBar> |
58 |
| -``` |
59 |
| -
|
60 |
| -/// |
61 |
| -
|
62 |
| -/// flavor react |
63 |
| -
|
64 |
| -```tsx |
65 |
| -<segmentedBar> |
66 |
| - <segmentedBarItem title="First" /> |
67 |
| - <segmentedBarItem title="Second" /> |
68 |
| - <segmentedBarItem title="Third" /> |
69 |
| -</segmentedBar> |
70 |
| -``` |
71 |
| -
|
72 |
| -/// |
73 |
| -
|
74 |
| -/// flavor vue |
75 |
| -
|
76 |
| -```xml |
77 |
| -<SegmentedBar> |
78 |
| - <SegmentedBarItem title="First" /> |
79 |
| - <SegmentedBarItem title="Second" /> |
80 |
| - <SegmentedBarItem title="Third" /> |
81 |
| -</SegmentedBar> |
82 |
| -``` |
83 |
| -
|
84 |
| -/// |
85 |
| ---> |
86 |
| - |
87 |
| -### SegmentedBar with `selectedIndex` |
88 |
| - |
89 |
| -To handle a `selectedIndex` change, listen to the `selectedIndexChanged` event. |
90 |
| - |
91 |
| -<!-- /// flavor plain --> |
92 |
| - |
93 |
| -```xml |
94 |
| -<SegmentedBar row="0" class="m-5" selectedIndex="{{ sbSelectedIndex }}" |
95 |
| - selectedIndexChanged=" {{ onSelectedIndexChange }} "> |
96 |
| - <SegmentedBar.items> |
97 |
| - <SegmentedBarItem title="Item 1" /> |
98 |
| - <SegmentedBarItem title="Item 2" /> |
99 |
| - <SegmentedBarItem title="Item 3" /> |
100 |
| - </SegmentedBar.items> |
101 |
| -</SegmentedBar> |
102 |
| -``` |
103 |
| - |
104 |
| -```ts |
105 |
| -import { Observable } from '@nativescript/core' |
106 |
| -export class HelloWorldModel extends Observable { |
107 |
| - sbSelectedIndex = 0 |
108 |
| - |
109 |
| - onSelectedIndexChange(args: SelectedIndexChangedEventData) { |
110 |
| - console.log('new value: ' + args.newIndex, 'old: ' + args.oldIndex) |
111 |
| - } |
112 |
| -} |
113 |
| -``` |
114 |
| - |
115 |
| -<!-- /// |
116 |
| -
|
117 |
| -/// flavor angular |
118 |
| -
|
119 |
| -```xml |
120 |
| -<SegmentedBar |
121 |
| - [items]="mySegmentedBarItems" |
122 |
| - selectedIndex="0" |
123 |
| - (selectedIndexChanged)="onSelectedIndexChanged($event)" |
124 |
| -> |
125 |
| -</SegmentedBar> |
126 |
| -``` |
127 |
| -
|
128 |
| -```ts |
129 |
| -import { Component, ChangeDetectionStrategy } from '@angular/core' |
130 |
| -import { |
131 |
| - SegmentedBar, |
132 |
| - SegmentedBarItem, |
133 |
| - SelectedIndexChangedEventData |
134 |
| -} from '@nativescript/core' |
135 |
| -
|
136 |
| -@Component({ |
137 |
| - moduleId: module.id, |
138 |
| - templateUrl: './usage.component.html', |
139 |
| - changeDetection: ChangeDetectionStrategy.OnPush |
140 |
| -}) |
141 |
| -export class BasicSegmentedBarComponent { |
142 |
| - mySegmentedBarItems: Array<SegmentedBarItem> = [] |
143 |
| -
|
144 |
| - constructor() { |
145 |
| - for (let i = 1; i < 5; i++) { |
146 |
| - const item = new SegmentedBarItem() |
147 |
| - item.title = 'Item ' + i |
148 |
| - this.mySegmentedBarItems.push(item) |
149 |
| - } |
150 |
| - } |
151 |
| -
|
152 |
| - public onSelectedIndexChanged(args: SelectedIndexChangedEventData) { |
153 |
| - const segmentedBar = args.object as SegmentedBar |
154 |
| - const oldIndex = args.oldIndex |
155 |
| - const newIndex = args.newIndex |
156 |
| - } |
157 |
| -} |
158 |
| -``` |
159 |
| -
|
160 |
| -/// |
161 |
| -
|
162 |
| -/// flavor vue |
163 |
| -
|
164 |
| -```xml |
165 |
| -<SegmentedBar |
166 |
| - :items="listOfItems" |
167 |
| - selectedIndex="0" |
168 |
| - @selectedIndexChanged="onSelectedIndexChange" |
169 |
| -/> |
170 |
| -``` |
171 |
| -
|
172 |
| -`<SegmentedBar>` provides two-way data binding using `v-model`. |
173 |
| -
|
174 |
| -```xml |
175 |
| -<SegmentedBar :items="listOfItems" v-model="selectedItem" /> |
176 |
| -``` |
177 |
| -
|
178 |
| -/// |
179 |
| -
|
180 |
| -/// flavor svelte |
181 |
| -
|
182 |
| -```xml |
183 |
| -<segmentedBar selectedIndex="0" on:selectedIndexChanged="{onSelectedIndexChange}" /> |
184 |
| -``` |
185 |
| -
|
186 |
| -`<segmentedBar>` can be populated with `{each}` block. |
187 |
| -
|
188 |
| -```xml |
189 |
| -<segmentedBar> |
190 |
| - {#each listOfItems as item} |
191 |
| - <segmentedBarItem title="{item}" /> |
192 |
| - {/each} |
193 |
| -</segmentedBar> |
194 |
| -``` |
195 |
| -
|
196 |
| -```js |
197 |
| -let listOfItems = ['First', 'Second', 'Third'] |
198 |
| -``` |
199 |
| -
|
200 |
| -`<segmentedBar>` provides two-way data binding of `selectedIndex`. |
201 |
| -
|
202 |
| -```tsx |
203 |
| -<segmentedBar bind:selectedIndex="{selectedItem}" > |
204 |
| -``` |
205 |
| -
|
206 |
| -/// |
207 |
| -
|
208 |
| -/// flavor react |
209 |
| -
|
210 |
| -```tsx |
211 |
| -<segmentedBar |
212 |
| - items={listOfItems} |
213 |
| - selectedIndex={0} |
214 |
| - selectedIndexChanged={onSelectedIndexChange} |
215 |
| -/> |
216 |
| -``` |
217 |
| -
|
218 |
| -/// --> |
| 15 | +<<< @/../examples/src/ui/SegmentedBar/template.xml#example |
219 | 16 |
|
220 | 17 | ## Props
|
221 | 18 |
|
222 | 19 | ### items
|
223 | 20 |
|
224 | 21 | ```ts
|
225 |
| -const segmentedBarItem1 = new SegmentedBarItem() |
226 |
| -segmentedBarItem1.title = 'Item 1' |
227 |
| - |
228 |
| -const segmentedBarItem2 = new SegmentedBarItem() |
229 |
| -segmentedBarItem2.title = 'Item 2' |
230 |
| - |
231 |
| -//SegmentedBar |
232 |
| -const segmentedBar = new SegmentedBar() |
233 |
| - |
234 |
| -segmentedBar.items = [segmentedBarItem1, segmentedBarItem2] |
| 22 | +items: SegmentedBarItem[] |
235 | 23 | ```
|
236 | 24 |
|
237 |
| -An array of items to be displayed in the SegmentedBar. Represents the button labels or icons of the SegmentedBar. |
238 |
| - |
239 |
| ---- |
| 25 | +An array of items to be displayed in the SegmentedBar. |
240 | 26 |
|
241 | 27 | ### selectedIndex
|
242 | 28 |
|
243 |
| -```xml |
244 |
| -<SegmentedBar selectedIndex="{{ sbSelectedIndex }}"> |
245 |
| -``` |
246 |
| - |
247 | 29 | ```ts
|
248 |
| -export class HelloWorldModel extends Observable { |
249 |
| - sbSelectedIndex = 0 |
250 |
| -} |
| 30 | +selectedIndex: number |
251 | 31 | ```
|
252 | 32 |
|
253 | 33 | Gets or sets the index of the selected item.
|
254 | 34 |
|
255 |
| ---- |
256 |
| - |
257 | 35 | ### selectedBackgroundColor
|
258 | 36 |
|
259 |
| -```xml |
260 |
| -<SegmentedBar selectedBackgroundColor="red"> |
261 |
| -</SegmentedBar> |
| 37 | +```ts |
| 38 | +selectedBackgroundColor: Color |
262 | 39 | ```
|
263 | 40 |
|
264 |
| -Gets or sets the background color of the selected item. To set the background color of the entire bar, use `backgroundColor`. |
| 41 | +Gets or sets the background color of the selected item. |
265 | 42 |
|
266 |
| ---- |
| 43 | +To set the background color of the entire bar, use `backgroundColor`. |
267 | 44 |
|
268 |
| -### ...Inherited |
| 45 | +See [Color](/api/class/Color). |
269 | 46 |
|
270 |
| -For additional inherited properties, refer to the [API Reference](https://docs.nativescript.org/api-reference/classes/segmentedbar). |
| 47 | +### ...Inherited |
271 | 48 |
|
272 |
| -## Events |
| 49 | +For additional inherited properties, refer to the [API Reference](/api/class/SegmentedBar). |
273 | 50 |
|
274 |
| -### selectedIndexChanged |
| 51 | +## SegmentedBarItem Props |
275 | 52 |
|
276 |
| -```xml |
277 |
| -<SegmentedBar selectedIndexChanged=" {{ onSelectedIndexChange }} "> |
278 |
| -``` |
| 53 | +### title |
279 | 54 |
|
280 | 55 | ```ts
|
281 |
| -onSelectedIndexChange(args: SelectedIndexChangedEventData) { |
282 |
| - const segmentedBar = args.object as SegmentedBar |
283 |
| -} |
| 56 | +title: string |
284 | 57 | ```
|
285 | 58 |
|
286 |
| -Emitted when the an item on the SegmentedBar is tapped. See the [SelectedIndexChangedEventData interface](#selectedindexchangedeventdata-interface) for the event data. |
| 59 | +Gets or sets the title of the SegmentedBarItem. |
287 | 60 |
|
288 |
| ---- |
| 61 | +## Events |
289 | 62 |
|
290 |
| -### SelectedIndexChangedEventData interface |
| 63 | +### selectedIndexChanged |
| 64 | + |
| 65 | +```ts |
| 66 | +on('selectedIndexChanged', (args: EventData) => { |
| 67 | + const segmentedBar = args.object as SegmentedBar |
| 68 | + console.log('SegmentedBar index changed to:', segmentedBar.selectedIndex) |
| 69 | +}) |
| 70 | +``` |
291 | 71 |
|
292 |
| -The `SelectedIndexChangedEventData` object provides the following data: |
293 |
| -| Name | Type | Description | |
294 |
| -|------|------|-------------| |
295 |
| -| `oldIndex`| `number`| The old selected index.| |
296 |
| -| `newIndex`| `number`| The new selected index.| |
| 72 | +Emitted when an item in the SegmentedBar is tapped. |
297 | 73 |
|
298 | 74 | ## Native component
|
299 | 75 |
|
|
0 commit comments