Skip to content

Commit f3863ea

Browse files
Merge pull request #63 from EddyVerbruggen/boxType
boxType iOS-Android alignment
2 parents 5e5ac05 + 5724207 commit f3863ea

10 files changed

+50
-47
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export class SomeComponent {
9797
- **checked** - boolean
9898
- **text** - text to use with the checkbox
9999
- **fillColor** - Color of the checkbox element
100+
- **boxType** - Either 'square' (default) or 'circle'. It's recommended to use 'circle' for radiobuttons. Note that plugin version 3.0.0 switched the default for iOS to 'square' for alignment with Android. Still want `circle` on iOS and `square` on Android? Just make the `boxType` value conditional.
100101

101102
## Events
102103

@@ -130,8 +131,7 @@ Add the following to `app/App_Resources/Android/drawable/checkbox_grey.xml`
130131

131132
## Radiobuttons, anyone?
132133
Want to use radiobutton behavior for your checkboxes (only one option possible within a group)?
133-
134-
Check out the second tab in the [Angular demo](demo-ng/), here's a screenshot:
134+
Set `boxType="circle"` and check out the second tab in the [Angular demo](demo-ng/), here's a screenshot:
135135

136136
<img src="./screens/radiobuttons.png" width="225px"/>
137137

checkbox-common.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum BoxType {
2+
circle = <any>"circle",
3+
square = <any>"square"
4+
}

checkbox.android.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { Color } from "tns-core-modules/color";
22
import { device } from "tns-core-modules/platform";
33
import app = require("tns-core-modules/application");
4-
import { CheckBoxInterface } from ".";
54
import {
65
View,
76
Property,
87
CssProperty,
98
Style,
109
booleanConverter
1110
} from "tns-core-modules/ui/core/view";
11+
import { CheckBoxInterface } from ".";
12+
import { BoxType } from "./checkbox-common";
13+
1214
declare const android: any, java: any;
1315

1416
export const checkedProperty = new Property<CheckBox, boolean>({
@@ -41,14 +43,9 @@ export const tintColorProperty = new CssProperty<Style, string>({
4143
}
4244
});
4345

44-
export const enum BoxType {
45-
Circle = 1,
46-
Square = 2
47-
}
48-
4946
export class CheckBox extends View implements CheckBoxInterface {
5047
private _android: any; /// android.widget.CheckBox
51-
private _boxType: number;
48+
private _boxType: string;
5249
private _checkStyle: string;
5350
private _checkPadding: string;
5451
private _checkPaddingLeft: string;
@@ -64,7 +61,7 @@ export class CheckBox extends View implements CheckBoxInterface {
6461
return this._android;
6562
}
6663

67-
set boxType(value: number) {
64+
set boxType(value: string) {
6865
this._boxType = value;
6966
}
7067

@@ -155,7 +152,7 @@ export class CheckBox extends View implements CheckBoxInterface {
155152

156153
public createNativeView() {
157154
this._android = new android.support.v7.widget[
158-
this.boxType == BoxType.Circle
155+
BoxType[this.boxType] === BoxType.circle
159156
? "AppCompatRadioButton"
160157
: "AppCompatCheckBox"
161158
](this._context, null);

checkbox.ios.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CheckBoxInterface } from "./index";
2+
import { BoxType } from "./checkbox-common";
23
import {
34
Property,
45
CssProperty,
@@ -57,10 +58,12 @@ const checkedProperty = new Property<CheckBox, boolean>({
5758
valueChanged: onCheckedPropertyChanged
5859
});
5960

60-
const boxTypeProperty = new Property<CheckBox, number>({
61+
const boxTypeProperty = new Property<CheckBox, BEMBoxType>({
6162
name: "boxType",
6263
valueConverter: v => {
63-
return parseInt(v, 10);
64+
return BoxType[v] === BoxType.circle
65+
? BEMBoxType.Circle
66+
: BEMBoxType.Square;
6467
}
6568
});
6669

@@ -121,16 +124,10 @@ export class CheckBox extends Button implements CheckBoxInterface {
121124
this._iosCheckbox.onCheckColor = new Color(color).ios;
122125
}
123126

124-
[boxTypeProperty.getDefault](): number {
125-
return 1;
126-
}
127-
128-
[boxTypeProperty.setNative](value: number) {
129-
let type = BEMBoxType.Circle;
130-
if (value === 2) {
131-
type = BEMBoxType.Square;
127+
[boxTypeProperty.setNative](value: any) {
128+
if (this._iosCheckbox) {
129+
this._iosCheckbox.boxType = value;
132130
}
133-
this._iosCheckbox.boxType = type;
134131
}
135132

136133
[checkedProperty.getDefault](): boolean {
@@ -236,9 +233,8 @@ export class CheckBox extends Button implements CheckBoxInterface {
236233
if (typeof this._hideBox !== "undefined") {
237234
this.hideBox = this._hideBox;
238235
}
239-
if (typeof this._boxType !== "undefined") {
240-
this.boxType = this._boxType;
241-
}
236+
237+
this.boxType = this.boxType === 0 ? BEMBoxType.Circle : BEMBoxType.Square;
242238

243239
if (typeof this._animationDuration !== "undefined") {
244240
this.animationDuration = this._animationDuration;

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
<StackLayout *tabItem="{title: 'Checkboxes'}">
88
<StackLayout class="page">
99
<StackLayout orientation="horizontal" class="p-10">
10-
<CheckBox #modelCheck [(ngModel)]="checkTest" class="checkbox" boxType="2" (checkedChange)="checkedChange(modelCheck)"></CheckBox>
10+
<!-- boxType="square" is the default, just testing this works as well -->
11+
<CheckBox #modelCheck [(ngModel)]="checkTest" class="checkbox" boxType="square" (checkedChange)="checkedChange(modelCheck)"></CheckBox>
1112
<Label text="Test NgModel" (tap)="modelCheck.toggle()"></Label>
1213
</StackLayout>
1314
<StackLayout class="form-group" [formGroup]="formGroup" orientation="horizontal" class="p-10">
14-
<CheckBox #reactiveCheck class="checkbox" formControlName="testCheck" boxType="2" class="checkbox"></CheckBox>
15+
<CheckBox #reactiveCheck class="checkbox" formControlName="testCheck" class="checkbox"></CheckBox>
1516
<Label text="Test Reactive FormGroup" (tap)="reactiveCheck.toggle()"></Label>
1617
</StackLayout>
1718

@@ -33,7 +34,7 @@
3334
<Label class="p-10" text="Tap either the buttons or the labels. You can even unselect the radiobutton selection." textWrap="true"></Label>
3435
<StackLayout class="p-10" *ngFor="let option of radioOptions">
3536
<StackLayout orientation="horizontal" verticalAlignment="center">
36-
<CheckBox #elem [checked]="option.selected" (checkedChange)="elem.checked !== option.selected && changeCheckedRadio(option)" class="checkbox"></CheckBox>
37+
<CheckBox #elem [checked]="option.selected" (checkedChange)="elem.checked !== option.selected && changeCheckedRadio(option)" class="checkbox" boxType="circle"></CheckBox>
3738
<StackLayout verticalAlignment="center">
3839
<Label [text]="option.text" textWrap="true" (tap)="changeCheckedRadio(option)"></Label>
3940
</StackLayout>

demo-ng/package.json

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"id": "org.nativescript.demong",
88
"tns-ios": {
99
"version": "3.0.1"
10+
},
11+
"tns-android": {
12+
"version": "3.2.0"
1013
}
1114
},
1215
"dependencies": {
@@ -27,6 +30,10 @@
2730
"zone.js": "~0.8.2"
2831
},
2932
"devDependencies": {
33+
"babel-traverse": "6.26.0",
34+
"babel-types": "6.26.0",
35+
"babylon": "6.18.0",
36+
"lazy": "1.0.11",
3037
"nativescript-dev-typescript": "~0.4.0",
3138
"typescript": "~2.2.1"
3239
}

demo/app/main-page.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ ListView{
2020

2121

2222
.fontBig {
23-
font-size: 40;
23+
font-size: 30;
2424
}

demo/app/main-page.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
<TabViewItem.view>
99
<ScrollView>
1010
<StackLayout id="wrapper">
11-
<CheckBox:CheckBox id="fontSizeTest" boxType="2" fillColor="#fff000" onCheckColor="green" text="Font Size Test" tint="red" tintColor="red" checked="false" class="fontBig" />
11+
<CheckBox:CheckBox boxType="circle" id="fontSizeTest" fillColor="#fff000" onCheckColor="green" text="Font Size Circle Test" tint="red" tintColor="red" checked="false" class="fontBig" />
1212
<Label text="Functions" class="title" />
1313
<StackLayout class="listitem">
1414
<GridLayout columns="*, auto" class="demosection">
15-
<CheckBox:CheckBox checkPadding="36" checkStyle="checkbox_grey" id="toggleTest" col="0" text="toggle()" checked="false" style="margin-right: 10" />
15+
<CheckBox:CheckBox boxType="square" checkPadding="36" checkStyle="checkbox_grey" id="toggleTest" col="0" text="toggle()" checked="false" style="margin-right: 10" />
1616
<Button col="1" text="GO" tap="onToggleTest" class="button" />
1717
</GridLayout>
1818
</StackLayout>

index.d.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
21
import { View } from "tns-core-modules/ui/core/view";
2+
33
/**
44
* Represents a CheckBox component.
55
*/
66
export declare class CheckBox extends View {
7-
8-
/**
7+
/**
98
* Gets the native [android widget](https://developer.android.com/reference/android/widget/CheckBox.html) that represents the user interface for this component. Valid only when running on Android OS.
109
*/
11-
android: any /* android.widget.CheckBox */;
10+
android: any /* android.widget.CheckBox */;
1211

13-
/**
12+
/**
1413
* Gets the ios Label with the checkbox as a subview
1514
*/
16-
ios: any /* Label */;
15+
ios: any /* Label */;
1716

18-
/**
17+
/**
1918
* Gets or sets if a switch is checked or not.
2019
*/
21-
checked: boolean;
20+
checked: boolean;
2221

23-
/**
22+
/**
2423
* Change the checked state of the view to the inverse of its current state.
2524
*/
26-
toggle(): void;
27-
25+
toggle(): void;
2826
}
2927

3028
export interface CheckBoxInterface {
31-
text?: string;
32-
checked: boolean;
33-
toggle(): void;
34-
}
29+
text?: string;
30+
checked: boolean;
31+
toggle(): void;
32+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-checkbox",
3-
"version": "2.1.15",
3+
"version": "3.0.0",
44
"description": "NativeScript plugin for checkbox widget.",
55
"main": "checkbox",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)