Skip to content

Commit 17263fa

Browse files
Merge pull request #43 from EddyVerbruggen/radiobuttons
Radiobuttons
2 parents a2aaec3 + 25c87fc commit 17263fa

File tree

7 files changed

+84
-19
lines changed

7 files changed

+84
-19
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ Add the following to `app/App_Resources/Android/drawable/checkbox_grey.xml`
124124
</selector>
125125
```
126126

127+
## Radiobuttons, anyone?
128+
Want to use radiobutton behavior for your checkboxes (only one option possible within a group)?
129+
130+
Check out the second tab in the [Angular demo](demo-ng/), here's a screenshot:
131+
132+
<img src="./screens/radiobuttons.png" width="225px"/>
127133

128134
## Demo Setup
129135
* npm i

demo-ng/app/item/items.component.html

+40-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
11
<ActionBar title="My App" class="action-bar">
22
</ActionBar>
3-
<StackLayout class="page">
4-
<StackLayout orientation="horizontal" class="p-10">
5-
<CheckBox #modelCheck [(ngModel)]="checkTest" class="checkbox"></CheckBox>
6-
<Label text="Test NgModel" (tap)="modelCheck.toggle()"></Label>
3+
4+
<TabView>
5+
6+
<!-- Tab1: Checkboxes -->
7+
<StackLayout *tabItem="{title: 'Checkboxes'}">
8+
<StackLayout class="page">
9+
<StackLayout orientation="horizontal" class="p-10">
10+
<CheckBox #modelCheck [(ngModel)]="checkTest" class="checkbox"></CheckBox>
11+
<Label text="Test NgModel" (tap)="modelCheck.toggle()"></Label>
12+
</StackLayout>
13+
<StackLayout class="form-group" [formGroup]="formGroup" orientation="horizontal" class="p-10">
14+
<CheckBox #reactiveCheck class="checkbox" formControlName="testCheck" class="checkbox"></CheckBox>
15+
<Label text="Test Reactive FormGroup" (tap)="reactiveCheck.toggle()"></Label>
16+
</StackLayout>
17+
18+
<StackLayout class="p-10">
19+
<Button text="Submit Values" (tap)="submit()" class="btn btn-primary"></Button>
20+
</StackLayout>
21+
22+
<ListView [items]="items" class="list-group">
23+
<ng-template let-item="item">
24+
<Label [nsRouterLink]="['/item', item.id]" [text]="item.name"
25+
class="list-group-item"></Label>
26+
</ng-template>
27+
</ListView>
28+
</StackLayout>
729
</StackLayout>
8-
<StackLayout class="form-group" [formGroup]="formGroup" orientation="horizontal" class="p-10">
9-
<CheckBox #reactiveCheck class="checkbox" formControlName="testCheck" class="checkbox"></CheckBox>
10-
<Label text="Test Reactive FormGroup" (tap)="reactiveCheck.toggle()"></Label>
11-
30+
31+
<!-- Tab2: Radiobuttons -->
32+
<StackLayout *tabItem="{title: 'Radiobuttons'}">
33+
<Label class="p-10" text="Tap either the buttons or the labels. You can even unselect the radiobutton selection." textWrap="true"></Label>
34+
<StackLayout class="p-10" *ngFor="let option of radioOptions">
35+
<StackLayout orientation="horizontal" verticalAlignment="center">
36+
<CheckBox #elem [checked]="option.selected" (checkedChange)="elem.checked !== option.selected && changeCheckedRadio(option)" class="checkbox"></CheckBox>
37+
<StackLayout verticalAlignment="center">
38+
<Label [text]="option.text" textWrap="true" (tap)="changeCheckedRadio(option)"></Label>
39+
</StackLayout>
40+
</StackLayout>
41+
</StackLayout>
1242
</StackLayout>
13-
<StackLayout class="p-10">
14-
<Button text="Submit Values" (tap)="submit()" class="btn btn-primary"></Button>
15-
</StackLayout>
16-
<ListView [items]="items" class="list-group">
17-
<ng-template let-item="item">
18-
<Label [nsRouterLink]="['/item', item.id]" [text]="item.name"
19-
class="list-group-item"></Label>
20-
</ng-template>
21-
</ListView>
22-
</StackLayout>
43+
44+
</TabView>

demo-ng/app/item/items.component.ts

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FormBuilder, FormGroup, Validators } from "@angular/forms";
33

44
import { Item } from "./item";
55
import { ItemService } from "./item.service";
6+
import { RadioOption } from "./radio-option";
67

78
@Component({
89
selector: "ns-items",
@@ -13,6 +14,7 @@ export class ItemsComponent implements OnInit {
1314
formGroup: FormGroup;
1415
checkTest: boolean;
1516
items: Item[];
17+
radioOptions?: Array<RadioOption>;
1618

1719
constructor(
1820
private formBuilder: FormBuilder,
@@ -33,10 +35,32 @@ export class ItemsComponent implements OnInit {
3335
});
3436

3537
this.items = this.itemService.getItems();
38+
39+
// Plain ol' inline Array definition coming up :)
40+
this.radioOptions = [
41+
new RadioOption("Radio option 1"),
42+
new RadioOption("Radio option 2"),
43+
new RadioOption("Radio option 3")
44+
];
3645
}
3746

3847
public submit() {
3948
console.log('NgModel value:', this.checkTest);
4049
console.log('Reactive FormGroup value:', this.formGroup.get('testCheck').value);
4150
}
51+
52+
changeCheckedRadio(radioOption: RadioOption): void {
53+
radioOption.selected = !radioOption.selected;
54+
55+
if (!radioOption.selected) {
56+
return;
57+
}
58+
59+
// uncheck all other options
60+
this.radioOptions.forEach(option => {
61+
if (option.text !== radioOption.text) {
62+
option.selected = false;
63+
}
64+
});
65+
}
4266
}

demo-ng/app/item/radio-option.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class RadioOption {
2+
text: string;
3+
selected: boolean = false;
4+
5+
constructor(text: string) {
6+
this.text = text;
7+
}
8+
}

demo-ng/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"@angular/platform-browser": "~4.1.0",
2020
"@angular/router": "~4.1.0",
2121
"nativescript-angular": "~3.0.0",
22-
"nativescript-checkbox": "file:///Users/nathan/Documents/github/bradmartin/nativescript-checkbox",
22+
"nativescript-checkbox": "file:..",
2323
"nativescript-theme-core": "~1.0.2",
2424
"reflect-metadata": "~0.1.8",
2525
"rxjs": "~5.3.0",

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
"name": "Osei Fortune",
5757
"email": "[email protected]",
5858
"url": "https://github.com/triniwiz"
59+
},
60+
{
61+
"name": "Eddy Verbruggen",
62+
"email": "[email protected]",
63+
"url": "https://github.com/EddyVerbruggen"
5964
}
6065
],
6166
"bugs": {

screens/radiobuttons.png

56.1 KB
Loading

0 commit comments

Comments
 (0)