|
1 | 1 | ---
|
2 | 2 | title: Switch
|
| 3 | +description: UI component for toggling state on or off. |
| 4 | +contributors: |
| 5 | + - rigor789 |
| 6 | + - Ombuweb |
3 | 7 | ---
|
4 | 8 |
|
5 |
| -<!-- TODO: Add flvaors& Vue checkedChange event listener--> |
6 |
| - |
7 | 9 | `<Switch>` is a UI component that lets users toggle between two states.
|
8 | 10 |
|
9 |
| -The default state is `false` or OFF. |
10 |
| - |
11 |
| ---- |
12 |
| - |
13 | 11 | <DeviceFrame type="ios">
|
14 |
| -<img src="https://raw.githubusercontent.com/nativescript-vue/nativescript-vue-ui-tests/master/screenshots/ios-simulator103iPhone6/Switch.png"/> |
| 12 | +<img src="../screenshots/ios/Switch.png"/> |
15 | 13 | </DeviceFrame>
|
16 | 14 | <DeviceFrame type="android">
|
17 |
| -<img src="https://raw.githubusercontent.com/nativescript-vue/nativescript-vue-ui-tests/master/screenshots/android23/Switch.png" /> |
| 15 | +<img src="../screenshots/android/Switch.png"/> |
18 | 16 | </DeviceFrame>
|
19 | 17 |
|
20 |
| -### Simple Switch |
21 |
| - |
22 |
| -To be notified when the Switch state changes, listen for the `checkedChange` event. |
23 |
| - |
24 |
| -<!-- /// flavor plain --> |
25 |
| - |
26 |
| -```xml |
27 |
| -<Switch checked="{{ checked }}" loaded="{{ onSwitchLoaded }}" /> |
28 |
| -``` |
29 |
| - |
30 |
| -```ts |
31 |
| -export class HelloWorldModel extends Observable { |
32 |
| - checked = true |
33 |
| - constructor() { |
34 |
| - super() |
35 |
| - } |
36 |
| - |
37 |
| - onSwitchLoaded(args: EventData) { |
38 |
| - const switchComponent = args.object as Switch |
39 |
| - |
40 |
| - switchComponent.on('checkedChange', (args: PropertyChangeData) => { |
41 |
| - console.log('checkedChange: ', args.value, 'Old: ' + args.oldValue) |
42 |
| - }) |
43 |
| - } |
44 |
| -} |
45 |
| -``` |
46 |
| - |
47 |
| -<!-- /// |
48 |
| -
|
49 |
| -/// flavor angular |
| 18 | +<<< @/../examples/src/ui/Switch/template.xml#example |
50 | 19 |
|
51 |
| -```xml |
52 |
| -<Switch checked="true" (checkedChange)="onCheckedChange($event)"> </Switch> |
53 |
| -``` |
54 |
| -
|
55 |
| -```ts |
56 |
| -import { Component } from '@angular/core' |
57 |
| -import { EventData, Switch } from '@nativescript/core' |
58 |
| -
|
59 |
| -@Component({ |
60 |
| - moduleId: module.id, |
61 |
| - templateUrl: './usage.component.html' |
62 |
| -}) |
63 |
| -export class BasicSwitchComponent { |
64 |
| - onCheckedChange(args: EventData) { |
65 |
| - const sw = args.object as Switch |
66 |
| - const isChecked = sw.checked // boolean |
67 |
| - } |
68 |
| -} |
69 |
| -``` |
| 20 | +## Examples |
70 | 21 |
|
71 |
| -/// |
72 |
| -
|
73 |
| -/// flavor vue |
74 |
| -
|
75 |
| -```xml |
76 |
| -<Switch checked="true" @checkedChange="onCheckedChange"/> |
77 |
| -``` |
78 |
| -```ts |
79 |
| -onCheckedChange(args: PropertyChangeData) { |
80 |
| -
|
81 |
| - console.log(args.value) |
82 |
| -} |
83 |
| -``` |
84 |
| -`<Switch>`provides two-way data binding using `v-model`. |
| 22 | +### Styling a Switch |
85 | 23 |
|
86 | 24 | ```xml
|
87 |
| -<Switch v-model="itemEnabled" /> |
88 |
| -``` |
89 |
| -
|
90 |
| -/// |
91 |
| -
|
92 |
| -/// flavor svelte |
93 |
| -
|
94 |
| -```tsx |
95 |
| -<switch checked="{true}" on:checkedChange="{onCheckedChange}" /> |
96 |
| -``` |
97 |
| -
|
98 |
| -`<switch>`provides two-way data binding for `checked`. |
99 |
| -
|
100 |
| -```tsx |
101 |
| -<switch bind:checked="{switchEnabled}" /> |
102 |
| -``` |
103 |
| -
|
104 |
| -/// |
105 |
| -
|
106 |
| -/// flavor react |
107 |
| -
|
108 |
| -```tsx |
109 |
| -<switch checked={true} /> |
110 |
| -``` |
111 |
| -
|
112 |
| -/// --> |
113 |
| - |
114 |
| -### Styling Switch |
115 |
| - |
116 |
| -```xml |
117 |
| -<Switch checked="true" color="#BFCDAC" backgroundColor="green" offBackgroundColor="#DC493D"/> |
| 25 | +<Switch checked="true" |
| 26 | + color="#BFCDAC" |
| 27 | + backgroundColor="green" |
| 28 | + offBackgroundColor="#DC493D" |
| 29 | +/> |
118 | 30 | ```
|
119 | 31 |
|
120 | 32 | ## Props
|
121 | 33 |
|
122 | 34 | ### checked
|
123 | 35 |
|
124 |
| -```xml |
125 |
| -<Switch checked="true"/> |
126 |
| -``` |
127 |
| - |
128 | 36 | ```ts
|
129 |
| -switchComponent.checked = false |
| 37 | +checked: boolean |
130 | 38 | ```
|
131 | 39 |
|
132 |
| -Gets or sets the state value of the switch. |
| 40 | +Gets or sets the state switch. |
133 | 41 |
|
134 |
| ---- |
| 42 | +Defaults to `false`. |
135 | 43 |
|
136 | 44 | ### offBackgroundColor
|
137 | 45 |
|
138 |
| -```xml |
139 |
| -<Switch offBackgroundColor="#DC493D"/> |
| 46 | +```ts |
| 47 | +offBackgroundColor: Color |
140 | 48 | ```
|
141 | 49 |
|
142 |
| -Gets or sets the off-state color. |
| 50 | +Gets or sets the off-state background color. |
143 | 51 |
|
144 |
| ---- |
| 52 | +See [Color](/api/class/Color). |
145 | 53 |
|
146 | 54 | ### ...Inherited
|
147 | 55 |
|
148 |
| -For additional inherited properties, refer to the [API Reference](https://docs.nativescript.org/api-reference/classes/switch). |
| 56 | +For additional inherited properties, refer to the [API Reference](/api/class/Switch). |
149 | 57 |
|
150 | 58 | ## Events
|
151 | 59 |
|
152 | 60 | ### checkedChange
|
153 | 61 |
|
154 | 62 | ```ts
|
155 |
| -onSwitchLoaded(args: EventData){ |
156 |
| - const switchComponent = args.object as Switch; |
157 |
| - |
158 |
| - switchComponent.on("checkedChange", (args: PropertyChangeData)=>{ |
159 |
| - |
160 |
| - console.log("checkedChange: ", args.value, "Old: "+args.oldValue) |
161 |
| - |
162 |
| - }) |
163 |
| -} |
| 63 | +on('checkedChange', (args: PropertyChangeData) => { |
| 64 | + const switch = args.object as Switch |
| 65 | + console.log('Switch checked:', args.value) |
| 66 | +}) |
164 | 67 | ```
|
165 | 68 |
|
166 |
| -Emitted when the switch state changes. See [PropertyChangeData Interface](https://docs.nativescript.org/api-reference/interfaces/propertychangedata) for the event data. |
| 69 | +See [PropertyChangeData](/api/interface/PropertyChangeData). |
167 | 70 |
|
168 | 71 | ## Native component
|
169 | 72 |
|
|
0 commit comments